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:
@@ -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(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user