fooder-app/lib/screens/add_product.dart

210 lines
5.8 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 popMeDady(Product product) {
Navigator.pop(
context,
product,
);
}
void showError(String message)
{
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message, textAlign: TextAlign.center),
backgroundColor: Theme.of(context).colorScheme.error,
),
);
}
Future<void> _addProduct() async {
try {
double.parse(carbController.text);
} catch (e) {
showError("Carbs must be a number");
return;
}
try {
double.parse(fatController.text);
} catch (e) {
showError("Fat must be a number");
return;
}
2023-07-30 20:44:50 +02:00
try {
double.parse(fiberController.text);
} catch (e) {
showError("Fiber must be a number");
return;
}
2023-07-30 15:05:20 +02:00
try {
double.parse(proteinController.text);
} catch (e) {
showError("Protein must be a number");
return;
}
try {
var productJson = await widget.apiClient.addProduct(
carb: double.parse(carbController.text),
fat: double.parse(fatController.text),
protein: double.parse(proteinController.text),
2023-07-30 20:44:50 +02:00
fiber: double.parse(fiberController.text),
2023-07-30 15:05:20 +02:00
name: nameController.text,
);
var product = Product.fromJson(productJson);
popMeDady(product);
} catch (e) {
showError("Error adding product, make sure there is no product with the same name");
return;
}
}
double calculateCalories() {
double calories = 0;
try {
calories += double.parse(carbController.text) * 4;
} catch (e) {
// ignore
}
try {
calories += double.parse(fatController.text) * 9;
} catch (e) {
// ignore
}
try {
calories += double.parse(proteinController.text) * 4;
} catch (e) {
// ignore
}
2023-07-30 20:44:50 +02:00
try {
calories += double.parse(fiberController.text) * 2;
} catch (e) {
// ignore
}
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,
title: const Text("🅵🅾🅾🅳🅴🆁"),
),
body: Center(
child: Container(
constraints: const BoxConstraints(maxWidth: 720),
padding: const EdgeInsets.all(10),
child: Column(
children: <Widget>[
TextFormField(
decoration: const InputDecoration(
labelText: 'Product name',
),
controller: nameController,
),
TextFormField(
decoration: const InputDecoration(
labelText: 'Carbs',
),
keyboardType:const TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')),
],
controller: carbController,
onChanged: (String value) {
setState(() {});
},
),
TextFormField(
decoration: const InputDecoration(
labelText: 'Fat',
),
keyboardType:const TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')),
],
controller: fatController,
onChanged: (String value) {
setState(() {});
},
),
TextFormField(
decoration: const InputDecoration(
labelText: 'Protein',
),
keyboardType:const TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')),
],
controller: proteinController,
onChanged: (String value) {
setState(() {});
},
),
2023-07-30 20:44:50 +02:00
TextFormField(
decoration: const InputDecoration(
labelText: 'Fiber',
),
keyboardType:const TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')),
],
controller: fiberController,
onChanged: (String value) {
setState(() {});
},
),
2023-07-30 15:05:20 +02:00
Text(
"${calculateCalories()} kcal",
textAlign: TextAlign.right,
),
]
)
),
),
floatingActionButton: FloatingActionButton(
onPressed: _addProduct,
child: const Icon(Icons.add),
),
);
}
}