2024-05-29 22:58:26 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-09-06 17:00:25 +00:00
|
|
|
import 'package:calorimeter/food_entry/food_entry_bloc.dart';
|
2024-09-06 16:51:24 +00:00
|
|
|
import 'package:calorimeter/utils/row_with_spacers_widget.dart';
|
2024-05-29 22:58:26 +00:00
|
|
|
|
2024-09-06 00:23:47 +00:00
|
|
|
class FoodEntryWidget extends StatefulWidget {
|
2024-09-09 20:41:48 +00:00
|
|
|
final FoodEntryState entry;
|
2024-09-04 20:47:32 +00:00
|
|
|
final Function(BuildContext context, String id) onDelete;
|
2024-09-24 15:23:01 +00:00
|
|
|
final Function(BuildContext context, FoodEntryState entry) onChange;
|
2024-09-25 15:33:40 +00:00
|
|
|
final Function(BuildContext context, FoodEntryState entry) onTap;
|
2024-06-01 10:41:58 +00:00
|
|
|
|
2024-09-24 15:23:01 +00:00
|
|
|
const FoodEntryWidget({
|
|
|
|
super.key,
|
|
|
|
required this.entry,
|
|
|
|
required this.onDelete,
|
|
|
|
required this.onChange,
|
2024-09-25 15:33:40 +00:00
|
|
|
required this.onTap,
|
2024-09-24 15:23:01 +00:00
|
|
|
});
|
2024-05-29 22:58:26 +00:00
|
|
|
|
2024-09-06 00:23:47 +00:00
|
|
|
@override
|
|
|
|
State<FoodEntryWidget> createState() => _FoodEntryWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _FoodEntryWidgetState extends State<FoodEntryWidget> {
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2024-05-29 22:58:26 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-09-06 00:23:47 +00:00
|
|
|
return GestureDetector(
|
2024-09-25 15:33:40 +00:00
|
|
|
onTap: () => widget.onTap(context, widget.entry),
|
2024-09-06 00:23:47 +00:00
|
|
|
child: Stack(
|
|
|
|
children: [
|
|
|
|
Positioned.fill(
|
|
|
|
child: Stack(children: [
|
|
|
|
Positioned.fill(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 8.0),
|
|
|
|
child: RowWidget(
|
2024-09-09 20:41:48 +00:00
|
|
|
widget.entry.waitingForNetwork
|
|
|
|
? const Center(child: CircularProgressIndicator())
|
|
|
|
: Text(widget.entry.name),
|
|
|
|
widget.entry.waitingForNetwork
|
|
|
|
? Container()
|
|
|
|
: Text(widget.entry.mass.ceil().toString(),
|
|
|
|
textAlign: TextAlign.end),
|
2024-09-06 00:23:47 +00:00
|
|
|
Opacity(
|
2024-09-25 15:33:40 +00:00
|
|
|
opacity: widget.entry.isSelected ? 0.0 : 1.0,
|
2024-09-24 15:23:01 +00:00
|
|
|
child: widget.entry.waitingForNetwork
|
|
|
|
? Container()
|
|
|
|
: Text(widget.entry.kcalPer100.ceil().toString(),
|
|
|
|
textAlign: TextAlign.end),
|
|
|
|
),
|
|
|
|
Opacity(
|
2024-09-25 15:33:40 +00:00
|
|
|
opacity: widget.entry.isSelected ? 0.0 : 1.0,
|
2024-09-24 15:23:01 +00:00
|
|
|
child: widget.entry.waitingForNetwork
|
|
|
|
? Container()
|
|
|
|
: Text(
|
|
|
|
(widget.entry.mass *
|
|
|
|
widget.entry.kcalPer100 /
|
|
|
|
100)
|
|
|
|
.ceil()
|
|
|
|
.toString(),
|
|
|
|
textAlign: TextAlign.end),
|
2024-09-06 00:23:47 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Opacity(
|
2024-09-25 15:33:40 +00:00
|
|
|
opacity: widget.entry.isSelected ? 0.66 : 0.0,
|
2024-09-06 00:23:47 +00:00
|
|
|
child: Container(
|
|
|
|
color: Theme.of(context).colorScheme.secondary)),
|
|
|
|
]),
|
|
|
|
),
|
|
|
|
Opacity(
|
2024-09-25 15:33:40 +00:00
|
|
|
opacity: widget.entry.isSelected ? 1.0 : 0.0,
|
2024-09-06 00:23:47 +00:00
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
children: [
|
2024-09-24 15:23:01 +00:00
|
|
|
SizedBox(
|
|
|
|
child: IconButton(
|
|
|
|
padding: const EdgeInsets.all(0.0),
|
|
|
|
icon: const Icon(Icons.edit),
|
2024-09-25 15:33:40 +00:00
|
|
|
onPressed: widget.entry.isSelected
|
2024-09-24 15:23:01 +00:00
|
|
|
? () async {
|
2024-09-25 15:33:40 +00:00
|
|
|
widget.onTap(context, widget.entry);
|
2024-09-24 15:23:01 +00:00
|
|
|
await showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (dialogContext) {
|
|
|
|
return FoodEntryChangeDialog(
|
|
|
|
entry: widget.entry,
|
|
|
|
onChange: (context, entry) {
|
|
|
|
widget.onChange(context, entry);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
: null),
|
|
|
|
),
|
2024-09-25 16:20:50 +00:00
|
|
|
SizedBox(
|
|
|
|
child: IconButton(
|
|
|
|
padding: const EdgeInsets.all(0.0),
|
|
|
|
iconSize: 24,
|
|
|
|
icon: const Icon(Icons.delete),
|
|
|
|
color: Colors.redAccent,
|
|
|
|
onPressed: widget.entry.isSelected
|
|
|
|
? () => widget.onDelete(context, widget.entry.id)
|
|
|
|
: null),
|
|
|
|
),
|
2024-09-06 00:23:47 +00:00
|
|
|
],
|
|
|
|
),
|
2024-09-04 20:47:32 +00:00
|
|
|
),
|
2024-09-06 00:23:47 +00:00
|
|
|
],
|
2024-06-09 12:42:17 +00:00
|
|
|
),
|
2024-05-29 22:58:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-09-24 15:23:01 +00:00
|
|
|
|
|
|
|
class FoodEntryChangeDialog extends StatefulWidget {
|
|
|
|
final FoodEntryState entry;
|
|
|
|
final Function(BuildContext context, FoodEntryState entry) onChange;
|
|
|
|
|
|
|
|
const FoodEntryChangeDialog(
|
|
|
|
{required this.entry, super.key, required this.onChange});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<FoodEntryChangeDialog> createState() => _FoodEntryChangeDialogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _FoodEntryChangeDialogState extends State<FoodEntryChangeDialog> {
|
|
|
|
late TextEditingController nameController;
|
|
|
|
late TextEditingController massController;
|
|
|
|
late TextEditingController kcalPer100Controller;
|
|
|
|
|
|
|
|
static const textFieldVerticalPadding = 16.0;
|
|
|
|
static const textFieldHorizontalPadding = 16.0;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
nameController = TextEditingController();
|
|
|
|
nameController.text = widget.entry.name;
|
|
|
|
|
|
|
|
massController = TextEditingController();
|
|
|
|
massController.text = widget.entry.mass.toString();
|
|
|
|
|
|
|
|
kcalPer100Controller = TextEditingController();
|
|
|
|
kcalPer100Controller.text = widget.entry.kcalPer100.toString();
|
|
|
|
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return AlertDialog(
|
|
|
|
content: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
vertical: textFieldVerticalPadding,
|
|
|
|
horizontal: textFieldHorizontalPadding),
|
|
|
|
child: TextField(
|
|
|
|
onSubmitted: (val) => _onSubmitAction(),
|
|
|
|
controller: nameController,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
label: Text("Name"),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
vertical: textFieldVerticalPadding,
|
|
|
|
horizontal: textFieldHorizontalPadding),
|
|
|
|
child: TextField(
|
|
|
|
onSubmitted: (val) => _onSubmitAction(),
|
|
|
|
controller: massController,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
label: Text("Menge"),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
vertical: textFieldVerticalPadding,
|
|
|
|
horizontal: textFieldHorizontalPadding),
|
|
|
|
child: TextField(
|
|
|
|
onSubmitted: (val) => _onSubmitAction(),
|
|
|
|
controller: kcalPer100Controller,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
label: Text("kcal pro Menge"),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
actions: [
|
|
|
|
IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
icon: const Icon(Icons.cancel)),
|
|
|
|
IconButton(
|
|
|
|
onPressed: () => _onSubmitAction(),
|
|
|
|
icon: const Icon(Icons.check),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onSubmitAction() {
|
|
|
|
int mass;
|
|
|
|
int kcalPer100;
|
|
|
|
|
|
|
|
try {
|
|
|
|
mass = int.parse(massController.text);
|
|
|
|
} catch (e) {
|
|
|
|
mass = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
kcalPer100 = int.parse(kcalPer100Controller.text);
|
|
|
|
} catch (e) {
|
|
|
|
kcalPer100 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
var newEntry = FoodEntryState.withID(
|
|
|
|
id: widget.entry.id,
|
|
|
|
name: nameController.text,
|
|
|
|
mass: mass,
|
|
|
|
kcalPer100: kcalPer100,
|
|
|
|
waitingForNetwork: widget.entry.waitingForNetwork,
|
2024-09-25 15:33:40 +00:00
|
|
|
isSelected: false,
|
2024-09-24 15:23:01 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
widget.onChange(context, newEntry);
|
|
|
|
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
}
|
|
|
|
}
|