calorimeter/lib/main.dart

98 lines
2.6 KiB
Dart
Raw Normal View History

2024-06-12 12:42:29 +00:00
import 'package:calodiary/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';
2024-06-09 12:42:17 +00:00
import 'package:go_router/go_router.dart';
import 'package:calodiary/settings_bloc.dart';
import 'package:calodiary/perdate_widget.dart';
import 'package:calodiary/settings.dart';
import 'package:calodiary/storage/storage.dart';
2024-06-09 17:06:10 +00:00
void main() async {
2024-06-09 18:11:54 +00:00
WidgetsFlutterBinding.ensureInitialized();
2024-06-12 12:42:29 +00:00
var storage = await AppStorage.create();
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 {
2024-06-12 12:42:29 +00:00
final AppStorage 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) {
2024-06-12 12:42:29 +00:00
return 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,
);
},
),
);
}
}
2024-06-09 12:42:17 +00:00
final router = GoRouter(initialLocation: '/day', routes: [
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
}
return PerDateWidget(date: 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
]);