2023-07-30 15:05:20 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
2023-07-30 15:31:36 +02:00
|
|
|
import 'package:fooder/screens/based.dart';
|
|
|
|
import 'package:fooder/models/product.dart';
|
2024-04-04 19:03:41 +02:00
|
|
|
import 'package:fooder/widgets/product.dart';
|
|
|
|
import 'package:fooder/components/text.dart';
|
|
|
|
import 'package:fooder/components/floating_action_button.dart';
|
2023-07-30 15:05:20 +02:00
|
|
|
|
|
|
|
class AddProductScreen extends BasedScreen {
|
|
|
|
const AddProductScreen({super.key, required super.apiClient});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<AddProductScreen> createState() => _AddProductScreen();
|
|
|
|
}
|
|
|
|
|
2024-04-04 19:03:41 +02:00
|
|
|
class _AddProductScreen extends BasedState<AddProductScreen> {
|
2023-07-30 15:05:20 +02:00
|
|
|
final nameController = TextEditingController();
|
|
|
|
final carbController = TextEditingController();
|
|
|
|
final fatController = TextEditingController();
|
2023-07-30 20:44:50 +02:00
|
|
|
final fiberController = TextEditingController();
|
2023-07-30 15:05:20 +02:00
|
|
|
final proteinController = TextEditingController();
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
nameController.dispose();
|
|
|
|
carbController.dispose();
|
|
|
|
fatController.dispose();
|
2023-07-30 20:44:50 +02:00
|
|
|
fiberController.dispose();
|
2023-07-30 15:05:20 +02:00
|
|
|
proteinController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2023-08-26 22:56:43 +02:00
|
|
|
void popMeDaddy(Product product) {
|
2023-07-30 15:05:20 +02:00
|
|
|
Navigator.pop(
|
|
|
|
context,
|
|
|
|
product,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-03-30 14:07:10 +01:00
|
|
|
Future<double?> _parseDouble(String text, String name,
|
|
|
|
{bool silent = false}) async {
|
2023-07-30 15:05:20 +02:00
|
|
|
try {
|
2023-07-31 12:49:01 +02:00
|
|
|
return double.parse(text.replaceAll(",", "."));
|
2023-07-30 15:05:20 +02:00
|
|
|
} catch (e) {
|
2023-10-27 14:09:38 +02:00
|
|
|
if (!silent) {
|
|
|
|
showError("$name must be a number");
|
|
|
|
}
|
2023-07-31 12:49:01 +02:00
|
|
|
return null;
|
2023-07-30 15:05:20 +02:00
|
|
|
}
|
2023-07-31 12:49:01 +02:00
|
|
|
}
|
2023-07-30 15:05:20 +02:00
|
|
|
|
2023-07-31 12:49:01 +02:00
|
|
|
Future<void> _addProduct() async {
|
|
|
|
var carb = await _parseDouble(carbController.text, "Carbs");
|
|
|
|
var fat = await _parseDouble(fatController.text, "Fat");
|
|
|
|
var protein = await _parseDouble(proteinController.text, "Protein");
|
2024-03-30 14:07:10 +01:00
|
|
|
var fiber =
|
|
|
|
await _parseDouble(fiberController.text, "Fiber", silent: true) ?? 0;
|
2023-07-30 20:44:50 +02:00
|
|
|
|
2023-10-27 14:09:38 +02:00
|
|
|
if (carb == null || fat == null || protein == null) {
|
2023-07-30 15:05:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
var productJson = await widget.apiClient.addProduct(
|
2023-07-31 12:49:01 +02:00
|
|
|
carb: carb,
|
|
|
|
fat: fat,
|
|
|
|
protein: protein,
|
|
|
|
fiber: fiber,
|
2023-07-30 15:05:20 +02:00
|
|
|
name: nameController.text,
|
|
|
|
);
|
|
|
|
var product = Product.fromJson(productJson);
|
2023-08-26 22:56:43 +02:00
|
|
|
popMeDaddy(product);
|
2023-07-30 15:05:20 +02:00
|
|
|
} catch (e) {
|
2023-08-28 14:45:32 +02:00
|
|
|
showError(
|
|
|
|
"Error adding product, make sure there is no product with the same name");
|
2023-07-30 15:05:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double calculateCalories() {
|
|
|
|
double calories = 0;
|
|
|
|
|
2023-07-31 12:49:01 +02:00
|
|
|
var carb = double.tryParse(carbController.text.replaceAll(",", "."));
|
|
|
|
var fat = double.tryParse(fatController.text.replaceAll(",", "."));
|
|
|
|
var protein = double.tryParse(proteinController.text.replaceAll(",", "."));
|
|
|
|
var fiber = double.tryParse(fiberController.text.replaceAll(",", "."));
|
|
|
|
|
|
|
|
if (carb != null) {
|
|
|
|
calories += carb * 4;
|
2023-07-30 15:05:20 +02:00
|
|
|
}
|
|
|
|
|
2023-07-31 12:49:01 +02:00
|
|
|
if (fat != null) {
|
|
|
|
calories += fat * 9;
|
2023-07-30 15:05:20 +02:00
|
|
|
}
|
|
|
|
|
2023-07-31 12:49:01 +02:00
|
|
|
if (protein != null) {
|
|
|
|
calories += protein * 4;
|
2023-07-30 15:05:20 +02:00
|
|
|
}
|
|
|
|
|
2023-07-31 12:49:01 +02:00
|
|
|
if (fiber != null) {
|
2023-08-03 21:01:11 +02:00
|
|
|
calories += fiber * 2;
|
2023-07-30 20:44:50 +02:00
|
|
|
}
|
|
|
|
|
2023-07-30 15:05:20 +02:00
|
|
|
return calories;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2024-04-04 19:03:41 +02:00
|
|
|
appBar: appBar(),
|
2023-07-30 15:05:20 +02:00
|
|
|
body: Center(
|
|
|
|
child: Container(
|
2023-08-28 14:45:32 +02:00
|
|
|
constraints: const BoxConstraints(maxWidth: 720),
|
|
|
|
padding: const EdgeInsets.all(10),
|
|
|
|
child: Column(children: <Widget>[
|
2024-04-04 19:03:41 +02:00
|
|
|
FTextInput(
|
|
|
|
labelText: 'Product name',
|
2023-07-30 15:05:20 +02:00
|
|
|
controller: nameController,
|
2024-04-04 19:03:41 +02:00
|
|
|
onChanged: (String value) {
|
|
|
|
setState(() {});
|
|
|
|
},
|
2023-07-30 15:05:20 +02:00
|
|
|
),
|
2024-04-04 19:03:41 +02:00
|
|
|
FTextInput(
|
|
|
|
labelText: 'Carbs',
|
2023-08-28 14:45:32 +02:00
|
|
|
keyboardType:
|
|
|
|
const TextInputType.numberWithOptions(decimal: true),
|
2023-07-30 15:05:20 +02:00
|
|
|
inputFormatters: <TextInputFormatter>[
|
2023-08-28 14:45:32 +02:00
|
|
|
FilteringTextInputFormatter.allow(
|
|
|
|
RegExp(r'^(\d+)?[\.,]?\d{0,2}')),
|
2023-07-30 15:05:20 +02:00
|
|
|
],
|
|
|
|
controller: carbController,
|
|
|
|
onChanged: (String value) {
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
),
|
2024-04-04 19:03:41 +02:00
|
|
|
FTextInput(
|
|
|
|
labelText: 'Fat',
|
2023-08-28 14:45:32 +02:00
|
|
|
keyboardType:
|
|
|
|
const TextInputType.numberWithOptions(decimal: true),
|
2023-07-30 15:05:20 +02:00
|
|
|
inputFormatters: <TextInputFormatter>[
|
2023-08-28 14:45:32 +02:00
|
|
|
FilteringTextInputFormatter.allow(
|
|
|
|
RegExp(r'^(\d+)?[\.,]?\d{0,2}')),
|
2023-07-30 15:05:20 +02:00
|
|
|
],
|
|
|
|
controller: fatController,
|
|
|
|
onChanged: (String value) {
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
),
|
2024-04-04 19:03:41 +02:00
|
|
|
FTextInput(
|
|
|
|
labelText: 'Protein',
|
2023-08-28 14:45:32 +02:00
|
|
|
keyboardType:
|
|
|
|
const TextInputType.numberWithOptions(decimal: true),
|
2023-07-30 15:05:20 +02:00
|
|
|
inputFormatters: <TextInputFormatter>[
|
2023-08-28 14:45:32 +02:00
|
|
|
FilteringTextInputFormatter.allow(
|
|
|
|
RegExp(r'^(\d+)?[\.,]?\d{0,2}')),
|
2023-07-30 15:05:20 +02:00
|
|
|
],
|
|
|
|
controller: proteinController,
|
|
|
|
onChanged: (String value) {
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
),
|
2024-04-04 19:03:41 +02:00
|
|
|
FTextInput(
|
|
|
|
labelText: 'Fiber',
|
2023-08-28 14:45:32 +02:00
|
|
|
keyboardType:
|
|
|
|
const TextInputType.numberWithOptions(decimal: true),
|
2023-07-30 20:44:50 +02:00
|
|
|
inputFormatters: <TextInputFormatter>[
|
2023-08-28 14:45:32 +02:00
|
|
|
FilteringTextInputFormatter.allow(
|
|
|
|
RegExp(r'^(\d+)?[\.,]?\d{0,2}')),
|
2023-07-30 20:44:50 +02:00
|
|
|
],
|
|
|
|
controller: fiberController,
|
|
|
|
onChanged: (String value) {
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
),
|
2024-04-04 19:03:41 +02:00
|
|
|
ProductWidget(
|
|
|
|
product: Product(
|
|
|
|
id: 0,
|
|
|
|
name: nameController.text,
|
|
|
|
carb: double.tryParse(
|
|
|
|
carbController.text.replaceAll(",", ".")) ??
|
|
|
|
0.0,
|
|
|
|
fat: double.tryParse(
|
|
|
|
fatController.text.replaceAll(",", ".")) ??
|
|
|
|
0.0,
|
|
|
|
protein: double.tryParse(
|
|
|
|
proteinController.text.replaceAll(",", ".")) ??
|
|
|
|
0.0,
|
|
|
|
fiber: double.tryParse(
|
|
|
|
fiberController.text.replaceAll(",", ".")) ??
|
|
|
|
0.0,
|
|
|
|
calories: calculateCalories(),
|
|
|
|
),
|
|
|
|
)
|
2023-08-28 14:45:32 +02:00
|
|
|
])),
|
2023-07-30 15:05:20 +02:00
|
|
|
),
|
2024-04-04 19:03:41 +02:00
|
|
|
floatingActionButton: FActionButton(
|
2023-07-30 15:05:20 +02:00
|
|
|
onPressed: _addProduct,
|
2024-04-04 19:03:41 +02:00
|
|
|
icon: Icons.save,
|
2023-07-30 15:05:20 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|