calorimeter/lib/main.dart

125 lines
3.6 KiB
Dart
Raw Normal View History

import 'package:calorimeter/food_entry/food_entry_bloc.dart';
import 'package:calorimeter/perdate/perdate_pageview.dart';
2024-09-06 17:00:25 +00:00
import 'package:calorimeter/storage/storage.dart';
import 'package:calorimeter/utils/settings_bloc.dart';
import 'package:calorimeter/utils/theme_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
2024-06-09 21:25:18 +00:00
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:go_router/go_router.dart';
2024-06-09 17:06:10 +00:00
List<FoodEntryState> entriesForToday = [];
DateTime timeNow = DateTime.now();
2024-06-09 17:06:10 +00:00
void main() async {
2024-06-09 18:11:54 +00:00
WidgetsFlutterBinding.ensureInitialized();
WidgetsFlutterBinding.ensureInitialized();
var storage = await FoodStorage.create();
await storage.buildFoodLookupDatabase();
timeNow = DateTime.now().copyWith(
hour: 0,
isUtc: true,
minute: 0,
second: 0,
millisecond: 0,
microsecond: 0);
entriesForToday = await storage.getEntriesForDate(timeNow);
var kcalLimit = await storage.readLimit();
2024-06-12 12:42:29 +00:00
var brightness = await storage.readBrightness();
2024-06-12 12:42:29 +00:00
runApp(
MainApp(
storage: storage,
kcalLimit: kcalLimit,
brightness: brightness,
),
);
}
class MainApp extends StatelessWidget {
final FoodStorage storage;
final double kcalLimit;
2024-06-12 12:42:29 +00:00
final String brightness;
2024-06-12 12:42:29 +00:00
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) => FoodEntryBloc(
storage: storage,
initialState:
GlobalEntryState(foodEntries: {timeNow: entriesForToday}),
),
),
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) {
2024-09-05 14:36:35 +00:00
var newBrightness = Brightness.light;
if (state.brightness == 'dark') {
2024-09-05 14:36:35 +00:00
newBrightness = Brightness.dark;
}
2024-06-12 12:42:29 +00:00
return MaterialApp.router(
routerConfig: GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) {
return PerDatePageview(
initalDate: DateTime.now().copyWith(
hour: 0,
minute: 0,
second: 0,
millisecond: 0,
microsecond: 0,
),
);
},
),
],
),
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('de'),
],
2024-09-05 14:36:35 +00:00
theme: ThemeData(
dividerTheme: const DividerThemeData(space: 2),
2024-09-05 14:36:35 +00:00
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.lightBlue,
brightness: newBrightness,
),
),
);
},
),
),
);
}
}