calorimeter/lib/main.dart
Marco 2509c1721c Overhaul ui and remove BackButtonListener
1. Make EnterFoodWidget animated
2. Fix exception when reading quantity for a food.

Introduce first integration test
2025-01-05 17:25:34 +01:00

120 lines
3.7 KiB
Dart

/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2024 Marco Groß <mgross@sw-gross.de> */
import 'package:calorimeter/food_entry/food_entry_bloc.dart';
import 'package:calorimeter/perdate/perdate_pageview_controller.dart';
import 'package:calorimeter/storage/storage.dart';
import 'package:calorimeter/utils/date_time_helper.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';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
var foodStorage = await FoodStorage.create();
await foodStorage.buildFoodLookupDatabase();
runApp(
MainApp(storage: foodStorage),
);
}
class MainApp extends StatefulWidget {
final FoodStorage storage;
const MainApp({super.key, required this.storage});
@override
State<MainApp> createState() => _MainAppState();
}
class _MainAppState extends State<MainApp> {
late DateTime timeNow;
late List<FoodEntryState> entriesForToday;
late double kcalLimit;
late String brightness;
late Future<bool>? initFuture;
Future<bool> asyncInit() async {
timeNow = DateTimeHelper.now();
entriesForToday = await widget.storage.getEntriesForDate(timeNow);
kcalLimit = await widget.storage.readLimit();
brightness = await widget.storage.readBrightness();
return true;
}
@override
void initState() {
super.initState();
initFuture = asyncInit();
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: FutureBuilder<Object>(
future: initFuture,
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return Center(child: CircularProgressIndicator());
}
return MultiBlocProvider(
providers: [
BlocProvider(
create: (context) => FoodEntryBloc(
storage: widget.storage,
initialState: GlobalEntryState(
foodEntries: {timeNow: entriesForToday}),
),
),
BlocProvider(
create: (context) => SettingsDataBloc(
SettingsState(kcalLimit: kcalLimit),
storage: widget.storage),
),
BlocProvider(
create: (context) => ThemeDataBloc(
ThemeState(brightness: brightness),
storage: widget.storage),
),
],
child: BlocBuilder<ThemeDataBloc, ThemeState>(
builder: (context, state) {
var newBrightness = Brightness.light;
if (state.brightness == 'dark') {
newBrightness = Brightness.dark;
}
return MaterialApp(
home: PerDatePageViewController(
initialDate: DateTimeHelper.now()),
localizationsDelegates:
AppLocalizations.localizationsDelegates,
supportedLocales: [
Locale('en'),
...AppLocalizations.supportedLocales,
],
theme: ThemeData(
dividerTheme: const DividerThemeData(space: 2),
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.lightBlue,
brightness: newBrightness,
),
),
);
},
),
);
}),
);
}
}