Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'package:firebase_database/firebase_database.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_crud/model/note.dart';
- import 'package:flutter_crud/ui/note_screen.dart';
- class ListViewNote extends StatefulWidget {
- @override
- _ListViewNoteState createState() => _ListViewNoteState();
- }
- final notesReference = FirebaseDatabase.instance.reference().child('notes');
- class _ListViewNoteState extends State<ListViewNote> {
- List<Note> items;
- StreamSubscription<Event> _onNoteAddedSubscription;
- StreamSubscription<Event> _onNoteChangedSubscription;
- @override
- void initState() {
- // TODO: implement initState
- super.initState();
- items = new List();
- _onNoteAddedSubscription = notesReference.onChildAdded.listen(_onNoteAdded);
- _onNoteChangedSubscription =
- notesReference.onChildChanged.listen(_onNoteUpdated);
- }
- @override
- void dispose() {
- _onNoteAddedSubscription.cancel();
- _onNoteChangedSubscription.cancel();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Firebase CRUD',
- home: Scaffold(
- appBar: AppBar(
- title: Text('Firebase CRUD'),
- // centerTitle: true,
- backgroundColor: Colors.blue,
- ),
- body: Center(
- child: ListView.builder(
- itemCount: items.length,
- padding: EdgeInsets.all(15.0),
- itemBuilder: (context, position) {
- return Column(
- children: <Widget>[
- // Divider(height: 5.0),
- ListTile(
- title: Text(
- '${items[position].title}',
- style: TextStyle(
- fontSize: 22.0,
- color: Colors.deepOrangeAccent,
- ),
- ),
- subtitle: Text(
- '${items[position].description}',
- style: TextStyle(
- fontSize: 18.0,
- fontStyle: FontStyle.italic,
- ),
- ),
- leading: Column(
- children: <Widget>[
- Padding(padding: EdgeInsets.all(10.0)),
- CircleAvatar(
- backgroundColor: Colors.blueAccent,
- radius: 15.0,
- child: Text(
- '${position + 1}',
- style: TextStyle(
- fontSize: 22.0,
- color: Colors.white,
- ),
- ),
- ),
- IconButton(
- icon: Icon(Icons.remove_circle_outline),
- onPressed: () =>
- _deleteNote(context, items[position], position),
- ),
- ],
- ),
- onTap: () => _navigateToNote(context, items[position]),
- ),
- Divider(height: 5.0),
- ],
- );
- },
- ),
- ),
- floatingActionButton:
- FloatingActionButton(
- child: Icon(Icons.add),
- onPressed: () => _createNewNote(context),
- ),
- ),
- );
- }
- void _onNoteAdded(Event event) {
- setState(() {
- items.add(new Note.fromSnapshot(event.snapshot));
- });
- }
- void _onNoteUpdated(Event event) {
- var oldNoteValue = items.singleWhere((note) => note.id == event.snapshot.key);
- setState(() {
- items[items.indexOf(oldNoteValue)] = new Note.fromSnapshot(event.snapshot);
- });
- }
- void _deleteNote(BuildContext context, Note note, int position) async {
- await notesReference.child(note.id).remove().then((_){
- setState(() {
- items.removeAt(position);
- });
- });
- }
- void _navigateToNote(BuildContext context, Note note) async {
- await Navigator.push(
- context,
- MaterialPageRoute(builder: (context) => NoteScreen(note)),
- );
- }
- void _createNewNote(BuildContext context) async {
- await Navigator.push(
- context,
- MaterialPageRoute(builder: (context) => NoteScreen(Note(null, '', ''))),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement