fooder-app/lib/screens/main.dart

137 lines
3.5 KiB
Dart
Raw Normal View History

2023-07-29 18:10:10 +02:00
import 'package:flutter/material.dart';
2023-07-30 15:31:36 +02:00
import 'package:fooder/screens/based.dart';
import 'package:fooder/screens/add_entry.dart';
2024-04-04 19:03:41 +02:00
import 'package:fooder/screens/add_meal.dart';
2023-07-30 15:31:36 +02:00
import 'package:fooder/models/diary.dart';
2024-03-29 16:47:25 +01:00
import 'package:fooder/widgets/summary.dart';
2024-03-28 16:25:49 +01:00
import 'package:fooder/widgets/meal.dart';
import 'package:fooder/components/sliver.dart';
2024-03-30 14:07:10 +01:00
import 'package:fooder/components/date_picker.dart';
2024-04-04 19:03:41 +02:00
import 'package:fooder/components/floating_action_button.dart';
2023-07-29 18:10:10 +02:00
2023-07-29 20:01:56 +02:00
class MainScreen extends BasedScreen {
const MainScreen(
{super.key, required super.apiClient, required super.storage});
2023-07-29 18:10:10 +02:00
@override
2023-07-29 20:01:56 +02:00
State<MainScreen> createState() => _MainScreen();
2023-07-29 18:10:10 +02:00
}
2024-03-28 16:25:49 +01:00
class _MainScreen extends BasedState<MainScreen> {
2023-07-29 20:10:59 +02:00
Diary? diary;
2023-07-29 23:54:51 +02:00
DateTime date = DateTime.now();
2023-07-29 20:10:59 +02:00
2023-07-29 20:55:32 +02:00
@override
2023-08-28 14:45:32 +02:00
void initState() {
2023-07-29 20:55:32 +02:00
super.initState();
_asyncInitState().then((value) => null);
}
Future<void> _asyncInitState() async {
var diaryMap = await apiClient.getDiary(date: date);
2024-03-28 16:25:49 +01:00
2023-07-29 20:55:32 +02:00
setState(() {
diary = Diary.fromJson(diaryMap);
2023-07-29 23:54:51 +02:00
date = date;
2023-07-29 20:55:32 +02:00
});
}
2024-03-28 16:25:49 +01:00
Future<void> _pickDate(DateTime date) async {
setState(() {
this.date = date;
});
2023-07-29 23:54:51 +02:00
await _asyncInitState();
}
Future<void> _addEntry() async {
2024-04-04 19:03:41 +02:00
if (diary == null) {
return;
}
2023-07-29 23:54:51 +02:00
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddEntryScreen(
apiClient: apiClient, diary: diary!, storage: storage),
2023-07-29 23:54:51 +02:00
),
).then((_) => _asyncInitState());
}
2024-04-04 19:03:41 +02:00
Future<void> _addMeal(context) async {
if (diary == null) {
return;
}
2024-03-29 16:47:25 +01:00
2024-04-04 19:03:41 +02:00
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddMealScreen(
apiClient: apiClient,
2024-04-04 19:03:41 +02:00
diary: diary!,
storage: storage,
2024-03-29 16:47:25 +01:00
),
2024-03-30 14:07:10 +01:00
),
2024-04-04 19:03:41 +02:00
).then((_) => _asyncInitState());
2024-03-29 16:47:25 +01:00
}
2023-07-29 18:10:10 +02:00
@override
Widget build(BuildContext context) {
2023-07-29 23:54:51 +02:00
Widget content;
2023-07-29 20:10:59 +02:00
if (diary != null) {
2024-03-30 14:07:10 +01:00
content = CustomScrollView(slivers: <Widget>[
SliverPersistentHeader(
delegate: FSliverAppBar(
child: FDatePickerWidget(date: date, onDatePicked: _pickDate)),
pinned: true,
),
SliverList(
delegate: SliverChildListDelegate(
[
SummaryWidget(
diary: diary!,
apiClient: apiClient,
2024-03-30 14:07:10 +01:00
refreshParent: _asyncInitState,
),
for (var (i, meal) in diary!.meals.indexed)
MealWidget(
apiClient: apiClient,
storage: storage,
2024-03-30 14:07:10 +01:00
meal: meal,
2024-03-29 16:47:25 +01:00
refreshParent: _asyncInitState,
2024-03-30 14:07:10 +01:00
initiallyExpanded: i == 0,
2024-04-04 19:03:41 +02:00
showText: showText,
2024-03-29 16:47:25 +01:00
),
2024-04-04 19:03:41 +02:00
const SizedBox(height: 200),
2024-03-30 14:07:10 +01:00
],
2023-07-29 23:54:51 +02:00
),
2024-03-30 14:07:10 +01:00
),
]);
2023-07-29 20:10:59 +02:00
} else {
2024-03-30 14:07:10 +01:00
content = const Center(child: CircularProgressIndicator());
2023-07-29 20:10:59 +02:00
}
2023-07-29 18:10:10 +02:00
return Scaffold(
2024-03-28 16:25:49 +01:00
body: content,
extendBodyBehindAppBar: true,
2024-03-29 16:47:25 +01:00
extendBody: true,
appBar: appBar(),
bottomNavigationBar: navBar(),
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
2024-04-04 19:03:41 +02:00
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FActionButton(
icon: Icons.playlist_add,
onPressed: () => _addMeal(context),
),
const SizedBox(width: 10),
FActionButton(
icon: Icons.library_add, onPressed: _addEntry, tag: "fap2"),
],
),
2023-07-29 18:10:10 +02:00
);
}
}