Advertisement
jevixlugya

trying to post text with images,videos/audios files

Apr 12th, 2024
697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 7.85 KB | None | 0 0
  1. class Postscreen extends StatefulWidget {
  2.   const Postscreen({Key? key}) : super(key: key);
  3.  
  4.   @override
  5.   State<Postscreen> createState() => _PostscreenState();
  6. }
  7.  
  8. class _PostscreenState extends State<Postscreen> {
  9.   //post messges
  10.   final FirestoreDatabase database = FirestoreDatabase();
  11.   final posttitleController = TextEditingController();
  12.   final postController = TextEditingController();
  13.   final postfileController= TextEditingController();
  14.   final FirebaseStorage storage = FirebaseStorage.instance;
  15.   final currentUser = FirebaseAuth.instance.currentUser!;
  16.  
  17.   @override
  18.   initState() {
  19.     super.initState();
  20.   }
  21. //pick files and uploading them to firebase after i download the url
  22.   PlatformFile? pickedFile;
  23.   UploadTask? uploadTask;
  24.   Future selectFile()async{
  25.     final result= await FilePicker.platform.pickFiles();
  26.     if (result==null) return;
  27.     setState(() {
  28.       pickedFile=result.files.first;
  29.     });
  30.     String? mimeStr = lookupMimeType(pickedFile!.path!);
  31.     var fileType = mimeStr?.split('/');
  32.     print('file type $fileType');
  33.     try {
  34.  
  35.       final path = 'postfiles/${pickedFile!.name}';
  36.       final file = File(pickedFile!.path!);
  37.       final ref = FirebaseStorage.instance.ref().child(path);
  38.       uploadTask = ref.putFile(file);
  39.       final imageUrl = await uploadTask!.whenComplete(() {});
  40.       final urlDownload = await imageUrl.ref.getDownloadURL();
  41.       print('firebase image =$urlDownload');
  42.  
  43.       setState(() {
  44.         postfileController.text = urlDownload;
  45.       });
  46.     }catch(e){
  47.     ScaffoldMessenger.of(context).showSnackBar(
  48.     const SnackBar(
  49.     content: Text(
  50.     'check your details /network, and try again',
  51.     ),
  52.     ),
  53.     );
  54.     }
  55.   }
  56.  
  57.  
  58. //here i was posting the content
  59.   Future<void> postMessage() async {
  60.  
  61.     if (postController.text.isNotEmpty && posttitleController.text.isNotEmpty) {
  62.       String message = postController.text;
  63.       String title = posttitleController.text;
  64.       String fileurl=postfileController.text;
  65.       database.addPosts(message, title,fileurl);
  66.       ScaffoldMessenger.of(context).showSnackBar(
  67.         const SnackBar(
  68.           content: Text(
  69.             'Post is succesfully created..',
  70.           ),
  71.         ),
  72.       );
  73.     } else {
  74.       ScaffoldMessenger.of(context).showSnackBar(
  75.         const SnackBar(
  76.           content: Text(
  77.             'please enter some text',
  78.           ),
  79.         ),
  80.       );
  81.     }
  82.     postController.clear();
  83.     posttitleController.clear();
  84.  
  85.   }
  86.  
  87.   @override
  88.   void dispose() {
  89.     postController.dispose();
  90.     super.dispose();
  91.   }
  92.  
  93.   @override
  94.   Widget build(BuildContext context) {
  95.     //String? mimeStr = lookupMimeType(pickedFile!.path!);
  96.     //var fileType = mimeStr?.split('/');
  97.     //print('file type ${fileType}');
  98.     return Scaffold(
  99.       backgroundColor: const Color.fromARGB(255, 245, 242, 242),
  100.       appBar: AppBar(
  101.           leading: const Icon(Icons.create,color: Colors.white,size: 32,),
  102.         title: const Text('CREATE A POST',
  103.             style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold)),
  104.         backgroundColor: Colors.deepPurple
  105.       ),
  106.       body: SafeArea(
  107.         child: SingleChildScrollView(
  108.           child: Column(
  109.             children: <Widget>[
  110.               //padding: const EdgeInsets.symmetric(horizontal: 16.0),
  111.               Container(
  112.                 margin: const EdgeInsets.all(8.0),
  113.                 child: TextField(
  114.                   controller: posttitleController,
  115.                   keyboardType: TextInputType.multiline,
  116.                   textInputAction: TextInputAction.done,
  117.                   maxLines: 2,
  118.                   decoration: const InputDecoration(
  119.                     //labelText: 'Say something to Community ',
  120.                     hintText: 'Title ',
  121.                     //prefixIcon: Icon(Icons.add),
  122.                     border: OutlineInputBorder(),
  123.                     //filled:true,
  124.                   ),
  125.                 ),
  126.               ),
  127.               _gap(),
  128.               Container(
  129.                 margin: const EdgeInsets.all(8.0),
  130.                 child: TextField(
  131.                   controller: postController,
  132.                   keyboardType: TextInputType.multiline,
  133.                   textInputAction: TextInputAction.done,
  134.                   maxLines: 5,
  135.                   decoration: const InputDecoration(
  136.                     //labelText: 'Say something to Community ',
  137.                     hintText: 'Say something to Community ',
  138.                     //prefixIcon: Icon(Icons.add),
  139.                     border: OutlineInputBorder(),
  140.                     //filled:true,
  141.                   ),
  142.                 ),
  143.               ),
  144.               _gap(),
  145.               const SizedBox(height: 10),
  146.             // image container if the image is not null
  147.               if (pickedFile != null)
  148.                 //if(fileType=='[image, jpeg]')
  149.                 Container(
  150.                     alignment: Alignment.center,
  151.                     width: double.infinity,
  152.                     //height: 300,
  153.                     color: Colors.grey[300],
  154.                     child: Image.file(
  155.                       File(pickedFile!.path!),
  156.                       width: double.infinity,
  157.                       fit: BoxFit.cover,
  158.                     )),
  159.  
  160.  
  161.          
  162.               _gap(),
  163.               Container(
  164.                 margin: const EdgeInsets.all(8.0),
  165.                 child: TextField(
  166.                   controller: postfileController,
  167.                   keyboardType: TextInputType.multiline,
  168.                   textInputAction: TextInputAction.done,
  169.                   readOnly: true,
  170.                   maxLines: 1,
  171.                   decoration: const InputDecoration(
  172.                     //labelText: 'Say something to Community ',
  173.                     hintText: 'File url',
  174.                     //prefixIcon: Icon(Icons.add),
  175.                     border: OutlineInputBorder(),
  176.                     //filled:true,
  177.                   ),
  178.                 ),
  179.               ),
  180.               _gap(),
  181.               SizedBox(
  182.                 //width: double.infinity,
  183.                 child: ElevatedButton(
  184.                   style: ElevatedButton.styleFrom(
  185.                     backgroundColor: Colors.deepPurple,
  186.                     shape: RoundedRectangleBorder(
  187.                         borderRadius: BorderRadius.circular(4)),
  188.                   ),
  189.                   onPressed: selectFile,
  190.                   child: const Padding(
  191.                     padding: EdgeInsets.all(10.0),
  192.                     child: Text(
  193.                       'Upload File',
  194.                       style: TextStyle(
  195.                           fontSize: 16,
  196.                           fontWeight: FontWeight.bold,
  197.                           color: Color.fromARGB(255, 246, 244, 244)),
  198.                     ),
  199.                   ),
  200.                 ),
  201.               ),
  202.               _gap(),
  203.  
  204.               SizedBox(
  205.                 //width: double.infinity,
  206.                 child: ElevatedButton(
  207.                   style: ElevatedButton.styleFrom(
  208.                     backgroundColor: Colors.deepPurple,
  209.                     shape: RoundedRectangleBorder(
  210.                         borderRadius: BorderRadius.circular(4)),
  211.                   ),
  212.                   onPressed: postMessage,
  213.                   child: const Padding(
  214.                     padding: EdgeInsets.all(10.0),
  215.                     child: Text(
  216.                       'Post',
  217.                       style: TextStyle(
  218.                           fontSize: 16,
  219.                           fontWeight: FontWeight.bold,
  220.                           color: Color.fromARGB(255, 248, 247, 247)),
  221.                     ),
  222.                   ),
  223.                 ),
  224.               ),
  225.               _gap(),
  226.             ],
  227.           ),
  228.         ),
  229.       ),
  230.     );
  231.   }
  232.  
  233.  
  234.  
  235.   Widget _gap() => const SizedBox(height: 16);
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement