Flutter-Ernaehrungsapp/lib/pages/shared_components/form_builder.dart

176 lines
6.2 KiB
Dart
Raw Permalink Normal View History

2023-06-19 20:23:27 +02:00
import 'package:flutter/cupertino.dart';
2023-06-03 00:40:40 +02:00
import 'package:flutter/material.dart';
2023-06-25 14:43:07 +02:00
import 'package:flutter_dotenv/flutter_dotenv.dart';
2023-06-03 00:40:40 +02:00
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:hive/hive.dart';
import '../../models/form_builder.dart';
import '../../models/user.dart';
2023-06-25 14:43:07 +02:00
import 'form_builder_text_field.dart';
2023-06-03 00:40:40 +02:00
class FormBuilderComponent extends StatefulWidget {
final bool lockTextFields;
const FormBuilderComponent({Key? key, required this.lockTextFields})
: super(key: key);
@override
State<FormBuilderComponent> createState() => _FormBuilderComponentState();
}
class _FormBuilderComponentState extends State<FormBuilderComponent> {
final formKey = GlobalKey<FormBuilderState>();
final List<FormTextField> listOfTextField = [
FormTextField(
"vorname",
TextInputType.text,
30,
"Der Vorname sollte maximal 30 Zeichen lang sein",
"Der Vorname sollte mindestens 2 Zeichen lang sein",
2,
null,
null,
null,
null),
FormTextField(
"nachname",
TextInputType.text,
30,
"Der Nachname sollte maximal 30 Zeichen lang sein",
"Der Nachname sollte mindestens 2 Zeichen lang sein",
2,
null,
null,
null,
null),
FormTextField(
"gewicht",
TextInputType.number,
null,
null,
null,
null,
200,
10,
"Der Gewicht sollte maximal 200 kg sein",
"Der Gewicht sollte mindestens 10 kg sein"),
FormTextField(
"groesse",
TextInputType.number,
null,
null,
null,
null,
230,
60,
"Die Größe sollte maximal 230cm sein",
"Die Größe sollte mindestens 60cm sein"),
FormTextField(
"alter",
TextInputType.number,
null,
null,
null,
null,
99,
6,
"Das Alter sollte maximal 99 Jahre alt sein",
"Das Alter sollte mindestens 6 Jahre alt sein"),
FormTextField(
"kalorien",
TextInputType.number,
null,
null,
null,
null,
30000,
1000,
"Die Kalorienanzahl sollte mindestens 30000 Kcal sein",
"Die Kalorienanzahl sollte mindestens 1000 Kcal sein")
];
@override
Widget build(BuildContext context) {
return FormBuilder(
key: formKey,
child: Padding(
padding: const EdgeInsets.all(8),
child: SingleChildScrollView(
child: Column(
children: [
ListView.builder(
primary: false,
itemCount: listOfTextField.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return FormBuilderTextFieldComponent(
2023-06-16 08:34:41 +02:00
widget.lockTextFields, listOfTextField[index]);
2023-06-03 00:40:40 +02:00
}),
Container(
margin:
const EdgeInsets.symmetric(vertical: 8, horizontal: 0),
width: double.infinity,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: const Size.fromHeight(40), //
2023-06-19 20:23:27 +02:00
backgroundColor: CupertinoColors.systemGreen,
2023-06-03 00:40:40 +02:00
foregroundColor: const Color(0xFFffffff),
shape: const StadiumBorder(),
),
onPressed: () {
2023-06-25 14:43:07 +02:00
final Box box = Hive.box<User>(dotenv.env['USER_BOX'] ?? 'USER');
2023-06-03 01:06:38 +02:00
2023-06-06 23:45:02 +02:00
if (widget.lockTextFields) {
box.put(
"USER",
User(
formKey.currentState?.fields['vorname']
?.value ??
box.get("USER").vorname.toString(),
formKey.currentState?.fields['nachname']
?.value ??
box.get("USER").nachname.toString(),
2023-06-20 19:36:24 +02:00
int.parse(formKey.currentState?.fields['gewicht']
?.value ??
box.get("USER").gewicht),
int.parse(formKey.currentState?.fields['groesse']
?.value ??
box.get("USER").groesse),
int.parse(formKey.currentState?.fields['alter']?.value ??
box.get("USER").alter),
int.parse(formKey.currentState?.fields['kalorien']
?.value ??
box.get("USER").kalorien)));
2023-06-03 01:06:38 +02:00
2023-06-06 23:45:02 +02:00
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(
duration: Duration(seconds: 2),
2023-06-07 00:01:26 +02:00
content: Text('Die Daten wurden erfolgreich gespeichert'),
2023-06-06 23:45:02 +02:00
));
} else {
box.put(
"USER",
User(
formKey.currentState?.fields['vorname']?.value,
formKey.currentState?.fields['nachname']?.value,
2023-06-16 08:34:41 +02:00
int.parse(formKey.currentState?.fields['gewicht']?.value),
int.parse(formKey.currentState?.fields['groesse']?.value),
int.parse(formKey.currentState?.fields['alter']?.value),
int.parse(formKey.currentState?.fields['kalorien']?.value)
));
2023-06-06 23:45:02 +02:00
}
2023-06-16 11:46:32 +02:00
Future.delayed(
const Duration(seconds: 2),
() => Navigator.of(context)
.pushNamedAndRemoveUntil('/navigation', (Route<dynamic> route)
=> false)
);
2023-06-03 00:40:40 +02:00
},
child: const Text("Eingaben bestätigen"),
)),
],
)),
));
}
}