calorimeter/lib/main.dart

77 lines
2.1 KiB
Dart
Raw Normal View History

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';
2024-06-09 12:42:17 +00:00
import 'package:go_router/go_router.dart';
import 'package:kalodings/food_entry_bloc.dart';
2024-06-09 17:06:10 +00:00
import 'package:kalodings/perdate_widget.dart';
import 'package:kalodings/settings.dart';
2024-06-09 17:06:10 +00:00
import 'package:kalodings/storage/storage.dart';
void main() async {
2024-06-09 18:11:54 +00:00
WidgetsFlutterBinding.ensureInitialized();
2024-06-09 17:06:10 +00:00
var storage = await FoodStorage.create();
2024-06-09 17:06:10 +00:00
runApp(MainApp(storage: storage));
}
class MainApp extends StatelessWidget {
2024-06-09 17:06:10 +00:00
final FoodStorage storage;
const MainApp({required this.storage, super.key});
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (BuildContext context) {
2024-06-09 17:06:10 +00:00
return FoodEntryBloc(FoodEntryState.init(), storage: storage);
},
2024-06-09 12:42:17 +00:00
child: MaterialApp.router(
2024-06-09 21:25:18 +00:00
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('de'),
],
2024-06-09 12:42:17 +00:00
theme: ThemeData.dark(),
routerConfig: router,
),
);
}
}
2024-06-09 12:42:17 +00:00
final router = GoRouter(routes: [
GoRoute(
path: '/',
name: 'perDayToday',
builder: (context, state) {
2024-06-09 21:25:18 +00:00
context
.read<FoodEntryBloc>()
.add(PageChangedEvent(changedToDate: DateTime.now()));
2024-06-09 17:06:10 +00:00
return PerDateWidget(DateTime.now());
2024-06-09 12:42:17 +00:00
}),
GoRoute(
path: '/day',
name: 'perDay',
builder: (context, state) {
2024-06-09 21:25:18 +00:00
DateTime date;
2024-06-09 12:42:17 +00:00
if (state.extra == null || state.extra is! DateTime) {
2024-06-09 21:25:18 +00:00
date = DateTime.now();
2024-06-09 12:42:17 +00:00
} else {
2024-06-09 21:25:18 +00:00
date = state.extra as DateTime;
2024-06-09 12:42:17 +00:00
}
2024-06-09 21:25:18 +00:00
context
.read<FoodEntryBloc>()
.add(PageChangedEvent(changedToDate: date));
2024-06-09 12:42:17 +00:00
2024-06-09 21:25:18 +00:00
return PerDateWidget(date);
2024-06-09 12:42:17 +00:00
}),
GoRoute(
path: '/settings',
name: 'settings',
builder: (context, state) {
return const SettingsWidget();
},
)
2024-06-09 12:42:17 +00:00
]);