fooder-app/lib/screens/add_product.dart

202 lines
6.1 KiB
Dart
Raw Normal View History

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';
2023-07-30 15:05:20 +02:00
class AddProductScreen extends BasedScreen {
const AddProductScreen({super.key, required super.apiClient});
@override
State<AddProductScreen> createState() => _AddProductScreen();
}
class _AddProductScreen extends State<AddProductScreen> {
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();
}
void popMeDaddy(Product product) {
2023-07-30 15:05:20 +02:00
Navigator.pop(
context,
product,
);
}
2023-08-28 14:45:32 +02:00
void showError(String message) {
2023-07-30 15:05:20 +02:00
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message, textAlign: TextAlign.center),
backgroundColor: Theme.of(context).colorScheme.error,
),
);
}
2023-10-27 14:09:38 +02: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");
2023-10-27 14:09:38 +02: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);
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(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
2023-10-27 13:48:45 +02:00
title: Text("🅵🅾🅾🅳🅴🆁", style: logoStyle(context)),
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>[
2023-07-30 15:05:20 +02:00
TextFormField(
decoration: const InputDecoration(
labelText: 'Product name',
),
controller: nameController,
),
TextFormField(
decoration: const InputDecoration(
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(() {});
},
),
TextFormField(
decoration: const InputDecoration(
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(() {});
},
),
TextFormField(
decoration: const InputDecoration(
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(() {});
},
),
2023-07-30 20:44:50 +02:00
TextFormField(
decoration: const InputDecoration(
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(() {});
},
),
2023-07-30 15:05:20 +02:00
Text(
2023-07-31 12:49:01 +02:00
"${calculateCalories().toStringAsFixed(2)} kcal",
2023-07-30 15:05:20 +02:00
textAlign: TextAlign.right,
),
2023-08-28 14:45:32 +02:00
])),
2023-07-30 15:05:20 +02:00
),
floatingActionButton: FloatingActionButton(
onPressed: _addProduct,
child: const Icon(Icons.add),
),
);
}
}