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/storage/storage.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@ -34,7 +32,7 @@ class _EnterFoodWidgetState extends State<EnterFoodWidget> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Consumer<ScannedFood>(
|
||||
return Consumer<EnterFoodController>(
|
||||
builder: (context, food, child) {
|
||||
nameController.text = food.name;
|
||||
kcalPerMassController.text = food.kcalPer100g;
|
||||
@ -72,7 +70,7 @@ class _EnterFoodWidgetState extends State<EnterFoodWidget> {
|
||||
double kcalPerMassForSelectedFood =
|
||||
suggestions[selectedFood]!;
|
||||
context
|
||||
.read<ScannedFood>()
|
||||
.read<EnterFoodController>()
|
||||
.set(selectedFood, kcalPerMassForSelectedFood.toString());
|
||||
}),
|
||||
TextField(
|
||||
@ -140,6 +138,6 @@ class _EnterFoodWidgetState extends State<EnterFoodWidget> {
|
||||
kcalPerMass: kcalPerMassAsNumber);
|
||||
|
||||
widget.onAdd(context, entry);
|
||||
context.read<ScannedFood>().set("", "");
|
||||
context.read<EnterFoodController>().set("", "");
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:developer';
|
||||
import 'dart:io';
|
||||
|
||||
class FoodFactLookupClient {
|
||||
|
@ -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(),
|
||||
);
|
||||
},
|
||||
),
|
||||
|
@ -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<PerDateWidget> {
|
||||
late FoodStorage storage;
|
||||
late Future<List<FoodEntry>> entriesFuture;
|
||||
List<FoodEntry> 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<PerDateWidget> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
log("PerDateWidgetState's build()");
|
||||
return FutureBuilder(
|
||||
future: entriesFuture,
|
||||
builder: (context, snapshot) {
|
||||
@ -50,7 +48,7 @@ class _PerDateWidgetState extends State<PerDateWidget> {
|
||||
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<PerDateWidget> {
|
||||
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<PerDateWidget> {
|
||||
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<ScannedFood>()
|
||||
.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<PerDateWidget> {
|
||||
}
|
||||
}
|
||||
|
||||
class ScannedFood extends ChangeNotifier {
|
||||
class EnterFoodController extends ChangeNotifier {
|
||||
String name = "";
|
||||
String kcalPer100g = "";
|
||||
|
||||
|
@ -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()));
|
||||
},
|
||||
),
|
||||
],
|
||||
|
@ -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),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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_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<SettingsWidget> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: BlocBuilder<SettingsDataBloc, SettingsState>(
|
||||
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<SettingsDataBloc>().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<SettingsDataBloc, SettingsState>(
|
||||
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<SettingsDataBloc>().add(
|
||||
DailyKcalLimitUpdated(kcal: setting));
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: const Text('Ok'))
|
||||
],
|
||||
);
|
||||
},
|
||||
context: context);
|
||||
}),
|
||||
],
|
||||
),
|
||||
]);
|
||||
}),
|
||||
drawer: const AppDrawer(),
|
||||
appBar: AppBar(title: const Text('Einstellungen')),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
21
pubspec.lock
21
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:
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user