mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2026-08-25 01:05:36 +00:00
refactor: split app wrapper from focused example code (complex apps)
Convert the final 12 multi-screen apps to the validated pattern: lib/main.dart keeps only the MaterialApp bootstrap (plus firebase init where that is genuine bootstrap), and a new lib/example.dart holds the entry screen and focused example code, importing existing supporting files. firebase_crash keeps its runZonedGuarded bootstrap in main.dart and extracts only CrashApp. Apps: bmi_calculator, covid19_mobile_app, expense_planner, firebase_crash_reporting, firebase_google_authentication, lunch_app, navigation_drawer, news_memes_app, save_data_locally_with_sqlite, todo_list_using_provider, unit_testing, using_firebase_db
This commit is contained in:
128
using_firebase_db/lib/example.dart
Normal file
128
using_firebase_db/lib/example.dart
Normal file
@@ -0,0 +1,128 @@
|
||||
// Example: Store and manage notes in a Firebase Realtime Database.
|
||||
|
||||
import 'dart:async';
|
||||
import 'package:firebase_database/firebase_database.dart';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class Note {
|
||||
late String id;
|
||||
late String content;
|
||||
late String createdOn;
|
||||
|
||||
Note(this.content) {
|
||||
createdOn = DateTime.now().toString();
|
||||
}
|
||||
}
|
||||
|
||||
class Example extends StatefulWidget {
|
||||
const Example({super.key});
|
||||
|
||||
@override
|
||||
_ExampleState createState() => _ExampleState();
|
||||
}
|
||||
|
||||
class _ExampleState extends State<Example> {
|
||||
final notesRef = FirebaseDatabase.instance.ref().child('notes');
|
||||
final inputController = TextEditingController();
|
||||
late StreamSubscription<DatabaseEvent> _noteAddedStream;
|
||||
List<Note> items = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_noteAddedStream =
|
||||
notesRef.orderByChild("created_on").onChildAdded.listen(_onNoteAdded);
|
||||
}
|
||||
|
||||
// Creates a new child under notes in the database
|
||||
void _addNote() {
|
||||
var note = Note(inputController.text);
|
||||
inputController.text = "";
|
||||
if (note.content.isNotEmpty) {
|
||||
notesRef.push().set({
|
||||
'content': note.content,
|
||||
'created_on': note.createdOn,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Fired whenever the database sees a new child under the notes
|
||||
// database reference
|
||||
void _onNoteAdded(DatabaseEvent event) {
|
||||
setState(() {
|
||||
final value = event.snapshot.value as Map;
|
||||
var note = Note(value["content"] as String);
|
||||
note.id = event.snapshot.key!;
|
||||
note.createdOn = value["created_on"] as String;
|
||||
items.add(note);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_noteAddedStream.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
// The note has to be cleared from the database and the local list
|
||||
void _deleteNote(int position) {
|
||||
String id = items[position].id;
|
||||
notesRef.child(id).remove().then((_) {
|
||||
setState(() {
|
||||
items.removeAt(position);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text("Using Firebase DB"),
|
||||
),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
Container(
|
||||
padding: EdgeInsets.all(15.0),
|
||||
child: TextField(
|
||||
style:
|
||||
TextStyle(fontSize: 24.0, height: 2.0, color: Colors.black),
|
||||
decoration: InputDecoration(
|
||||
border: InputBorder.none, hintText: 'Add a note'),
|
||||
controller: inputController,
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: SizedBox(
|
||||
height: 200.0,
|
||||
child: ListView.builder(
|
||||
itemCount: items.length,
|
||||
padding: const EdgeInsets.all(10.0),
|
||||
itemBuilder: (context, position) {
|
||||
return Card(
|
||||
child: ListTile(
|
||||
leading: Icon(Icons.note),
|
||||
title: Text(items[position].content),
|
||||
onLongPress: () {
|
||||
_deleteNote(position);
|
||||
},
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: _addNote,
|
||||
tooltip: 'Add Note',
|
||||
child: Icon(Icons.add),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,8 @@
|
||||
import 'dart:async';
|
||||
import 'package:firebase_database/firebase_database.dart';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class Note {
|
||||
late String id;
|
||||
late String content;
|
||||
late String createdOn;
|
||||
import 'example.dart';
|
||||
|
||||
Note(this.content) {
|
||||
createdOn = DateTime.now().toString();
|
||||
}
|
||||
}
|
||||
|
||||
void main() => runApp(MyApp());
|
||||
void main() => runApp(const MyApp());
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
@@ -25,120 +14,7 @@ class MyApp extends StatelessWidget {
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
home: MyHomePage(title: 'Firebase demo'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyHomePage extends StatefulWidget {
|
||||
const MyHomePage({super.key, required this.title});
|
||||
|
||||
final String title;
|
||||
@override
|
||||
_MyHomePageState createState() => _MyHomePageState();
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
final notesRef = FirebaseDatabase.instance.ref().child('notes');
|
||||
final inputController = TextEditingController();
|
||||
late StreamSubscription<DatabaseEvent> _noteAddedStream;
|
||||
List<Note> items = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_noteAddedStream =
|
||||
notesRef.orderByChild("created_on").onChildAdded.listen(_onNoteAdded);
|
||||
}
|
||||
|
||||
// Creates a new child under notes in the database
|
||||
void _addNote() {
|
||||
var note = Note(inputController.text);
|
||||
inputController.text = "";
|
||||
if (note.content.isNotEmpty) {
|
||||
notesRef.push().set({
|
||||
'content': note.content,
|
||||
'created_on': note.createdOn,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Fired whenever the database sees a new child under the notes
|
||||
// database reference
|
||||
void _onNoteAdded(DatabaseEvent event) {
|
||||
setState(() {
|
||||
final value = event.snapshot.value as Map;
|
||||
var note = Note(value["content"] as String);
|
||||
note.id = event.snapshot.key!;
|
||||
note.createdOn = value["created_on"] as String;
|
||||
items.add(note);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_noteAddedStream.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
// The note has to be cleared from the database and the local list
|
||||
void _deleteNote(int position) {
|
||||
String id = items[position].id;
|
||||
notesRef.child(id).remove().then((_) {
|
||||
setState(() {
|
||||
items.removeAt(position);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text("Using Firebase DB"),
|
||||
),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
Container(
|
||||
padding: EdgeInsets.all(15.0),
|
||||
child: TextField(
|
||||
style:
|
||||
TextStyle(fontSize: 24.0, height: 2.0, color: Colors.black),
|
||||
decoration: InputDecoration(
|
||||
border: InputBorder.none, hintText: 'Add a note'),
|
||||
controller: inputController,
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: SizedBox(
|
||||
height: 200.0,
|
||||
child: ListView.builder(
|
||||
itemCount: items.length,
|
||||
padding: const EdgeInsets.all(10.0),
|
||||
itemBuilder: (context, position) {
|
||||
return Card(
|
||||
child: ListTile(
|
||||
leading: Icon(Icons.note),
|
||||
title: Text(items[position].content),
|
||||
onLongPress: () {
|
||||
_deleteNote(position);
|
||||
},
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: _addNote,
|
||||
tooltip: 'Add Note',
|
||||
child: Icon(Icons.add),
|
||||
),
|
||||
home: const Example(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user