Make shit pretty

This commit is contained in:
Marco 2024-09-05 16:36:35 +02:00
parent 131f39c1c8
commit 1db4e5e351
5 changed files with 96 additions and 81 deletions

View File

@ -1,5 +1,3 @@
import 'dart:developer';
import 'package:calodiary/storage/storage.dart'; import 'package:calodiary/storage/storage.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:calodiary/food_entry_bloc.dart'; import 'package:calodiary/food_entry_bloc.dart';

View File

@ -53,9 +53,9 @@ class MainApp extends StatelessWidget {
], ],
child: BlocBuilder<ThemeDataBloc, ThemeState>( child: BlocBuilder<ThemeDataBloc, ThemeState>(
builder: (context, state) { builder: (context, state) {
var switchToTheme = ThemeData.light(); var newBrightness = Brightness.light;
if (state.brightness == 'dark') { if (state.brightness == 'dark') {
switchToTheme = ThemeData.dark(); newBrightness = Brightness.dark;
} }
return MaterialApp.router( return MaterialApp.router(
@ -67,7 +67,12 @@ class MainApp extends StatelessWidget {
supportedLocales: const [ supportedLocales: const [
Locale('de'), Locale('de'),
], ],
theme: switchToTheme, theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.lightBlue,
brightness: newBrightness,
),
),
routerConfig: router, routerConfig: router,
); );
}, },

View File

@ -21,7 +21,7 @@ class PerDateWidget extends StatefulWidget {
class _PerDateWidgetState extends State<PerDateWidget> { class _PerDateWidgetState extends State<PerDateWidget> {
late FoodStorage storage; late FoodStorage storage;
late Future<List<FoodEntry>> entriesFuture; late Future<List<FoodEntry>> entriesFuture;
late List<FoodEntry> entries; late List<FoodEntry> entries = [];
@override @override
void initState() { void initState() {
@ -37,84 +37,90 @@ class _PerDateWidgetState extends State<PerDateWidget> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var formattedDate = DateFormat.yMMMMd('de').format(widget.date); var formattedDate = DateFormat.yMMMMd('de').format(widget.date);
return Scaffold( return FutureBuilder(
appBar: AppBar( future: entriesFuture,
title: Text(formattedDate), builder: (context, snapshot) {
actions: [ if (snapshot.connectionState != ConnectionState.done) {
BlocBuilder<ThemeDataBloc, ThemeState>(builder: (context, state) { return const Center(child: CircularProgressIndicator());
var icon = const Icon(Icons.light_mode); } else {
if (state.brightness == 'light') { return BlocProvider(
icon = const Icon(Icons.dark_mode); create: (context) => FoodEntryBloc(
} initialState: FoodEntryState(foodEntries: entries),
return IconButton( storage: storage,
icon: icon, forDate: widget.date),
onPressed: () { child: BlocBuilder<FoodEntryBloc, FoodEntryState>(
context.read<ThemeDataBloc>().add(ThemeToggleEvent()); builder: (context, state) {
}, return Scaffold(
); appBar: AppBar(
}), title: Text(formattedDate),
], actions: [
), BlocBuilder<ThemeDataBloc, ThemeState>(
drawer: const AppDrawer(), builder: (context, state) {
body: FutureBuilder( var icon = const Icon(Icons.light_mode);
future: entriesFuture, if (state.brightness == 'light') {
builder: (context, snapshot) { icon = const Icon(Icons.dark_mode);
if (snapshot.connectionState != ConnectionState.done) { }
return const Center(child: CircularProgressIndicator()); return IconButton(
} else { icon: icon,
return BlocProvider( onPressed: () {
create: (context) => FoodEntryBloc( context
initialState: FoodEntryState(foodEntries: entries), .read<ThemeDataBloc>()
storage: storage, .add(ThemeToggleEvent());
forDate: widget.date), },
child: BlocBuilder<FoodEntryBloc, FoodEntryState>( );
builder: (context, state) { }),
return ListView.builder( ],
itemCount: state.foodEntries.length + 2, ),
itemBuilder: (BuildContext itemBuilderContext, int index) { body: ListView.builder(
if (index == state.foodEntries.length) { itemCount: state.foodEntries.length + 1,
return Padding( itemBuilder:
padding: const EdgeInsets.symmetric(vertical: 10), (BuildContext itemBuilderContext, int listIndex) {
child: SumWidget(foodEntries: state.foodEntries), if (listIndex == state.foodEntries.length) {
); return Column(
} children: [
if (index == state.foodEntries.length + 1) { EnterFoodWidget(
onAdd: (context, entry) {
context
.read<FoodEntryBloc>()
.add(FoodEntryEvent(entry: entry));
},
),
const SizedBox(height: 75),
],
);
}
var entryIndex = listIndex;
return Column( return Column(
children: [ children: [
EnterFoodWidget( FoodEntryWidget(
onAdd: (context, entry) { entry: state.foodEntries[entryIndex],
context onDelete: (callbackContext, id) {
callbackContext
.read<FoodEntryBloc>() .read<FoodEntryBloc>()
.add(FoodEntryEvent(entry: entry)); .add(FoodDeletionEvent(
entryID: id,
));
}, },
), ),
const SizedBox(height: 75), if (listIndex != state.foodEntries.length - 1)
const Divider(),
], ],
); );
} },
),
return Column( bottomNavigationBar: BottomAppBar(
children: [ shape: const CircularNotchedRectangle(),
FoodEntryWidget( color: Theme.of(context).colorScheme.secondary,
entry: state.foodEntries[index], child: SumWidget(foodEntries: state.foodEntries)),
onDelete: (callbackContext, id) { drawer: const AppDrawer(),
callbackContext floatingActionButton:
.read<FoodEntryBloc>() CalendarFloatingButton(date: widget.date),
.add(FoodDeletionEvent( floatingActionButtonLocation:
entryID: id, FloatingActionButtonLocation.endDocked);
)); }),
}, );
), }
const Divider(), });
],
);
},
);
}),
);
}
},
),
floatingActionButton: CalendarFloatingButton(date: widget.date));
} }
} }

View File

@ -8,7 +8,7 @@ import 'package:universal_platform/universal_platform.dart';
class FoodStorage { class FoodStorage {
static late FoodStorage _instance; static late FoodStorage _instance;
late String path; late String path;
late Map<String, double> _foodLookupDatabase = {}; final Map<String, double> _foodLookupDatabase = {};
FoodStorage._create(); FoodStorage._create();

View File

@ -23,10 +23,16 @@ class _SumWidgetState extends State<SumWidget> {
} }
return RowWidget( return RowWidget(
Text(
'kcal heute: ${sum.ceil().toString()}/${state.kcalLimit.ceil()}',
style: Theme.of(context)
.textTheme
.bodyMedium!
.copyWith(color: Theme.of(context).colorScheme.onPrimary),
),
null,
null, null,
null, null,
const Text("kcal heute:"),
Text('${sum.ceil().toString()}/${state.kcalLimit.ceil()}'),
null, null,
); );
}, },