Advertisement
rachmadi

listview_note.dart

Oct 4th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.46 KB | None | 0 0
  1. import 'dart:async';
  2. import 'package:firebase_database/firebase_database.dart';
  3.  
  4. import 'package:flutter/material.dart';
  5.  
  6. import 'package:flutter_crud/model/note.dart';
  7. import 'package:flutter_crud/ui/note_screen.dart';
  8.  
  9. class ListViewNote extends StatefulWidget {
  10.   @override
  11.   _ListViewNoteState createState() => _ListViewNoteState();
  12. }
  13.  
  14. final notesReference = FirebaseDatabase.instance.reference().child('notes');
  15.  
  16. class _ListViewNoteState extends State<ListViewNote> {
  17.   List<Note> items;
  18.   StreamSubscription<Event> _onNoteAddedSubscription;
  19.   StreamSubscription<Event> _onNoteChangedSubscription;
  20.  
  21.   @override
  22.   void initState() {
  23.     // TODO: implement initState
  24.     super.initState();
  25.  
  26.     items = new List();
  27.  
  28.     _onNoteAddedSubscription = notesReference.onChildAdded.listen(_onNoteAdded);
  29.     _onNoteChangedSubscription =
  30.         notesReference.onChildChanged.listen(_onNoteUpdated);
  31.   }
  32.  
  33.   @override
  34.   void dispose() {
  35.     _onNoteAddedSubscription.cancel();
  36.     _onNoteChangedSubscription.cancel();
  37.     super.dispose();
  38.   }
  39.  
  40.   @override
  41.   Widget build(BuildContext context) {
  42.     return MaterialApp(
  43.       title: 'Firebase CRUD',
  44.       home: Scaffold(
  45.         appBar: AppBar(
  46.           title: Text('Firebase CRUD'),
  47. //          centerTitle: true,
  48.           backgroundColor: Colors.blue,
  49.         ),
  50.         body: Center(
  51.           child: ListView.builder(
  52.             itemCount: items.length,
  53.             padding: EdgeInsets.all(15.0),
  54.             itemBuilder: (context, position) {
  55.               return Column(
  56.                 children: <Widget>[
  57. //                  Divider(height: 5.0),
  58.                   ListTile(
  59.                     title: Text(
  60.                       '${items[position].title}',
  61.                       style: TextStyle(
  62.                         fontSize: 22.0,
  63.                         color: Colors.deepOrangeAccent,
  64.                       ),
  65.                     ),
  66.                     subtitle: Text(
  67.                       '${items[position].description}',
  68.                       style: TextStyle(
  69.                         fontSize: 18.0,
  70.                         fontStyle: FontStyle.italic,
  71.                       ),
  72.                     ),
  73.                     leading: Column(
  74.                       children: <Widget>[
  75.                         Padding(padding: EdgeInsets.all(10.0)),
  76.                         CircleAvatar(
  77.                           backgroundColor: Colors.blueAccent,
  78.                           radius: 15.0,
  79.                           child: Text(
  80.                             '${position + 1}',
  81.                             style: TextStyle(
  82.                               fontSize: 22.0,
  83.                               color: Colors.white,
  84.                             ),
  85.                           ),
  86.                         ),
  87.                         IconButton(
  88.                           icon: Icon(Icons.remove_circle_outline),
  89.                           onPressed: () =>
  90.                               _deleteNote(context, items[position], position),
  91.                         ),
  92.                       ],
  93.                     ),
  94.                     onTap: () => _navigateToNote(context, items[position]),
  95.                   ),
  96.                   Divider(height: 5.0),
  97.                 ],
  98.               );
  99.             },
  100.           ),
  101.         ),
  102.         floatingActionButton:
  103.             FloatingActionButton(
  104.                 child: Icon(Icons.add),
  105.                 onPressed: () => _createNewNote(context),
  106.             ),
  107.       ),
  108.     );
  109.   }
  110.  
  111.   void _onNoteAdded(Event event) {
  112.     setState(() {
  113.       items.add(new Note.fromSnapshot(event.snapshot));
  114.     });
  115.   }
  116.  
  117.   void _onNoteUpdated(Event event) {
  118.     var oldNoteValue = items.singleWhere((note) => note.id == event.snapshot.key);
  119.     setState(() {
  120.       items[items.indexOf(oldNoteValue)] = new Note.fromSnapshot(event.snapshot);
  121.     });
  122.   }
  123.  
  124.   void _deleteNote(BuildContext context, Note note, int position) async {
  125.     await notesReference.child(note.id).remove().then((_){
  126.       setState(() {
  127.         items.removeAt(position);
  128.       });
  129.     });
  130.   }
  131.  
  132.   void _navigateToNote(BuildContext context, Note note) async {
  133.     await Navigator.push(
  134.         context,
  135.         MaterialPageRoute(builder: (context) => NoteScreen(note)),
  136.     );
  137.   }
  138.  
  139.   void _createNewNote(BuildContext context) async {
  140.     await Navigator.push(
  141.         context,
  142.         MaterialPageRoute(builder: (context) => NoteScreen(Note(null, '', ''))),
  143.     );
  144.   }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement