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';
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _AddMealScreen extends State<AddMealScreen> {
|
|
|
|
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(() {
|
|
|
|
nameController.text = "Meal ${widget.diary.meals.length}";
|
|
|
|
});
|
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();
|
|
|
|
}
|
|
|
|
|
2023-08-26 22:56:43 +02:00
|
|
|
void popMeDaddy() {
|
2023-07-30 14:40:45 +02:00
|
|
|
Navigator.pop(
|
|
|
|
context,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-28 14:45:32 +02:00
|
|
|
void showError(String message) {
|
2023-07-30 14:40:45 +02:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(message, textAlign: TextAlign.center),
|
|
|
|
backgroundColor: Theme.of(context).colorScheme.error,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _addMeal() async {
|
|
|
|
await widget.apiClient.addMeal(
|
|
|
|
name: nameController.text,
|
|
|
|
diaryId: widget.diary.id,
|
|
|
|
);
|
2023-08-26 22:56:43 +02:00
|
|
|
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(
|
|
|
|
appBar: AppBar(
|
|
|
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
2023-10-27 13:48:45 +02:00
|
|
|
title: Text("🅵🅾🅾🅳🅴🆁", style: logoStyle(context)),
|
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>[
|
|
|
|
TextFormField(
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
labelText: 'Meal name',
|
|
|
|
),
|
|
|
|
controller: nameController,
|
|
|
|
onChanged: (_) => setState(() {
|
|
|
|
nameChanged = true;
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
TextFormField(
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
hintText: 'Search presets',
|
|
|
|
),
|
|
|
|
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
|
|
|
),
|
2023-07-30 14:40:45 +02:00
|
|
|
floatingActionButton: FloatingActionButton(
|
2023-10-27 17:10:09 +02:00
|
|
|
onPressed: _addMealFromPreset,
|
2023-07-30 14:40:45 +02:00
|
|
|
child: const Icon(Icons.add),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|