fooder-app/lib/screens/main.dart

114 lines
2.9 KiB
Dart
Raw Normal View History

2023-07-29 18:10:10 +02:00
import 'package:flutter/material.dart';
2023-07-29 18:47:13 +02:00
import 'package:fooder_web/screens/based.dart';
2023-07-29 23:54:51 +02:00
import 'package:fooder_web/screens/login.dart';
import 'package:fooder_web/screens/add_entry.dart';
2023-07-29 19:21:02 +02:00
import 'package:fooder_web/models/diary.dart';
import 'package:fooder_web/widgets/diary.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
}
2023-07-29 20:01:56 +02:00
class _MainScreen extends State<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
void initState () {
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);
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
});
}
2023-07-29 23:54:51 +02:00
Future<void> _pickDate() async {
date = (await showDatePicker(
context: context,
initialDate: date,
firstDate: DateTime(2020),
lastDate: DateTime(2025),
))!;
await _asyncInitState();
}
void _logout() async {
widget.apiClient.logout();
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => LoginScreen(apiClient: widget.apiClient),
),
);
}
Future<void> _addEntry() async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddEntryScreen(apiClient: widget.apiClient, diary: diary!),
),
).then((_) => _asyncInitState());
}
2023-07-29 18:10:10 +02:00
@override
Widget build(BuildContext context) {
2023-07-29 23:54:51 +02:00
Widget content;
Widget title;
2023-07-29 20:10:59 +02:00
if (diary != null) {
2023-07-29 20:55:32 +02:00
content = Container(
2023-07-29 23:54:51 +02:00
constraints: const BoxConstraints(maxWidth: 720),
2023-07-29 20:55:32 +02:00
padding: const EdgeInsets.all(10),
child: DiaryWidget(diary: diary!, apiClient: widget.apiClient, refreshParent: _asyncInitState),
2023-07-29 20:55:32 +02:00
);
2023-07-29 23:54:51 +02:00
title = Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text("🅵🅾🅾🅳🅴🆁"),
const Spacer(),
Text(
"${date.year}-${date.month}-${date.day}",
style: const TextStyle(fontSize: 20),
),
IconButton(
icon: const Icon(Icons.calendar_month),
onPressed: _pickDate,
),
const Spacer(),
IconButton(
icon: const Icon(Icons.logout),
onPressed: _logout,
),
],
);
2023-07-29 20:10:59 +02:00
} else {
content = const CircularProgressIndicator();
2023-07-29 23:54:51 +02:00
title = const Text("🅵🅾🅾🅳🅴🆁");
2023-07-29 20:10:59 +02:00
}
2023-07-29 18:10:10 +02:00
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
2023-07-29 23:54:51 +02:00
title: title,
2023-07-29 18:10:10 +02:00
),
body: Center(
2023-07-29 20:10:59 +02:00
child: content,
2023-07-29 18:10:10 +02:00
),
2023-07-29 23:54:51 +02:00
floatingActionButton: FloatingActionButton(
onPressed: _addEntry,
child: const Icon(Icons.add),
),
2023-07-29 18:10:10 +02:00
);
}
}