fooder-app/lib/screens/main.dart

161 lines
4.4 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-03-29 16:47:25 +01:00
import 'package:fooder/screens/add_meal.dart';
2023-07-30 15:31:36 +02:00
import 'package:fooder/models/diary.dart';
import 'package:fooder/widgets/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';
2024-03-29 16:47:25 +01:00
import 'package:fooder/widgets/macroEntry.dart';
2024-03-28 16:25:49 +01:00
import 'package:fooder/components/sliver.dart';
import 'package:fooder/components/datePicker.dart';
2024-03-29 16:47:25 +01:00
import 'package:blur/blur.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});
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 {
2023-07-29 23:54:51 +02:00
var diaryMap = await widget.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 {
await Navigator.push(
context,
MaterialPageRoute(
2023-08-28 14:45:32 +02:00
builder: (context) =>
AddEntryScreen(apiClient: widget.apiClient, diary: diary!),
2023-07-29 23:54:51 +02:00
),
).then((_) => _asyncInitState());
}
2024-03-29 16:47:25 +01:00
Widget floatingActionButton(BuildContext context) {
var theme = Theme.of(context);
var colorScheme = theme.colorScheme;
return Container(
height: 64,
width: 64,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(32),
boxShadow: [
BoxShadow(
color: colorScheme.primary.withOpacity(0.3),
blurRadius: 5,
offset: const Offset(0, 5),
)
],
),
child: ClipRRect(
borderRadius: BorderRadius.circular(32),
child: Stack(
children: [
Blur(
blur: 10,
blurColor: colorScheme.primary.withOpacity(0.1),
child: Container(
height: 64,
width: 64,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
colorScheme.primary.withOpacity(0.1),
colorScheme.secondary.withOpacity(0.1),
],
),
),
),
),
Container(
height: 64,
width: 64,
child: FloatingActionButton(
elevation: 0,
onPressed: _addEntry,
backgroundColor: Colors.transparent,
child: Icon(
Icons.library_add,
color: colorScheme.onPrimary,
),
),
),
],
),
),
);
}
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-28 16:25:49 +01:00
content = CustomScrollView(
slivers: <Widget>[
SliverPersistentHeader(
delegate: FSliverAppBar(child: FDatePickerWidget(date: date, onDatePicked: _pickDate)),
pinned: true,
),
2024-03-28 16:25:49 +01:00
SliverList(
delegate: SliverChildListDelegate(
[
2024-03-29 16:47:25 +01:00
SummaryWidget(
diary: diary!,
apiClient: widget.apiClient,
refreshParent: _asyncInitState,
),
for (var (i, meal) in diary!.meals.indexed)
2024-03-28 16:25:49 +01:00
MealWidget(
meal: meal,
apiClient: widget.apiClient,
refreshParent: _asyncInitState,
2024-03-29 16:47:25 +01:00
initiallyExpanded: i == 0,
2024-03-28 16:25:49 +01:00
),
],
),
2023-07-29 23:54:51 +02:00
),
2024-03-28 16:25:49 +01:00
]
2023-07-29 23:54:51 +02:00
);
2023-07-29 20:10:59 +02:00
} else {
2024-03-28 16:25:49 +01:00
content = const Center(child: const CircularProgressIndicator());
2023-07-29 20:10:59 +02:00
}
2024-03-29 16:47:25 +01: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,
floatingActionButton: floatingActionButton(context),
2023-07-29 18:10:10 +02:00
);
}
}