fooder-app/lib/screens/add_meal.dart

162 lines
4.1 KiB
Dart
Raw Normal View History

2023-07-30 14:40:45 +02:00
import 'package:flutter/material.dart';
2023-07-30 15:31:36 +02:00
import 'package:fooder/screens/based.dart';
import 'package:fooder/models/diary.dart';
2023-10-27 17:10:09 +02:00
import 'package:fooder/models/preset.dart';
import 'package:fooder/widgets/preset.dart';
2024-04-04 19:03:41 +02:00
import 'package:fooder/components/text.dart';
import 'package:fooder/components/floating_action_button.dart';
2023-07-30 14:40:45 +02:00
class AddMealScreen extends BasedScreen {
final Diary diary;
2023-08-28 14:45:32 +02:00
const AddMealScreen(
{super.key, required super.apiClient, required this.diary});
2023-07-30 14:40:45 +02:00
@override
State<AddMealScreen> createState() => _AddMealScreen();
}
2024-04-04 19:03:41 +02:00
class _AddMealScreen extends BasedState<AddMealScreen> {
2023-07-30 14:40:45 +02:00
final nameController = TextEditingController();
2023-10-27 17:10:09 +02:00
final presetNameController = TextEditingController();
bool nameChanged = false;
List<Preset> presets = [];
2023-10-27 17:44:03 +02:00
Preset? selectedPreset;
2023-10-27 17:10:09 +02:00
Future<void> _getPresets() async {
var presetsMap =
await widget.apiClient.getPresets(presetNameController.text);
setState(() {
presets = (presetsMap['presets'] as List<dynamic>)
.map((e) => Preset.fromJson(e as Map<String, dynamic>))
.toList();
2023-10-27 17:44:03 +02:00
selectedPreset = null;
2023-10-27 17:10:09 +02:00
});
}
2023-07-30 14:40:45 +02:00
2023-07-30 14:42:55 +02:00
@override
2023-08-28 14:45:32 +02:00
void initState() {
2023-07-30 14:42:55 +02:00
super.initState();
setState(() {
2024-04-04 19:03:41 +02:00
nameController.text = "Meal ${widget.diary.meals.length + 1}";
2023-07-30 14:42:55 +02:00
});
2023-10-27 17:10:09 +02:00
_getPresets();
2023-07-30 14:42:55 +02:00
}
2023-07-30 14:40:45 +02:00
@override
void dispose() {
nameController.dispose();
super.dispose();
}
void popMeDaddy() {
2023-07-30 14:40:45 +02:00
Navigator.pop(
context,
);
}
Future<void> _addMeal() async {
await widget.apiClient.addMeal(
name: nameController.text,
diaryId: widget.diary.id,
);
popMeDaddy();
2023-07-30 14:40:45 +02:00
}
2023-10-27 17:33:57 +02:00
Future<void> _deletePreset(Preset preset) async {
widget.apiClient.deletePreset(preset.id);
setState(() {
presets.remove(preset);
});
}
Future<void> deletePreset(context, Preset preset) async {
2023-10-27 17:10:09 +02:00
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Confirm deletion of the preset'),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.cancel),
onPressed: () {
Navigator.pop(context);
},
),
IconButton(
icon: const Icon(Icons.delete),
onPressed: () {
2023-10-27 17:33:57 +02:00
_deletePreset(preset);
2023-10-27 17:10:09 +02:00
Navigator.pop(context);
},
),
],
);
},
);
}
Future<void> _addMealFromPreset() async {
2023-10-27 17:44:03 +02:00
if (selectedPreset == null) {
2023-10-27 17:10:09 +02:00
_addMeal();
return;
}
await widget.apiClient.addMealFromPreset(
2023-10-27 17:44:03 +02:00
name: nameChanged ? nameController.text : selectedPreset!.name,
2023-10-27 17:10:09 +02:00
diaryId: widget.diary.id,
2023-10-27 17:44:03 +02:00
presetId: selectedPreset!.id,
2023-10-27 17:10:09 +02:00
);
popMeDaddy();
}
2023-07-30 14:40:45 +02:00
@override
Widget build(BuildContext context) {
return Scaffold(
2024-04-04 19:03:41 +02:00
appBar: appBar(),
2023-07-30 14:40:45 +02:00
body: Center(
2023-10-27 17:10:09 +02:00
child: Container(
constraints: const BoxConstraints(maxWidth: 720),
padding: const EdgeInsets.all(10),
child: ListView(children: <Widget>[
2024-04-04 19:03:41 +02:00
FTextInput(
labelText: 'Meal name',
2023-10-27 17:10:09 +02:00
controller: nameController,
onChanged: (_) => setState(() {
nameChanged = true;
}),
),
2024-04-04 19:03:41 +02:00
FTextInput(
labelText: 'Search presets',
2023-10-27 17:10:09 +02:00
controller: presetNameController,
onChanged: (_) => _getPresets(),
),
for (var preset in presets)
ListTile(
onTap: () {
setState(() {
presets = [preset];
presetNameController.text = preset.name;
2023-10-27 17:44:03 +02:00
selectedPreset = preset;
2023-10-27 17:10:09 +02:00
});
_addMealFromPreset();
},
onLongPress: () {
2023-10-27 17:33:57 +02:00
deletePreset(context, preset);
2023-10-27 17:10:09 +02:00
},
title: PresetWidget(
preset: preset,
),
),
]),
2023-07-30 14:40:45 +02:00
),
2023-10-27 17:10:09 +02:00
),
2024-04-04 19:03:41 +02:00
floatingActionButton: FActionButton(
2023-10-27 17:10:09 +02:00
onPressed: _addMealFromPreset,
2024-04-04 19:03:41 +02:00
icon: Icons.playlist_add_rounded,
2023-07-30 14:40:45 +02:00
),
);
}
}