rachmadi

post_add1.dart

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