Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Postscreen extends StatefulWidget {
- const Postscreen({Key? key}) : super(key: key);
- @override
- State<Postscreen> createState() => _PostscreenState();
- }
- class _PostscreenState extends State<Postscreen> {
- //post messges
- final FirestoreDatabase database = FirestoreDatabase();
- final posttitleController = TextEditingController();
- final postController = TextEditingController();
- final postfileController= TextEditingController();
- final FirebaseStorage storage = FirebaseStorage.instance;
- final currentUser = FirebaseAuth.instance.currentUser!;
- @override
- initState() {
- super.initState();
- }
- //pick files and uploading them to firebase after i download the url
- PlatformFile? pickedFile;
- UploadTask? uploadTask;
- Future selectFile()async{
- final result= await FilePicker.platform.pickFiles();
- if (result==null) return;
- setState(() {
- pickedFile=result.files.first;
- });
- String? mimeStr = lookupMimeType(pickedFile!.path!);
- var fileType = mimeStr?.split('/');
- print('file type $fileType');
- try {
- final path = 'postfiles/${pickedFile!.name}';
- final file = File(pickedFile!.path!);
- final ref = FirebaseStorage.instance.ref().child(path);
- uploadTask = ref.putFile(file);
- final imageUrl = await uploadTask!.whenComplete(() {});
- final urlDownload = await imageUrl.ref.getDownloadURL();
- print('firebase image =$urlDownload');
- setState(() {
- postfileController.text = urlDownload;
- });
- }catch(e){
- ScaffoldMessenger.of(context).showSnackBar(
- const SnackBar(
- content: Text(
- 'check your details /network, and try again',
- ),
- ),
- );
- }
- }
- //here i was posting the content
- Future<void> postMessage() async {
- if (postController.text.isNotEmpty && posttitleController.text.isNotEmpty) {
- String message = postController.text;
- String title = posttitleController.text;
- String fileurl=postfileController.text;
- database.addPosts(message, title,fileurl);
- ScaffoldMessenger.of(context).showSnackBar(
- const SnackBar(
- content: Text(
- 'Post is succesfully created..',
- ),
- ),
- );
- } else {
- ScaffoldMessenger.of(context).showSnackBar(
- const SnackBar(
- content: Text(
- 'please enter some text',
- ),
- ),
- );
- }
- postController.clear();
- posttitleController.clear();
- }
- @override
- void dispose() {
- postController.dispose();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- //String? mimeStr = lookupMimeType(pickedFile!.path!);
- //var fileType = mimeStr?.split('/');
- //print('file type ${fileType}');
- return Scaffold(
- backgroundColor: const Color.fromARGB(255, 245, 242, 242),
- appBar: AppBar(
- leading: const Icon(Icons.create,color: Colors.white,size: 32,),
- title: const Text('CREATE A POST',
- style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold)),
- backgroundColor: Colors.deepPurple
- ),
- body: SafeArea(
- child: SingleChildScrollView(
- child: Column(
- children: <Widget>[
- //padding: const EdgeInsets.symmetric(horizontal: 16.0),
- Container(
- margin: const EdgeInsets.all(8.0),
- child: TextField(
- controller: posttitleController,
- keyboardType: TextInputType.multiline,
- textInputAction: TextInputAction.done,
- maxLines: 2,
- decoration: const InputDecoration(
- //labelText: 'Say something to Community ',
- hintText: 'Title ',
- //prefixIcon: Icon(Icons.add),
- border: OutlineInputBorder(),
- //filled:true,
- ),
- ),
- ),
- _gap(),
- Container(
- margin: const EdgeInsets.all(8.0),
- child: TextField(
- controller: postController,
- keyboardType: TextInputType.multiline,
- textInputAction: TextInputAction.done,
- maxLines: 5,
- decoration: const InputDecoration(
- //labelText: 'Say something to Community ',
- hintText: 'Say something to Community ',
- //prefixIcon: Icon(Icons.add),
- border: OutlineInputBorder(),
- //filled:true,
- ),
- ),
- ),
- _gap(),
- const SizedBox(height: 10),
- // image container if the image is not null
- if (pickedFile != null)
- //if(fileType=='[image, jpeg]')
- Container(
- alignment: Alignment.center,
- width: double.infinity,
- //height: 300,
- color: Colors.grey[300],
- child: Image.file(
- File(pickedFile!.path!),
- width: double.infinity,
- fit: BoxFit.cover,
- )),
- _gap(),
- Container(
- margin: const EdgeInsets.all(8.0),
- child: TextField(
- controller: postfileController,
- keyboardType: TextInputType.multiline,
- textInputAction: TextInputAction.done,
- readOnly: true,
- maxLines: 1,
- decoration: const InputDecoration(
- //labelText: 'Say something to Community ',
- hintText: 'File url',
- //prefixIcon: Icon(Icons.add),
- border: OutlineInputBorder(),
- //filled:true,
- ),
- ),
- ),
- _gap(),
- SizedBox(
- //width: double.infinity,
- child: ElevatedButton(
- style: ElevatedButton.styleFrom(
- backgroundColor: Colors.deepPurple,
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(4)),
- ),
- onPressed: selectFile,
- child: const Padding(
- padding: EdgeInsets.all(10.0),
- child: Text(
- 'Upload File',
- style: TextStyle(
- fontSize: 16,
- fontWeight: FontWeight.bold,
- color: Color.fromARGB(255, 246, 244, 244)),
- ),
- ),
- ),
- ),
- _gap(),
- SizedBox(
- //width: double.infinity,
- child: ElevatedButton(
- style: ElevatedButton.styleFrom(
- backgroundColor: Colors.deepPurple,
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(4)),
- ),
- onPressed: postMessage,
- child: const Padding(
- padding: EdgeInsets.all(10.0),
- child: Text(
- 'Post',
- style: TextStyle(
- fontSize: 16,
- fontWeight: FontWeight.bold,
- color: Color.fromARGB(255, 248, 247, 247)),
- ),
- ),
- ),
- ),
- _gap(),
- ],
- ),
- ),
- ),
- );
- }
- Widget _gap() => const SizedBox(height: 16);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement