Refactor code

This commit is contained in:
Marco 2024-09-06 13:48:56 +02:00
parent 6552756702
commit 45f44099a1
15 changed files with 193 additions and 163 deletions

View File

@ -1,6 +1,6 @@
import 'package:calodiary/storage/storage.dart';
import 'package:flutter/material.dart';
import 'package:calodiary/food_entry_bloc.dart';
import 'package:calodiary/food_entry/food_entry_bloc.dart';
import 'package:calodiary/row_with_spacers_widget.dart';
class EnterFoodWidget extends StatefulWidget {

View File

@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:calodiary/food_entry_bloc.dart';
import 'package:calodiary/food_entry/food_entry_bloc.dart';
import 'package:calodiary/row_with_spacers_widget.dart';
class FoodEntryWidget extends StatefulWidget {
@ -66,7 +66,7 @@ class _FoodEntryWidgetState extends State<FoodEntryWidget> {
children: [
SizedBox(
child: IconButton(
padding: EdgeInsets.all(0.0),
padding: const EdgeInsets.all(0.0),
icon: const Icon(Icons.cancel),
onPressed: showCancelAndDelete
? () => setState(() {
@ -77,7 +77,7 @@ class _FoodEntryWidgetState extends State<FoodEntryWidget> {
),
SizedBox(
child: IconButton(
padding: EdgeInsets.all(0.0),
padding: const EdgeInsets.all(0.0),
iconSize: 24,
icon: const Icon(Icons.delete),
color: Colors.redAccent,

View File

@ -1,8 +1,9 @@
import 'package:calodiary/perdate_widget.dart';
import 'package:calodiary/settings.dart';
import 'package:calodiary/settings_bloc.dart';
import 'package:calodiary/storage/storage.dart';
import 'package:calodiary/theme_bloc.dart';
import 'package:calodiary/perdate/perdate_widget.dart';
import 'package:calodiary/utils/router.dart';
import 'package:calodiary/utils/settings.dart';
import 'package:calodiary/utils/settings_bloc.dart';
import 'package:calodiary/utils/theme_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
@ -74,7 +75,7 @@ class MainApp extends StatelessWidget {
brightness: newBrightness,
),
),
routerConfig: router,
routerConfig: getRouterConfig(),
);
},
),
@ -82,26 +83,3 @@ class MainApp extends StatelessWidget {
);
}
}
final router = GoRouter(initialLocation: '/day', routes: [
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;
}
return PerDateWidget(date: date);
}),
GoRoute(
path: '/settings',
name: 'settings',
builder: (context, state) {
return const SettingsWidget();
},
)
]);

View File

@ -0,0 +1,53 @@
import 'package:calodiary/enter_food_widget.dart';
import 'package:calodiary/food_entry/food_entry_bloc.dart';
import 'package:calodiary/food_entry/food_entry_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class FoodEntryList extends StatelessWidget {
final List<FoodEntry> entries;
const FoodEntryList({
required this.entries,
super.key,
});
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: entries.length + 1,
itemBuilder: (BuildContext itemBuilderContext, int listIndex) {
if (listIndex == entries.length) {
return Column(
children: [
EnterFoodWidget(
onAdd: (context, entry) {
context
.read<FoodEntryBloc>()
.add(FoodEntryEvent(entry: entry));
},
),
const SizedBox(height: 75),
],
);
}
var entryIndex = listIndex;
return Column(
children: [
FoodEntryWidget(
key: ValueKey(entries[entryIndex].id),
entry: entries[entryIndex],
onDelete: (callbackContext, id) {
callbackContext.read<FoodEntryBloc>().add(FoodDeletionEvent(
entryID: id,
));
},
),
const Divider(),
],
);
},
);
}
}

View File

@ -0,0 +1,72 @@
import 'package:calodiary/app_drawer.dart';
import 'package:calodiary/utils/calendar_floating_button.dart';
import 'package:calodiary/perdate/entry_list.dart';
import 'package:calodiary/food_entry/food_entry_bloc.dart';
import 'package:calodiary/storage/storage.dart';
import 'package:calodiary/utils/sum_widget.dart';
import 'package:calodiary/utils/theme_switcher_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
class PerDateWidget extends StatefulWidget {
final DateTime date;
const PerDateWidget({super.key, required this.date});
@override
State<PerDateWidget> createState() => _PerDateWidgetState();
}
class _PerDateWidgetState extends State<PerDateWidget> {
late FoodStorage storage;
late Future<List<FoodEntry>> entriesFuture;
List<FoodEntry> entries = [];
String formattedDate = '';
@override
void initState() {
formattedDate = DateFormat.yMMMMd('de').format(widget.date);
storage = FoodStorage.getInstance();
entriesFuture = storage.getEntriesForDate(widget.date);
entriesFuture.then((val) {
entries = val;
});
super.initState();
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: entriesFuture,
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return const Center(child: CircularProgressIndicator());
} else {
return BlocProvider(
create: (context) => FoodEntryBloc(
initialState: FoodEntryState(foodEntries: entries),
storage: storage,
forDate: widget.date),
child: BlocBuilder<FoodEntryBloc, FoodEntryState>(
builder: (context, state) {
return Scaffold(
appBar: AppBar(
title: Text(formattedDate),
actions: const [ThemeSwitcherButton()],
),
body: FoodEntryList(entries: state.foodEntries),
bottomNavigationBar: BottomAppBar(
shape: const CircularNotchedRectangle(),
color: Theme.of(context).colorScheme.secondary,
child: SumWidget(foodEntries: state.foodEntries)),
drawer: const AppDrawer(),
floatingActionButton:
CalendarFloatingButton(date: widget.date),
floatingActionButtonLocation:
FloatingActionButtonLocation.endDocked);
}),
);
}
});
}
}

