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/login.dart';
|
|
|
|
import 'package:fooder/screens/add_entry.dart';
|
|
|
|
import 'package:fooder/models/diary.dart';
|
|
|
|
import 'package:fooder/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
|
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);
|
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),
|
2023-10-27 13:35:31 +02:00
|
|
|
lastDate: DateTime(DateTime.now().year + 1),
|
2023-07-29 23:54:51 +02:00
|
|
|
))!;
|
|
|
|
await _asyncInitState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _logout() async {
|
2024-03-25 14:59:16 +01:00
|
|
|
await widget.apiClient.logout();
|
2023-07-29 23:54:51 +02:00
|
|
|
Navigator.pushReplacement(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => LoginScreen(apiClient: widget.apiClient),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
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),
|
2023-08-28 14:45:32 +02:00
|
|
|
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>[
|
2023-08-02 16:45:47 +02:00
|
|
|
TextButton(
|
2023-10-27 13:48:45 +02:00
|
|
|
child: Text(
|
|
|
|
"🅵🅾🅾🅳🅴🆁",
|
|
|
|
style: logoStyle(context),
|
|
|
|
),
|
2023-08-02 16:45:47 +02:00
|
|
|
onPressed: () {
|
|
|
|
Navigator.pushReplacement(
|
|
|
|
context,
|
2023-08-28 14:45:32 +02:00
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) =>
|
|
|
|
MainScreen(apiClient: widget.apiClient)),
|
2023-08-02 16:45:47 +02:00
|
|
|
).then((_) => _asyncInitState());
|
|
|
|
},
|
|
|
|
),
|
2023-07-29 23:54:51 +02:00
|
|
|
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-10-27 13:48:45 +02:00
|
|
|
title = Text("🅵🅾🅾🅳🅴🆁", style: logoStyle(context));
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|