2024-06-12 12:42:29 +00:00
|
|
|
import 'package:calodiary/theme_bloc.dart';
|
2024-06-09 17:06:10 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2024-06-10 01:06:56 +00:00
|
|
|
import 'package:intl/intl.dart';
|
2024-06-11 17:32:25 +00:00
|
|
|
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';
|
2024-06-09 17:06:10 +00:00
|
|
|
|
2024-06-11 17:05:42 +00:00
|
|
|
class PerDateWidget extends StatefulWidget {
|
2024-06-09 17:06:10 +00:00
|
|
|
final DateTime date;
|
2024-06-11 17:05:42 +00:00
|
|
|
const PerDateWidget({super.key, required this.date});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<PerDateWidget> createState() => _PerDateWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _PerDateWidgetState extends State<PerDateWidget> {
|
2024-06-12 12:42:29 +00:00
|
|
|
late AppStorage storage;
|
2024-06-11 17:05:42 +00:00
|
|
|
late Future<List<FoodEntry>> entriesFuture;
|
|
|
|
late List<FoodEntry> entries;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
2024-06-12 12:42:29 +00:00
|
|
|
storage = AppStorage.getInstance();
|
2024-06-11 17:05:42 +00:00
|
|
|
entriesFuture = storage.getEntriesForDate(widget.date);
|
|
|
|
entriesFuture.then((val) {
|
|
|
|
entries = val;
|
|
|
|
});
|
|
|
|
}
|
2024-06-09 17:06:10 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-06-11 17:05:42 +00:00
|
|
|
var formattedDate = DateFormat.yMMMMd('de').format(widget.date);
|
2024-06-09 17:06:10 +00:00
|
|
|
return Scaffold(
|
2024-06-10 01:06:56 +00:00
|
|
|
appBar: AppBar(
|
|
|
|
title: Text(formattedDate),
|
2024-06-12 12:42:29 +00:00
|
|
|
actions: [
|
2024-06-13 19:49:04 +00:00
|
|
|
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());
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}),
|
2024-06-12 12:42:29 +00:00
|
|
|
],
|
2024-06-10 01:06:56 +00:00
|
|
|
),
|
|
|
|
drawer: const AppDrawer(),
|
2024-06-11 17:05:42 +00:00
|
|
|
body: 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 ListView.builder(
|
|
|
|
itemCount: state.foodEntries.length + 2,
|
|
|
|
itemBuilder: (BuildContext itemBuilderContext, int index) {
|
|
|
|
if (index == state.foodEntries.length) {
|
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 10),
|
|
|
|
child: SumWidget(foodEntries: state.foodEntries),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (index == state.foodEntries.length + 1) {
|
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
EnterFoodWidget(
|
|
|
|
onAdd: (context, entry) {
|
|
|
|
context
|
|
|
|
.read<FoodEntryBloc>()
|
|
|
|
.add(FoodEntryEvent(entry: entry));
|
|
|
|
},
|
|
|
|
),
|
|
|
|
const SizedBox(height: 75),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return FoodEntryWidget(
|
|
|
|
entry: state.foodEntries[index],
|
|
|
|
onDelete: (callbackContext) {
|
|
|
|
callbackContext
|
2024-06-10 01:31:06 +00:00
|
|
|
.read<FoodEntryBloc>()
|
2024-06-11 17:05:42 +00:00
|
|
|
.add(FoodDeletionEvent(
|
|
|
|
entryID: state.foodEntries[index].id,
|
|
|
|
));
|
2024-06-10 01:31:06 +00:00
|
|
|
},
|
2024-06-11 17:05:42 +00:00
|
|
|
);
|
|
|
|
},
|
2024-06-10 01:06:56 +00:00
|
|
|
);
|
2024-06-11 17:05:42 +00:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2024-06-09 17:06:10 +00:00
|
|
|
},
|
2024-06-10 01:06:56 +00:00
|
|
|
),
|
2024-06-11 17:05:42 +00:00
|
|
|
floatingActionButton: CalendarFloatingButton(date: widget.date));
|
2024-06-09 17:06:10 +00:00
|
|
|
}
|
|
|
|
}
|