rachmadi

post_add2.dart

Oct 11th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.91 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:firebase_database/firebase_database.dart';
  3. import 'package:flutter_training/home.dart';
  4. import 'dart:async';
  5. import 'dart:io';
  6. import 'package:image_picker/image_picker.dart';
  7. import 'package:firebase_storage/firebase_storage.dart';
  8.  
  9. class PostAdd extends StatefulWidget {
  10.   @override
  11.   _PostAddState createState() => _PostAddState();
  12. }
  13.  
  14. class _PostAddState extends State<PostAdd> {
  15.   String postText;
  16.   final textController = TextEditingController();
  17.   File _imageFile;
  18.   File _image;
  19.  
  20.   @override
  21.   Widget build(BuildContext context) {
  22.     return Scaffold(
  23.         appBar: AppBar(
  24.           title: Text('Post Add'),
  25.           actions: <Widget>[
  26.             FlatButton(
  27.               child: Text('Save'),
  28.               textColor: Colors.white,
  29.               onPressed: _postSave,
  30.             ),
  31.           ],
  32.         ),
  33.         body: ListView(
  34.           padding: EdgeInsets.all(16.0),
  35.           children: <Widget>[
  36.             Container(
  37.               child: Column(
  38.                 children: <Widget>[
  39.                   TextField(
  40.                     decoration: InputDecoration(
  41.                       labelText: 'Write your post...',
  42.                       border: OutlineInputBorder(
  43.                         borderRadius: BorderRadius.circular(5.0),
  44.                       ),
  45.                     ),
  46.                     maxLength: 250,
  47.                     maxLines: 6,
  48.                     controller: textController,
  49.                     //onSubmitted: (value) => postText = value,
  50.                   ),
  51.                   Row(
  52.                     children: <Widget>[
  53.                       IconButton(
  54.                         icon: Icon(
  55.                           Icons.camera_alt,
  56.                           color: Colors.grey,
  57.                         ),
  58.                         onPressed: _openCamera,
  59.                       ),
  60.                       IconButton(
  61.                         icon: Icon(
  62.                           Icons.image,
  63.                           color: Colors.grey,
  64.                         ),
  65.                         onPressed: _openGallery,
  66.                       ),
  67.                     ],
  68.                   ),
  69.                   _imageFile != null ? buildImage() : Text(''),
  70. //                  Image.file(_imageFile),
  71.                 ],
  72.               ),
  73.             ),
  74.           ],
  75.         ));
  76.   }
  77.  
  78.   Image buildImage() {
  79. //    if (_imageFile != null) {
  80.     return Image.file(_imageFile);
  81. //    }
  82.   }
  83.  
  84.   void _postSave() {
  85.     StorageReference storageRef =
  86.         FirebaseStorage.instance.ref().child('myimage.jpg');
  87.     if (_image != null) {
  88.       StorageUploadTask task = storageRef.putFile(_image);
  89. //      if(task.isSuccessful) {
  90.       var downloadUrl = storageRef.getDownloadURL();
  91.       print('downloadUrl: ${downloadUrl.toString()}');
  92.       var postRef = FirebaseDatabase.instance.reference().child('posts');
  93.       try {
  94.         postRef.push().set({
  95.           'content': textController.text,
  96.         });
  97.       } catch (e) {
  98.         print('Error: ${e}');
  99.       }
  100.       textController.clear();
  101.       Navigator.pop(
  102.         context,
  103.         MaterialPageRoute(builder: (context) => HomePage()),
  104.       );
  105. //      } else {
  106. //        print('task is not successful...');
  107. //      }
  108.  
  109.     }
  110.   }
  111.  
  112.   void _openCamera() async {
  113.     print('open camera...');
  114.     try {
  115.       _imageFile = await ImagePicker.pickImage(source: ImageSource.camera);
  116.       print('path: ${_imageFile}');
  117.       setState(() {
  118.         _image = _imageFile;
  119.       });
  120.     } catch (e) {
  121.       print('Error: ${e}');
  122.     }
  123.   }
  124.  
  125.   void _openGallery() async {
  126.     print('open gallery...');
  127.     try {
  128.       _imageFile = await ImagePicker.pickImage(source: ImageSource.gallery);
  129.       print('path: ${_imageFile.path}');
  130.       setState(() {
  131.         _image = _imageFile;
  132.       });
  133.     } catch (e) {
  134.       print('Error: ${e}');
  135.     }
  136.   }
  137. }
Add Comment
Please, Sign In to add comment