mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2026-08-24 16:50:47 +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:
12
bmi_calculator/lib/example.dart
Normal file
12
bmi_calculator/lib/example.dart
Normal file
@@ -0,0 +1,12 @@
|
||||
import 'package:bmi_calculator/calculator/calculator_page.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
// Example: BMI calculator entry screen.
|
||||
class Example extends StatelessWidget {
|
||||
const Example({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return CalculatorPage(title: 'BMI CALCULATOR');
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
import 'package:bmi_calculator/calculator/calculator_page.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import 'example.dart';
|
||||
|
||||
void main() {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
@@ -20,7 +21,7 @@ class MyApp extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'BMI Calculator',
|
||||
home: CalculatorPage(title: 'BMI CALCULATOR'),
|
||||
home: const Example(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
12
covid19_mobile_app/lib/example.dart
Normal file
12
covid19_mobile_app/lib/example.dart
Normal file
@@ -0,0 +1,12 @@
|
||||
// Example: World Wide COVID-19 cases with daily charts
|
||||
import 'package:flutter/material.dart';
|
||||
import 'screens/home.dart';
|
||||
|
||||
class Example extends StatelessWidget {
|
||||
const Example({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Home();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'screens/home.dart';
|
||||
import 'example.dart';
|
||||
|
||||
void main() => runApp(MyApp());
|
||||
|
||||
@@ -18,7 +18,7 @@ class MyApp extends StatelessWidget {
|
||||
).copyWith(secondary: Color(0xfff4796b)),
|
||||
brightness: Brightness.dark,
|
||||
),
|
||||
home: Home(),
|
||||
home: const Example(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
93
expense_planner/lib/example.dart
Normal file
93
expense_planner/lib/example.dart
Normal file
@@ -0,0 +1,93 @@
|
||||
// Example: Track personal expenses with a chart and a list of transactions.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import './models/transaction.dart';
|
||||
import './widgets/chart.dart';
|
||||
import './widgets/new_transaction.dart';
|
||||
import './widgets/transaction_list.dart';
|
||||
|
||||
class Example extends StatefulWidget {
|
||||
const Example({super.key});
|
||||
|
||||
@override
|
||||
_ExampleState createState() => _ExampleState();
|
||||
}
|
||||
|
||||
class _ExampleState extends State<Example> {
|
||||
final List<Transaction> _userTransactions = [];
|
||||
|
||||
List<Transaction> get _recentTransactions {
|
||||
return _userTransactions.where((tx) {
|
||||
return tx.date.isAfter(
|
||||
DateTime.now().subtract(
|
||||
Duration(days: 7),
|
||||
),
|
||||
);
|
||||
}).toList();
|
||||
}
|
||||
|
||||
void _addNewTransaction(
|
||||
String txTitle, double txAmount, DateTime chosenDate) {
|
||||
final newTx = Transaction(
|
||||
title: txTitle,
|
||||
amount: txAmount,
|
||||
date: chosenDate,
|
||||
id: DateTime.now().toString(),
|
||||
);
|
||||
|
||||
setState(() {
|
||||
_userTransactions.add(newTx);
|
||||
});
|
||||
}
|
||||
|
||||
void _startAddNewTransaction(BuildContext ctx) {
|
||||
showModalBottomSheet(
|
||||
context: ctx,
|
||||
builder: (_) {
|
||||
return GestureDetector(
|
||||
onTap: () {},
|
||||
behavior: HitTestBehavior.opaque,
|
||||
child: NewTransaction(_addNewTransaction),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _deleteTransaction(String id) {
|
||||
setState(() {
|
||||
_userTransactions.removeWhere((tx) => tx.id == id);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(
|
||||
'Personal Expenses',
|
||||
),
|
||||
actions: <Widget>[
|
||||
IconButton(
|
||||
icon: Icon(Icons.add),
|
||||
onPressed: () => _startAddNewTransaction(context),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: <Widget>[
|
||||
Chart(_recentTransactions),
|
||||
TransactionList(_userTransactions, _deleteTransaction),
|
||||
],
|
||||
),
|
||||
),
|
||||
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
|
||||
floatingActionButton: FloatingActionButton(
|
||||
child: Icon(Icons.add),
|
||||
onPressed: () => _startAddNewTransaction(context),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import './widgets/new_transaction.dart';
|
||||
import './widgets/transaction_list.dart';
|
||||
import './widgets/chart.dart';
|
||||
import './models/transaction.dart';
|
||||
import 'example.dart';
|
||||
|
||||
void main() => runApp(MyApp());
|
||||
void main() => runApp(const MyApp());
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
@@ -34,92 +31,7 @@ class MyApp extends StatelessWidget {
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
)),
|
||||
home: MyHomePage(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyHomePage extends StatefulWidget {
|
||||
const MyHomePage({super.key});
|
||||
|
||||
@override
|
||||
_MyHomePageState createState() => _MyHomePageState();
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
final List<Transaction> _userTransactions = [];
|
||||
|
||||
List<Transaction> get _recentTransactions {
|
||||
return _userTransactions.where((tx) {
|
||||
return tx.date.isAfter(
|
||||
DateTime.now().subtract(
|
||||
Duration(days: 7),
|
||||
),
|
||||
);
|
||||
}).toList();
|
||||
}
|
||||
|
||||
void _addNewTransaction(
|
||||
String txTitle, double txAmount, DateTime chosenDate) {
|
||||
final newTx = Transaction(
|
||||
title: txTitle,
|
||||
amount: txAmount,
|
||||
date: chosenDate,
|
||||
id: DateTime.now().toString(),
|
||||
);
|
||||
|
||||
setState(() {
|
||||
_userTransactions.add(newTx);
|
||||
});
|
||||
}
|
||||
|
||||
void _startAddNewTransaction(BuildContext ctx) {
|
||||
showModalBottomSheet(
|
||||
context: ctx,
|
||||
builder: (_) {
|
||||
return GestureDetector(
|
||||
onTap: () {},
|
||||
behavior: HitTestBehavior.opaque,
|
||||
child: NewTransaction(_addNewTransaction),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _deleteTransaction(String id) {
|
||||
setState(() {
|
||||
_userTransactions.removeWhere((tx) => tx.id == id);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(
|
||||
'Personal Expenses',
|
||||
),
|
||||
actions: <Widget>[
|
||||
IconButton(
|
||||
icon: Icon(Icons.add),
|
||||
onPressed: () => _startAddNewTransaction(context),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: <Widget>[
|
||||
Chart(_recentTransactions),
|
||||
TransactionList(_userTransactions, _deleteTransaction),
|
||||
],
|
||||
),
|
||||
),
|
||||
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
|
||||
floatingActionButton: FloatingActionButton(
|
||||
child: Icon(Icons.add),
|
||||
onPressed: () => _startAddNewTransaction(context),
|
||||
),
|
||||
home: const Example(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
31
firebase_crash_reporting/lib/example.dart
Normal file
31
firebase_crash_reporting/lib/example.dart
Normal file
@@ -0,0 +1,31 @@
|
||||
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
// Example: Two buttons to test Crashlytics custom logging and crash reporting.
|
||||
class Example extends StatelessWidget {
|
||||
const Example({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
//custom Crashlytics log message
|
||||
FirebaseCrashlytics.instance.log("It's a bug");
|
||||
},
|
||||
child: Text("Custom Log")),
|
||||
const SizedBox(height: 10),
|
||||
ElevatedButton(
|
||||
child: Text('Crash the app'),
|
||||
onPressed: () {
|
||||
FirebaseCrashlytics.instance.crash();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'example.dart';
|
||||
|
||||
void main() {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
runZonedGuarded(() {
|
||||
@@ -17,12 +19,12 @@ void main() {
|
||||
class App extends StatelessWidget {
|
||||
const App({super.key});
|
||||
|
||||
|
||||
//initialise firebase and crashlytics
|
||||
Future<void> _initializeFirebase() async {
|
||||
await Firebase.initializeApp();
|
||||
await FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(true);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
@@ -40,7 +42,7 @@ class App extends StatelessWidget {
|
||||
}
|
||||
//firebase and crashlytics initialise complete
|
||||
if (snapshot.connectionState == ConnectionState.done) {
|
||||
return CrashApp();
|
||||
return const Example();
|
||||
}
|
||||
return Center(
|
||||
child: Column(
|
||||
@@ -56,31 +58,3 @@ class App extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CrashApp extends StatelessWidget {
|
||||
const CrashApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
//custom Crashlytics log message
|
||||
FirebaseCrashlytics.instance.log("It's a bug");
|
||||
},
|
||||
child: Text("Custom Log")),
|
||||
const SizedBox(height: 10),
|
||||
ElevatedButton(
|
||||
child: Text('Crash the app'),
|
||||
onPressed: () {
|
||||
FirebaseCrashlytics.instance.crash();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
13
firebase_google_authentication/lib/example.dart
Normal file
13
firebase_google_authentication/lib/example.dart
Normal file
@@ -0,0 +1,13 @@
|
||||
// Example: Google sign-in flow with login, signup, and an authenticated info page.
|
||||
|
||||
import 'package:firebase_google_authentication/Screens/HomePage.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class Example extends StatelessWidget {
|
||||
const Example({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const HomePage();
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:firebase_google_authentication/Screens/HomePage.dart';
|
||||
import 'package:firebase_google_authentication/Services/google_auth.dart';
|
||||
import 'package:firebase_google_authentication/example.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
void main() async{
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await Firebase.initializeApp();
|
||||
runApp(MyApp());
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@@ -19,7 +19,7 @@ class MyApp extends StatelessWidget {
|
||||
create: (context) => GoogleSignInProvider(),
|
||||
child: MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: HomePage(),
|
||||
home: const Example(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
12
lunch_app/lib/example.dart
Normal file
12
lunch_app/lib/example.dart
Normal file
@@ -0,0 +1,12 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:lunch_app/views/prototype/home.dart';
|
||||
|
||||
// Example: Lunch app entry screen with category, option, and food listing.
|
||||
class Example extends StatelessWidget {
|
||||
const Example({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const Home();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'views/prototype/home.dart';
|
||||
import 'example.dart';
|
||||
|
||||
void main() => runApp(LunchApp());
|
||||
|
||||
@@ -23,7 +23,7 @@ class LunchApp extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
),
|
||||
home: Home(),
|
||||
home: const Example(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
13
navigation_drawer/lib/example.dart
Normal file
13
navigation_drawer/lib/example.dart
Normal file
@@ -0,0 +1,13 @@
|
||||
// Example: A home screen with a navigation drawer to switch between settings and account screens.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:navigation_drawer/screens/home.dart';
|
||||
|
||||
class Example extends StatelessWidget {
|
||||
const Example({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const HomeScreen();
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,25 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:navigation_drawer/example.dart';
|
||||
import 'package:navigation_drawer/screens/account.dart';
|
||||
import 'package:navigation_drawer/screens/home.dart';
|
||||
import 'package:navigation_drawer/screens/settings.dart';
|
||||
|
||||
void main() {
|
||||
runApp(MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: HomeScreen(), // route for home is '/' implicitly
|
||||
routes: <String, WidgetBuilder>{
|
||||
// define the routes
|
||||
SettingsScreen.routeName: (BuildContext context) => SettingsScreen(),
|
||||
AccountScreen.routeName: (BuildContext context) => AccountScreen(),
|
||||
},
|
||||
));
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: const Example(), // route for home is '/' implicitly
|
||||
routes: <String, WidgetBuilder>{
|
||||
// define the routes
|
||||
SettingsScreen.routeName: (BuildContext context) => SettingsScreen(),
|
||||
AccountScreen.routeName: (BuildContext context) => AccountScreen(),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
12
news_memes_app/lib/example.dart
Normal file
12
news_memes_app/lib/example.dart
Normal file
@@ -0,0 +1,12 @@
|
||||
// Example: Home page with news and memes navigation
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:news_memes_app/Screens/HomePage.dart';
|
||||
|
||||
class Example extends StatelessWidget {
|
||||
const Example({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const HomePage();
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,17 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:news_memes_app/Screens/HomePage.dart';
|
||||
import 'example.dart';
|
||||
|
||||
void main() {
|
||||
runApp(MaterialApp(
|
||||
home: HomePage(),
|
||||
));
|
||||
runApp(MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
home: const Example(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
12
save_data_locally_with_sqlite/lib/example.dart
Normal file
12
save_data_locally_with_sqlite/lib/example.dart
Normal file
@@ -0,0 +1,12 @@
|
||||
// Example: Save data locally using sqlite
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:save_data_locally_with_sqlite/screens/homescreen/homescreen.dart';
|
||||
|
||||
class Example extends StatelessWidget {
|
||||
const Example({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const HomeScreen();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:save_data_locally_with_sqlite/screens/homescreen/homescreen.dart';
|
||||
import 'example.dart';
|
||||
|
||||
void main() {
|
||||
runApp(MyApp());
|
||||
@@ -16,7 +16,7 @@ class MyApp extends StatelessWidget {
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
home: HomeScreen(),
|
||||
home: const Example(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
18
todo_list_using_provider/lib/example.dart
Normal file
18
todo_list_using_provider/lib/example.dart
Normal file
@@ -0,0 +1,18 @@
|
||||
// Example: Todo list using provider
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'notifiers/todo_list.dart';
|
||||
import 'views/home.dart';
|
||||
|
||||
class Example extends StatelessWidget {
|
||||
const Example({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ChangeNotifierProvider<TodoList>(
|
||||
create: (context) => TodoList(),
|
||||
child: const MyHomePage(title: 'Flutter Demo Home Page'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'notifiers/todo_list.dart';
|
||||
import 'views/home.dart';
|
||||
import 'example.dart';
|
||||
|
||||
void main() {
|
||||
runApp(MyApp());
|
||||
@@ -20,10 +17,7 @@ class MyApp extends StatelessWidget {
|
||||
primarySwatch: Colors.blue,
|
||||
visualDensity: VisualDensity.adaptivePlatformDensity,
|
||||
),
|
||||
home: ChangeNotifierProvider<TodoList>(
|
||||
create: (context) => TodoList(),
|
||||
child: MyHomePage(title: 'Flutter Demo Home Page'),
|
||||
),
|
||||
home: const Example(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
12
unit_testing/lib/example.dart
Normal file
12
unit_testing/lib/example.dart
Normal file
@@ -0,0 +1,12 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:unit_testing/screens/sign_in_screen.dart';
|
||||
|
||||
// Example: Sign-in entry screen with email and password validation.
|
||||
class Example extends StatelessWidget {
|
||||
const Example({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const SignInScreen();
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@ import 'package:flutter/material.dart';
|
||||
import 'package:unit_testing/screens/home_screen.dart';
|
||||
import 'package:unit_testing/screens/sign_in_screen.dart';
|
||||
|
||||
import 'example.dart';
|
||||
|
||||
void main() => runApp(MyApp());
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@@ -13,6 +15,7 @@ class MyApp extends StatelessWidget {
|
||||
debugShowCheckedModeBanner: false,
|
||||
title: 'Material App',
|
||||
initialRoute: "/",
|
||||
home: const Example(),
|
||||
routes: {
|
||||
"/": (context) => SignInScreen(),
|
||||
"/homeScreen": (context) => HomeScreen(),
|
||||
|
||||
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