calorimeter/lib/main.dart
Marco b83f547f6b Implement food entry lookup on entering a food name.
Now, an on-the-fly food lookup is created from existing entries on startup. Those entries are used to make suggestions when the user is typing to enter new food entries.
2024-09-04 22:47:37 +02:00

102 lines
2.7 KiB
Dart

import 'package:calodiary/perdate_widget.dart';
import 'package:calodiary/settings.dart';
import 'package:calodiary/settings_bloc.dart';
import 'package:calodiary/storage/storage.dart';
import 'package:calodiary/theme_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:go_router/go_router.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
var storage = await FoodStorage.create();
await storage.buildFoodLookupDatabase();
var kcalLimit = await storage.readLimit();
var brightness = await storage.readBrightness();
runApp(
MainApp(
storage: storage,
kcalLimit: kcalLimit,
brightness: brightness,
),
);
}
class MainApp extends StatelessWidget {
final FoodStorage storage;
final double kcalLimit;
final String brightness;
const MainApp(
{required this.storage,
required this.kcalLimit,
required this.brightness,
super.key});
@override
Widget build(BuildContext context) {
return SafeArea(
child: MultiBlocProvider(
providers: [
BlocProvider(
create: (context) => SettingsDataBloc(
SettingsState(kcalLimit: kcalLimit),
storage: storage),
),
BlocProvider(
create: (context) => ThemeDataBloc(
ThemeState(brightness: brightness),
storage: storage),
),
],
child: BlocBuilder<ThemeDataBloc, ThemeState>(
builder: (context, state) {
var switchToTheme = ThemeData.light();
if (state.brightness == 'dark') {
switchToTheme = ThemeData.dark();
}
return MaterialApp.router(
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('de'),
],
theme: switchToTheme,
routerConfig: router,
);
},
),
),
);
}
}
final router = 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();
},
)
]);