calorimeter/lib/main.dart

77 lines
2.1 KiB
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';
import 'package:kalodings/food_entry_bloc.dart';
import 'package:kalodings/perdate_widget.dart';
import 'package:kalodings/settings.dart';
import 'package:kalodings/storage/storage.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
var storage = await FoodStorage.create();
runApp(MainApp(storage: storage));
}
class MainApp extends StatelessWidget {
final FoodStorage storage;
const MainApp({required this.storage, super.key});
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (BuildContext context) {
return FoodEntryBloc(FoodEntryState.init(), storage: storage);
},
child: MaterialApp.router(
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('de'),
],
theme: ThemeData.dark(),
routerConfig: router,
),
);
}
}
final router = GoRouter(routes: [
GoRoute(
path: '/',
name: 'perDayToday',
builder: (context, state) {
context
.read<FoodEntryBloc>()
.add(PageChangedEvent(changedToDate: DateTime.now()));
return PerDateWidget(DateTime.now());
}),
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;
}
context
.read<FoodEntryBloc>()
.add(PageChangedEvent(changedToDate: date));
return PerDateWidget(date);
}),
GoRoute(
path: '/settings',
name: 'settings',
builder: (context, state) {
return const SettingsWidget();
},
)
]);