View File

@ -1,126 +0,0 @@
import 'package:calodiary/theme_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
import 'package:calodiary/app_drawer.dart';
import 'package:calodiary/calendar_floating_button.dart';
import 'package:calodiary/enter_food_widget.dart';
import 'package:calodiary/food_entry_bloc.dart';
import 'package:calodiary/food_entry_widget.dart';
import 'package:calodiary/storage/storage.dart';
import 'package:calodiary/sum_widget.dart';
class PerDateWidget extends StatefulWidget {
final DateTime date;
const PerDateWidget({super.key, required this.date});
@override
State<PerDateWidget> createState() => _PerDateWidgetState();
}
class _PerDateWidgetState extends State<PerDateWidget> {
late FoodStorage storage;
late Future<List<FoodEntry>> entriesFuture;
late List<FoodEntry> entries = [];
@override
void initState() {
super.initState();
storage = FoodStorage.getInstance();
entriesFuture = storage.getEntriesForDate(widget.date);
entriesFuture.then((val) {
entries = val;
});
}
@override
Widget build(BuildContext context) {
var formattedDate = DateFormat.yMMMMd('de').format(widget.date);
return FutureBuilder(
future: entriesFuture,
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return const Center(child: CircularProgressIndicator());
} else {
return BlocProvider(
create: (context) => FoodEntryBloc(
initialState: FoodEntryState(foodEntries: entries),
storage: storage,
forDate: widget.date),
child: BlocBuilder<FoodEntryBloc, FoodEntryState>(
builder: (context, state) {
return Scaffold(
appBar: AppBar(
title: Text(formattedDate),
actions: [
BlocBuilder<ThemeDataBloc, ThemeState>(
builder: (context, state) {
var icon = const Icon(Icons.light_mode);
if (state.brightness == 'light') {
icon = const Icon(Icons.dark_mode);
}
return IconButton(
icon: icon,
onPressed: () {
context
.read<ThemeDataBloc>()
.add(ThemeToggleEvent());
},
);
}),
],
),
body: ListView.builder(
itemCount: state.foodEntries.length + 1,
itemBuilder:
(BuildContext itemBuilderContext, int listIndex) {
if (listIndex == state.foodEntries.length) {
return Column(
children: [
EnterFoodWidget(
onAdd: (context, entry) {
context
.read<FoodEntryBloc>()
.add(FoodEntryEvent(entry: entry));
},
),
const SizedBox(height: 75),
],
);
}
var entryIndex = listIndex;
return Column(
children: [
FoodEntryWidget(
key: ValueKey(state.foodEntries[entryIndex].id),
entry: state.foodEntries[entryIndex],
onDelete: (callbackContext, id) {
callbackContext
.read<FoodEntryBloc>()
.add(FoodDeletionEvent(
entryID: id,
));
},
),
const Divider(),
],
);
},
),
bottomNavigationBar: BottomAppBar(
shape: const CircularNotchedRectangle(),
color: Theme.of(context).colorScheme.secondary,
child: SumWidget(foodEntries: state.foodEntries)),
drawer: const AppDrawer(),
floatingActionButton:
CalendarFloatingButton(date: widget.date),
floatingActionButtonLocation:
FloatingActionButtonLocation.endDocked);
}),
);
}
});
}
}

View File

@ -1,7 +1,7 @@
import 'dart:developer';
import 'dart:io';
import 'package:calodiary/food_entry_bloc.dart';
import 'package:calodiary/food_entry/food_entry_bloc.dart';
import 'package:path_provider/path_provider.dart';
import 'package:universal_platform/universal_platform.dart';

28
lib/utils/router.dart Normal file
View File

@ -0,0 +1,28 @@
import 'package:calodiary/perdate/perdate_widget.dart';
import 'package:calodiary/utils/settings.dart';
import 'package:go_router/go_router.dart';
GoRouter getRouterConfig() {
return GoRouter(initialLocation: '/day', routes: [
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;
}
return PerDateWidget(date: date);
}),
GoRoute(
path: '/settings',
name: 'settings',
builder: (context, state) {
return const SettingsWidget();
},
)
]);
}

View File

@ -2,8 +2,8 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:calodiary/app_drawer.dart';
import 'package:calodiary/calendar_floating_button.dart';
import 'package:calodiary/settings_bloc.dart';
import 'package:calodiary/utils/calendar_floating_button.dart';
import 'package:calodiary/utils/settings_bloc.dart';
import 'package:settings_ui/settings_ui.dart';
class SettingsWidget extends StatefulWidget {

View File

@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:calodiary/food_entry_bloc.dart';
import 'package:calodiary/settings_bloc.dart';
import 'package:calodiary/food_entry/food_entry_bloc.dart';
import 'package:calodiary/utils/settings_bloc.dart';
import 'package:calodiary/row_with_spacers_widget.dart';
class SumWidget extends StatefulWidget {

View File

@ -0,0 +1,25 @@
import 'package:calodiary/utils/theme_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class ThemeSwitcherButton extends StatelessWidget {
const ThemeSwitcherButton({
super.key,
});
@override
Widget build(BuildContext context) {
return BlocBuilder<ThemeDataBloc, ThemeState>(builder: (context, state) {
var icon = const Icon(Icons.light_mode);
if (state.brightness == 'light') {
icon = const Icon(Icons.dark_mode);
}
return IconButton(
icon: icon,
onPressed: () {
context.read<ThemeDataBloc>().add(ThemeToggleEvent());
},
);
});
}
}