Advertisement
rachmadi

note_screen.dart

Oct 4th, 2018
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.31 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:firebase_database/firebase_database.dart';
  3.  
  4. import 'package:flutter_crud/model/note.dart';
  5.  
  6. class NoteScreen extends StatefulWidget {
  7.   final Note note;
  8.  
  9.   NoteScreen(this.note);
  10.  
  11.   @override
  12.   _NoteScreenState createState() => _NoteScreenState();
  13. }
  14.  
  15. final noteReference = FirebaseDatabase.instance.reference().child('notes');
  16.  
  17. class _NoteScreenState extends State<NoteScreen> {
  18.   TextEditingController _titleController;
  19.   TextEditingController _descriptionController;
  20.  
  21.   @override
  22.   void initState() {
  23.     // TODO: implement initState
  24.     super.initState();
  25.  
  26.     _titleController = TextEditingController(text: widget.note.title);
  27.     _descriptionController =
  28.         TextEditingController(text: widget.note.description);
  29.   }
  30.  
  31.   @override
  32.   Widget build(BuildContext context) {
  33.     return Scaffold(
  34.       appBar: AppBar(
  35.         title: Text('Note'),
  36.       ),
  37.       body: Container(
  38.         margin: EdgeInsets.all(15.0),
  39.         alignment: Alignment.center,
  40.         child: Column(
  41.           children: <Widget>[
  42.             TextField(
  43.               controller: _titleController,
  44.               decoration: InputDecoration(labelText: 'Title'),
  45.             ),
  46.             Padding(padding: EdgeInsets.all(5.0)),
  47.             TextField(
  48.               controller: _descriptionController,
  49.               decoration: InputDecoration(labelText: 'Description'),
  50.             ),
  51.             Padding(padding: EdgeInsets.all(5.0)),
  52.             RaisedButton(
  53.               child: (widget.note.id != null) ? Text('Update') : Text('Add'),
  54.               onPressed: () {
  55.                 if(widget.note.id != null) {
  56.                   noteReference.child(widget.note.id).set({
  57.                     'title': _titleController,
  58.                     'description': _descriptionController
  59.                   }).then((_) {
  60.                     Navigator.pop(context);
  61.                   });
  62.                 } else {
  63.                   noteReference.push().set({
  64.                     'title': _titleController.text,
  65.                     'description': _descriptionController.text
  66.                   }).then((_) {
  67.                     Navigator.pop(context);
  68.                   });
  69.                 }
  70.               },
  71.             ),
  72.           ],
  73.         ),
  74.       ),
  75.     );
  76.   }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement