From 1a4614f33164c1d1f8fded5f2ddae668bebdbb81 Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 7 Sep 2024 01:38:03 +0200 Subject: [PATCH] Remove go_router because I could not figure out why initState() was called on my widget, thus clearing state when I did not expect it --- lib/food_entry/enter_food_widget.dart | 8 +-- lib/food_scan/food_fact_lookup.dart | 1 - lib/main.dart | 6 +- lib/perdate/perdate_widget.dart | 41 +++++------ lib/utils/app_drawer.dart | 9 +-- lib/utils/calendar_floating_button.dart | 36 +++++----- lib/utils/router.dart | 28 -------- lib/utils/scan_food_floating_button.dart | 31 +++++++++ lib/utils/settings.dart | 88 ++++++++++++------------ pubspec.lock | 21 ------ pubspec.yaml | 1 - 11 files changed, 124 insertions(+), 146 deletions(-) delete mode 100644 lib/utils/router.dart create mode 100644 lib/utils/scan_food_floating_button.dart diff --git a/lib/food_entry/enter_food_widget.dart b/lib/food_entry/enter_food_widget.dart index 6227609..031707c 100644 --- a/lib/food_entry/enter_food_widget.dart +++ b/lib/food_entry/enter_food_widget.dart @@ -1,5 +1,3 @@ -import 'dart:developer'; - import 'package:calorimeter/perdate/perdate_widget.dart'; import 'package:calorimeter/storage/storage.dart'; import 'package:flutter/material.dart'; @@ -34,7 +32,7 @@ class _EnterFoodWidgetState extends State { @override Widget build(BuildContext context) { - return Consumer( + return Consumer( builder: (context, food, child) { nameController.text = food.name; kcalPerMassController.text = food.kcalPer100g; @@ -72,7 +70,7 @@ class _EnterFoodWidgetState extends State { double kcalPerMassForSelectedFood = suggestions[selectedFood]!; context - .read() + .read() .set(selectedFood, kcalPerMassForSelectedFood.toString()); }), TextField( @@ -140,6 +138,6 @@ class _EnterFoodWidgetState extends State { kcalPerMass: kcalPerMassAsNumber); widget.onAdd(context, entry); - context.read().set("", ""); + context.read().set("", ""); } } diff --git a/lib/food_scan/food_fact_lookup.dart b/lib/food_scan/food_fact_lookup.dart index 8369a55..5c60253 100644 --- a/lib/food_scan/food_fact_lookup.dart +++ b/lib/food_scan/food_fact_lookup.dart @@ -1,5 +1,4 @@ import 'dart:convert'; -import 'dart:developer'; import 'dart:io'; class FoodFactLookupClient { diff --git a/lib/main.dart b/lib/main.dart index 9624e18..fe25176 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,5 @@ +import 'package:calorimeter/perdate/perdate_widget.dart'; import 'package:calorimeter/storage/storage.dart'; -import 'package:calorimeter/utils/router.dart'; import 'package:calorimeter/utils/settings_bloc.dart'; import 'package:calorimeter/utils/theme_bloc.dart'; import 'package:flutter/material.dart'; @@ -56,7 +56,8 @@ class MainApp extends StatelessWidget { newBrightness = Brightness.dark; } - return MaterialApp.router( + return MaterialApp( + home: PerDateWidget(date: DateTime.now()), localizationsDelegates: const [ GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, @@ -72,7 +73,6 @@ class MainApp extends StatelessWidget { brightness: newBrightness, ), ), - routerConfig: getRouterConfig(), ); }, ), diff --git a/lib/perdate/perdate_widget.dart b/lib/perdate/perdate_widget.dart index 7e6cb69..7028578 100644 --- a/lib/perdate/perdate_widget.dart +++ b/lib/perdate/perdate_widget.dart @@ -1,7 +1,6 @@ import 'dart:developer'; -import 'package:barcode_scan2/barcode_scan2.dart'; -import 'package:calorimeter/food_scan/food_fact_lookup.dart'; +import 'package:calorimeter/utils/scan_food_floating_button.dart'; import 'package:calorimeter/utils/app_drawer.dart'; import 'package:calorimeter/food_entry/food_entry_bloc.dart'; import 'package:calorimeter/perdate/entry_list.dart'; @@ -27,12 +26,10 @@ class _PerDateWidgetState extends State { late FoodStorage storage; late Future> entriesFuture; List entries = []; - String formattedDate = ''; - FoodFactLookupClient client = FoodFactLookupClient(); @override void initState() { - formattedDate = DateFormat.yMMMMd('de').format(widget.date); + log("PerDateWidgetState's initState()"); storage = FoodStorage.getInstance(); entriesFuture = storage.getEntriesForDate(widget.date); entriesFuture.then((val) { @@ -43,6 +40,7 @@ class _PerDateWidgetState extends State { @override Widget build(BuildContext context) { + log("PerDateWidgetState's build()"); return FutureBuilder( future: entriesFuture, builder: (context, snapshot) { @@ -50,7 +48,7 @@ class _PerDateWidgetState extends State { return const Center(child: CircularProgressIndicator()); } else { return ChangeNotifierProvider( - create: (context) => ScannedFood(), + create: (context) => EnterFoodController(), child: BlocProvider( create: (context) => FoodEntryBloc( initialState: FoodEntryState(foodEntries: entries), @@ -60,7 +58,8 @@ class _PerDateWidgetState extends State { builder: (context, state) { return Scaffold( appBar: AppBar( - title: Text(formattedDate), + title: + Text(DateFormat.yMMMMd('de').format(widget.date)), actions: const [ThemeSwitcherButton()], ), body: FoodEntryList(entries: state.foodEntries), @@ -70,22 +69,20 @@ class _PerDateWidgetState extends State { child: SumWidget(foodEntries: state.foodEntries)), drawer: const AppDrawer(), floatingActionButton: OverflowBar(children: [ - FloatingActionButton( - child: const Icon(Icons.barcode_reader), - onPressed: () async { - var scanResult = await BarcodeScanner.scan(); - var food = await client - .retrieveFoodInfo(scanResult.rawContent); - - if (!context.mounted) return; - - context - .read() - .set(food.name, food.kcalPer100g.toString()); + const ScanFoodFloatingButton(), + const SizedBox(width: 8), + CalendarFloatingButton( + startFromDate: widget.date, + onDateSelected: (dateSelected) { + Navigator.of(context).push( + MaterialPageRoute( + builder: (context) { + return PerDateWidget(date: dateSelected); + }, + ), + ); }, ), - const SizedBox(width: 8), - CalendarFloatingButton(date: widget.date), ]), floatingActionButtonLocation: FloatingActionButtonLocation.endDocked); @@ -97,7 +94,7 @@ class _PerDateWidgetState extends State { } } -class ScannedFood extends ChangeNotifier { +class EnterFoodController extends ChangeNotifier { String name = ""; String kcalPer100g = ""; diff --git a/lib/utils/app_drawer.dart b/lib/utils/app_drawer.dart index eb9aedd..786d786 100644 --- a/lib/utils/app_drawer.dart +++ b/lib/utils/app_drawer.dart @@ -1,5 +1,5 @@ +import 'package:calorimeter/utils/settings.dart'; import 'package:flutter/material.dart'; -import 'package:go_router/go_router.dart'; class AppDrawer extends StatelessWidget { const AppDrawer({ @@ -20,7 +20,7 @@ class AppDrawer extends StatelessWidget { leading: IconButton( icon: const Icon(Icons.close), onPressed: () { - context.pop(); + Navigator.of(context).pop(); }, ), title: const Text('Menü')), @@ -29,8 +29,9 @@ class AppDrawer extends StatelessWidget { title: const Text('Einstellungen'), trailing: const Icon(Icons.settings), onTap: () { - context.pop(); - context.pushNamed('settings'); + Navigator.of(context).pop(); + Navigator.of(context).push(MaterialPageRoute( + builder: (context) => const SettingsWidget())); }, ), ], diff --git a/lib/utils/calendar_floating_button.dart b/lib/utils/calendar_floating_button.dart index 693e030..161b0bc 100644 --- a/lib/utils/calendar_floating_button.dart +++ b/lib/utils/calendar_floating_button.dart @@ -1,27 +1,31 @@ import 'package:flutter/material.dart'; -import 'package:go_router/go_router.dart'; class CalendarFloatingButton extends StatelessWidget { - final DateTime date; + final DateTime startFromDate; + final Function(DateTime) onDateSelected; - const CalendarFloatingButton({super.key, required this.date}); + const CalendarFloatingButton( + {super.key, required this.startFromDate, required this.onDateSelected}); @override Widget build(BuildContext context) { return FloatingActionButton( - onPressed: () async { - var router = GoRouter.of(context); - var datePicked = await showDatePicker( - locale: const Locale('de'), - context: context, - initialDate: date, - currentDate: DateTime.now(), - firstDate: DateTime.now().subtract(const Duration(days: 365 * 10)), - lastDate: DateTime.now(), - ); + onPressed: () async { + var datePicked = await showDatePicker( + locale: const Locale('de'), + context: context, + initialDate: startFromDate, + currentDate: DateTime.now(), + firstDate: DateTime.now().subtract(const Duration(days: 365 * 10)), + lastDate: DateTime.now(), + ); - router.pushNamed('perDay', extra: datePicked); - }, - child: const Icon(Icons.today)); + if (!context.mounted) return; + + onDateSelected(datePicked ?? DateTime.now()); + }, + heroTag: "calendarFAB", + child: const Icon(Icons.today), + ); } } diff --git a/lib/utils/router.dart b/lib/utils/router.dart deleted file mode 100644 index 7dde46a..0000000 --- a/lib/utils/router.dart +++ /dev/null @@ -1,28 +0,0 @@ -import 'package:calorimeter/perdate/perdate_widget.dart'; -import 'package:calorimeter/utils/settings.dart'; -import 'package:go_router/go_router.dart'; - -GoRouter getRouterConfig() { - return GoRouter(initialLocation: '/day', routes: [ - GoRoute( - path: '/day', - name: 'perDay', - builder: (context, state) { - DateTime date; - if (state.extra == null || state.extra is! DateTime) { - date = DateTime.now(); - } else { - date = state.extra as DateTime; - } - - return PerDateWidget(date: date); - }), - GoRoute( - path: '/settings', - name: 'settings', - builder: (context, state) { - return const SettingsWidget(); - }, - ) - ]); -} diff --git a/lib/utils/scan_food_floating_button.dart b/lib/utils/scan_food_floating_button.dart new file mode 100644 index 0000000..b0443fa --- /dev/null +++ b/lib/utils/scan_food_floating_button.dart @@ -0,0 +1,31 @@ +import 'package:barcode_scan2/barcode_scan2.dart'; +import 'package:calorimeter/food_scan/food_fact_lookup.dart'; +import 'package:calorimeter/perdate/perdate_widget.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; + +class ScanFoodFloatingButton extends StatelessWidget { + const ScanFoodFloatingButton({ + super.key, + }); + + @override + Widget build(BuildContext context) { + return FloatingActionButton( + heroTag: "scanFoodFAB", + child: const Icon(Icons.barcode_reader), + onPressed: () async { + var client = FoodFactLookupClient(); + + var scanResult = await BarcodeScanner.scan(); + var food = await client.retrieveFoodInfo(scanResult.rawContent); + + if (!context.mounted) return; + + context + .read() + .set(food.name, food.kcalPer100g.toString()); + }, + ); + } +} diff --git a/lib/utils/settings.dart b/lib/utils/settings.dart index 4409f6b..bc2a1c9 100644 --- a/lib/utils/settings.dart +++ b/lib/utils/settings.dart @@ -1,8 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:go_router/go_router.dart'; import 'package:calorimeter/utils/app_drawer.dart'; -import 'package:calorimeter/utils/calendar_floating_button.dart'; import 'package:calorimeter/utils/settings_bloc.dart'; import 'package:settings_ui/settings_ui.dart'; @@ -19,48 +17,48 @@ class _SettingsWidgetState extends State { @override Widget build(BuildContext context) { return Scaffold( - body: BlocBuilder( - builder: (context, state) { - return SettingsList(sections: [ - SettingsSection( - title: const Text('Deine persönlichen Einstellungen'), - tiles: [ - SettingsTile.navigation( - leading: const Icon(Icons.food_bank), - title: const Text('Kalorienlimit pro Tag'), - value: Text(state.kcalLimit.toString()), - onPressed: (context) async { - await showDialog( - builder: (ctx) { - return AlertDialog( - title: const Text("Kalorienlimit pro Tag"), - content: TextField(controller: kcalPerDayCtrl), - actions: [ - TextButton( - onPressed: () { - double setting; - try { - setting = - double.parse(kcalPerDayCtrl.text); - } catch (e) { - setting = 2000.0; - } - context.read().add( - DailyKcalLimitUpdated(kcal: setting)); - ctx.pop(); - }, - child: const Text('Ok')) - ], - ); - }, - context: context); - }), - ], - ), - ]); - }), - drawer: const AppDrawer(), - appBar: AppBar(title: const Text('Einstellungen')), - floatingActionButton: CalendarFloatingButton(date: DateTime.now())); + body: BlocBuilder( + builder: (context, state) { + return SettingsList(sections: [ + SettingsSection( + title: const Text('Deine persönlichen Einstellungen'), + tiles: [ + SettingsTile.navigation( + leading: const Icon(Icons.food_bank), + title: const Text('Kalorienlimit pro Tag'), + value: Text(state.kcalLimit.toString()), + onPressed: (context) async { + await showDialog( + builder: (ctx) { + return AlertDialog( + title: const Text("Kalorienlimit pro Tag"), + content: TextField(controller: kcalPerDayCtrl), + actions: [ + TextButton( + onPressed: () { + double setting; + try { + setting = + double.parse(kcalPerDayCtrl.text); + } catch (e) { + setting = 2000.0; + } + context.read().add( + DailyKcalLimitUpdated(kcal: setting)); + Navigator.of(context).pop(); + }, + child: const Text('Ok')) + ], + ); + }, + context: context); + }), + ], + ), + ]); + }), + drawer: const AppDrawer(), + appBar: AppBar(title: const Text('Einstellungen')), + ); } } diff --git a/pubspec.lock b/pubspec.lock index 127bd0c..2bef989 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -120,19 +120,6 @@ packages: description: flutter source: sdk version: "0.0.0" - flutter_web_plugins: - dependency: transitive - description: flutter - source: sdk - version: "0.0.0" - go_router: - dependency: "direct main" - description: - name: go_router - sha256: "2ddb88e9ad56ae15ee144ed10e33886777eb5ca2509a914850a5faa7b52ff459" - url: "https://pub.dev" - source: hosted - version: "14.2.7" intl: dependency: "direct main" description: @@ -173,14 +160,6 @@ packages: url: "https://pub.dev" source: hosted version: "4.0.0" - logging: - dependency: transitive - description: - name: logging - sha256: "623a88c9594aa774443aa3eb2d41807a48486b5613e67599fb4c41c0ad47c340" - url: "https://pub.dev" - source: hosted - version: "1.2.0" matcher: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index a7b1cc7..2b81d62 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,7 +12,6 @@ dependencies: flutter_bloc: ^8.1.6 flutter_localizations: sdk: flutter - go_router: ^14.2.7 intl: any path_provider: ^2.1.4 settings_ui: ^2.0.2