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
This commit is contained in:
parent
4afb3ca319
commit
1a4614f331
@ -1,5 +1,3 @@
|
|||||||
import 'dart:developer';
|
|
||||||
|
|
||||||
import 'package:calorimeter/perdate/perdate_widget.dart';
|
import 'package:calorimeter/perdate/perdate_widget.dart';
|
||||||
import 'package:calorimeter/storage/storage.dart';
|
import 'package:calorimeter/storage/storage.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
@ -34,7 +32,7 @@ class _EnterFoodWidgetState extends State<EnterFoodWidget> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Consumer<ScannedFood>(
|
return Consumer<EnterFoodController>(
|
||||||
builder: (context, food, child) {
|
builder: (context, food, child) {
|
||||||
nameController.text = food.name;
|
nameController.text = food.name;
|
||||||
kcalPerMassController.text = food.kcalPer100g;
|
kcalPerMassController.text = food.kcalPer100g;
|
||||||
@ -72,7 +70,7 @@ class _EnterFoodWidgetState extends State<EnterFoodWidget> {
|
|||||||
double kcalPerMassForSelectedFood =
|
double kcalPerMassForSelectedFood =
|
||||||
suggestions[selectedFood]!;
|
suggestions[selectedFood]!;
|
||||||
context
|
context
|
||||||
.read<ScannedFood>()
|
.read<EnterFoodController>()
|
||||||
.set(selectedFood, kcalPerMassForSelectedFood.toString());
|
.set(selectedFood, kcalPerMassForSelectedFood.toString());
|
||||||
}),
|
}),
|
||||||
TextField(
|
TextField(
|
||||||
@ -140,6 +138,6 @@ class _EnterFoodWidgetState extends State<EnterFoodWidget> {
|
|||||||
kcalPerMass: kcalPerMassAsNumber);
|
kcalPerMass: kcalPerMassAsNumber);
|
||||||
|
|
||||||
widget.onAdd(context, entry);
|
widget.onAdd(context, entry);
|
||||||
context.read<ScannedFood>().set("", "");
|
context.read<EnterFoodController>().set("", "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:developer';
|
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
class FoodFactLookupClient {
|
class FoodFactLookupClient {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
|
import 'package:calorimeter/perdate/perdate_widget.dart';
|
||||||
import 'package:calorimeter/storage/storage.dart';
|
import 'package:calorimeter/storage/storage.dart';
|
||||||
import 'package:calorimeter/utils/router.dart';
|
|
||||||
import 'package:calorimeter/utils/settings_bloc.dart';
|
import 'package:calorimeter/utils/settings_bloc.dart';
|
||||||
import 'package:calorimeter/utils/theme_bloc.dart';
|
import 'package:calorimeter/utils/theme_bloc.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
@ -56,7 +56,8 @@ class MainApp extends StatelessWidget {
|
|||||||
newBrightness = Brightness.dark;
|
newBrightness = Brightness.dark;
|
||||||
}
|
}
|
||||||
|
|
||||||
return MaterialApp.router(
|
return MaterialApp(
|
||||||
|
home: PerDateWidget(date: DateTime.now()),
|
||||||
localizationsDelegates: const [
|
localizationsDelegates: const [
|
||||||
GlobalMaterialLocalizations.delegate,
|
GlobalMaterialLocalizations.delegate,
|
||||||
GlobalWidgetsLocalizations.delegate,
|
GlobalWidgetsLocalizations.delegate,
|
||||||
@ -72,7 +73,6 @@ class MainApp extends StatelessWidget {
|
|||||||
brightness: newBrightness,
|
brightness: newBrightness,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
routerConfig: getRouterConfig(),
|
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import 'dart:developer';
|
import 'dart:developer';
|
||||||
|
|
||||||
import 'package:barcode_scan2/barcode_scan2.dart';
|
import 'package:calorimeter/utils/scan_food_floating_button.dart';
|
||||||
import 'package:calorimeter/food_scan/food_fact_lookup.dart';
|
|
||||||
import 'package:calorimeter/utils/app_drawer.dart';
|
import 'package:calorimeter/utils/app_drawer.dart';
|
||||||
import 'package:calorimeter/food_entry/food_entry_bloc.dart';
|
import 'package:calorimeter/food_entry/food_entry_bloc.dart';
|
||||||
import 'package:calorimeter/perdate/entry_list.dart';
|
import 'package:calorimeter/perdate/entry_list.dart';
|
||||||
@ -27,12 +26,10 @@ class _PerDateWidgetState extends State<PerDateWidget> {
|
|||||||
late FoodStorage storage;
|
late FoodStorage storage;
|
||||||
late Future<List<FoodEntry>> entriesFuture;
|
late Future<List<FoodEntry>> entriesFuture;
|
||||||
List<FoodEntry> entries = [];
|
List<FoodEntry> entries = [];
|
||||||
String formattedDate = '';
|
|
||||||
FoodFactLookupClient client = FoodFactLookupClient();
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
formattedDate = DateFormat.yMMMMd('de').format(widget.date);
|
log("PerDateWidgetState's initState()");
|
||||||
storage = FoodStorage.getInstance();
|
storage = FoodStorage.getInstance();
|
||||||
entriesFuture = storage.getEntriesForDate(widget.date);
|
entriesFuture = storage.getEntriesForDate(widget.date);
|
||||||
entriesFuture.then((val) {
|
entriesFuture.then((val) {
|
||||||
@ -43,6 +40,7 @@ class _PerDateWidgetState extends State<PerDateWidget> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
log("PerDateWidgetState's build()");
|
||||||
return FutureBuilder(
|
return FutureBuilder(
|
||||||
future: entriesFuture,
|
future: entriesFuture,
|
||||||
builder: (context, snapshot) {
|
builder: (context, snapshot) {
|
||||||
@ -50,7 +48,7 @@ class _PerDateWidgetState extends State<PerDateWidget> {
|
|||||||
return const Center(child: CircularProgressIndicator());
|
return const Center(child: CircularProgressIndicator());
|
||||||
} else {
|
} else {
|
||||||
return ChangeNotifierProvider(
|
return ChangeNotifierProvider(
|
||||||
create: (context) => ScannedFood(),
|
create: (context) => EnterFoodController(),
|
||||||
child: BlocProvider(
|
child: BlocProvider(
|
||||||
create: (context) => FoodEntryBloc(
|
create: (context) => FoodEntryBloc(
|
||||||
initialState: FoodEntryState(foodEntries: entries),
|
initialState: FoodEntryState(foodEntries: entries),
|
||||||
@ -60,7 +58,8 @@ class _PerDateWidgetState extends State<PerDateWidget> {
|
|||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: Text(formattedDate),
|
title:
|
||||||
|
Text(DateFormat.yMMMMd('de').format(widget.date)),
|
||||||
actions: const [ThemeSwitcherButton()],
|
actions: const [ThemeSwitcherButton()],
|
||||||
),
|
),
|
||||||
body: FoodEntryList(entries: state.foodEntries),
|
body: FoodEntryList(entries: state.foodEntries),
|
||||||
@ -70,22 +69,20 @@ class _PerDateWidgetState extends State<PerDateWidget> {
|
|||||||
child: SumWidget(foodEntries: state.foodEntries)),
|
child: SumWidget(foodEntries: state.foodEntries)),
|
||||||
drawer: const AppDrawer(),
|
drawer: const AppDrawer(),
|
||||||
floatingActionButton: OverflowBar(children: [
|
floatingActionButton: OverflowBar(children: [
|
||||||
FloatingActionButton(
|
const ScanFoodFloatingButton(),
|
||||||
child: const Icon(Icons.barcode_reader),
|
const SizedBox(width: 8),
|
||||||
onPressed: () async {
|
CalendarFloatingButton(
|
||||||
var scanResult = await BarcodeScanner.scan();
|
startFromDate: widget.date,
|
||||||
var food = await client
|
onDateSelected: (dateSelected) {
|
||||||
.retrieveFoodInfo(scanResult.rawContent);
|
Navigator.of(context).push(
|
||||||
|
MaterialPageRoute(
|
||||||
if (!context.mounted) return;
|
builder: (context) {
|
||||||
|
return PerDateWidget(date: dateSelected);
|
||||||
context
|
},
|
||||||
.read<ScannedFood>()
|
),
|
||||||
.set(food.name, food.kcalPer100g.toString());
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
const SizedBox(width: 8),
|
|
||||||
CalendarFloatingButton(date: widget.date),
|
|
||||||
]),
|
]),
|
||||||
floatingActionButtonLocation:
|
floatingActionButtonLocation:
|
||||||
FloatingActionButtonLocation.endDocked);
|
FloatingActionButtonLocation.endDocked);
|
||||||
@ -97,7 +94,7 @@ class _PerDateWidgetState extends State<PerDateWidget> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ScannedFood extends ChangeNotifier {
|
class EnterFoodController extends ChangeNotifier {
|
||||||
String name = "";
|
String name = "";
|
||||||
String kcalPer100g = "";
|
String kcalPer100g = "";
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
|
import 'package:calorimeter/utils/settings.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
|
||||||
|
|
||||||
class AppDrawer extends StatelessWidget {
|
class AppDrawer extends StatelessWidget {
|
||||||
const AppDrawer({
|
const AppDrawer({
|
||||||
@ -20,7 +20,7 @@ class AppDrawer extends StatelessWidget {
|
|||||||
leading: IconButton(
|
leading: IconButton(
|
||||||
icon: const Icon(Icons.close),
|
icon: const Icon(Icons.close),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
context.pop();
|
Navigator.of(context).pop();
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
title: const Text('Menü')),
|
title: const Text('Menü')),
|
||||||
@ -29,8 +29,9 @@ class AppDrawer extends StatelessWidget {
|
|||||||
title: const Text('Einstellungen'),
|
title: const Text('Einstellungen'),
|
||||||
trailing: const Icon(Icons.settings),
|
trailing: const Icon(Icons.settings),
|
||||||
onTap: () {
|
onTap: () {
|
||||||
context.pop();
|
Navigator.of(context).pop();
|
||||||
context.pushNamed('settings');
|
Navigator.of(context).push(MaterialPageRoute(
|
||||||
|
builder: (context) => const SettingsWidget()));
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
@ -1,27 +1,31 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
|
||||||
|
|
||||||
class CalendarFloatingButton extends StatelessWidget {
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return FloatingActionButton(
|
return FloatingActionButton(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
var router = GoRouter.of(context);
|
var datePicked = await showDatePicker(
|
||||||
var datePicked = await showDatePicker(
|
locale: const Locale('de'),
|
||||||
locale: const Locale('de'),
|
context: context,
|
||||||
context: context,
|
initialDate: startFromDate,
|
||||||
initialDate: date,
|
currentDate: DateTime.now(),
|
||||||
currentDate: DateTime.now(),
|
firstDate: DateTime.now().subtract(const Duration(days: 365 * 10)),
|
||||||
firstDate: DateTime.now().subtract(const Duration(days: 365 * 10)),
|
lastDate: DateTime.now(),
|
||||||
lastDate: DateTime.now(),
|
);
|
||||||
);
|
|
||||||
|
|
||||||
router.pushNamed('perDay', extra: datePicked);
|
if (!context.mounted) return;
|
||||||
},
|
|
||||||
child: const Icon(Icons.today));
|
onDateSelected(datePicked ?? DateTime.now());
|
||||||
|
},
|
||||||
|
heroTag: "calendarFAB",
|
||||||
|
child: const Icon(Icons.today),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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();
|
|
||||||
},
|
|
||||||
)
|
|
||||||
]);
|
|
||||||
}
|
|
31
lib/utils/scan_food_floating_button.dart
Normal file
31
lib/utils/scan_food_floating_button.dart
Normal file
@ -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<EnterFoodController>()
|
||||||
|
.set(food.name, food.kcalPer100g.toString());
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,6 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.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/app_drawer.dart';
|
||||||
import 'package:calorimeter/utils/calendar_floating_button.dart';
|
|
||||||
import 'package:calorimeter/utils/settings_bloc.dart';
|
import 'package:calorimeter/utils/settings_bloc.dart';
|
||||||
import 'package:settings_ui/settings_ui.dart';
|
import 'package:settings_ui/settings_ui.dart';
|
||||||
|
|
||||||
@ -19,48 +17,48 @@ class _SettingsWidgetState extends State<SettingsWidget> {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
body: BlocBuilder<SettingsDataBloc, SettingsState>(
|
body: BlocBuilder<SettingsDataBloc, SettingsState>(
|
||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
return SettingsList(sections: [
|
return SettingsList(sections: [
|
||||||
SettingsSection(
|
SettingsSection(
|
||||||
title: const Text('Deine persönlichen Einstellungen'),
|
title: const Text('Deine persönlichen Einstellungen'),
|
||||||
tiles: [
|
tiles: [
|
||||||
SettingsTile.navigation(
|
SettingsTile.navigation(
|
||||||
leading: const Icon(Icons.food_bank),
|
leading: const Icon(Icons.food_bank),
|
||||||
title: const Text('Kalorienlimit pro Tag'),
|
title: const Text('Kalorienlimit pro Tag'),
|
||||||
value: Text(state.kcalLimit.toString()),
|
value: Text(state.kcalLimit.toString()),
|
||||||
onPressed: (context) async {
|
onPressed: (context) async {
|
||||||
await showDialog(
|
await showDialog(
|
||||||
builder: (ctx) {
|
builder: (ctx) {
|
||||||
return AlertDialog(
|
return AlertDialog(
|
||||||
title: const Text("Kalorienlimit pro Tag"),
|
title: const Text("Kalorienlimit pro Tag"),
|
||||||
content: TextField(controller: kcalPerDayCtrl),
|
content: TextField(controller: kcalPerDayCtrl),
|
||||||
actions: [
|
actions: [
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
double setting;
|
double setting;
|
||||||
try {
|
try {
|
||||||
setting =
|
setting =
|
||||||
double.parse(kcalPerDayCtrl.text);
|
double.parse(kcalPerDayCtrl.text);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
setting = 2000.0;
|
setting = 2000.0;
|
||||||
}
|
}
|
||||||
context.read<SettingsDataBloc>().add(
|
context.read<SettingsDataBloc>().add(
|
||||||
DailyKcalLimitUpdated(kcal: setting));
|
DailyKcalLimitUpdated(kcal: setting));
|
||||||
ctx.pop();
|
Navigator.of(context).pop();
|
||||||
},
|
},
|
||||||
child: const Text('Ok'))
|
child: const Text('Ok'))
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
context: context);
|
context: context);
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
]);
|
]);
|
||||||
}),
|
}),
|
||||||
drawer: const AppDrawer(),
|
drawer: const AppDrawer(),
|
||||||
appBar: AppBar(title: const Text('Einstellungen')),
|
appBar: AppBar(title: const Text('Einstellungen')),
|
||||||
floatingActionButton: CalendarFloatingButton(date: DateTime.now()));
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
21
pubspec.lock
21
pubspec.lock
@ -120,19 +120,6 @@ packages:
|
|||||||
description: flutter
|
description: flutter
|
||||||
source: sdk
|
source: sdk
|
||||||
version: "0.0.0"
|
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:
|
intl:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
@ -173,14 +160,6 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "4.0.0"
|
version: "4.0.0"
|
||||||
logging:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: logging
|
|
||||||
sha256: "623a88c9594aa774443aa3eb2d41807a48486b5613e67599fb4c41c0ad47c340"
|
|
||||||
url: "https://pub.dev"
|
|
||||||
source: hosted
|
|
||||||
version: "1.2.0"
|
|
||||||
matcher:
|
matcher:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -12,7 +12,6 @@ dependencies:
|
|||||||
flutter_bloc: ^8.1.6
|
flutter_bloc: ^8.1.6
|
||||||
flutter_localizations:
|
flutter_localizations:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
go_router: ^14.2.7
|
|
||||||
intl: any
|
intl: any
|
||||||
path_provider: ^2.1.4
|
path_provider: ^2.1.4
|
||||||
settings_ui: ^2.0.2
|
settings_ui: ^2.0.2
|
||||||
|
Loading…
Reference in New Issue
Block a user