Flutter-Ernaehrungsapp/lib/android/components/form/form_builder.dart

176 lines
6.1 KiB
Dart
Raw Normal View History

2023-06-03 00:40:40 +02:00
import 'package:ernaehrung/android/components/form/form_builder_text_field_component.dart';
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:hive/hive.dart';
import '../../models/form_builder.dart';
import '../../models/user.dart';
import '../../pages/nav_pages/main_page.dart';
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(
true, listOfTextField[index]);
}),
Container(
margin:
const EdgeInsets.symmetric(vertical: 8, horizontal: 0),
width: double.infinity,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: const Size.fromHeight(40), //
backgroundColor: const Color(0xFF6E7BFB),
foregroundColor: const Color(0xFFffffff),
shape: const StadiumBorder(),
),
onPressed: () {
final Box box = Hive.box<User>("USER_BOX");
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(),
formKey.currentState?.fields['gewicht']
?.value ??
box.get("USER").gewicht,
formKey.currentState?.fields['groesse']
?.value ??
box.get("USER").groesse,
formKey.currentState?.fields['alter']?.value ??
box.get("USER").alter,
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),
content: Text('Eine Beispiel Snackbar'),
));
} else {
box.put(
"USER",
User(
formKey.currentState?.fields['vorname']?.value,
formKey.currentState?.fields['nachname']?.value,
formKey.currentState?.fields['gewicht']?.value,
formKey.currentState?.fields['groesse']?.value,
formKey.currentState?.fields['alter']?.value,
formKey
.currentState?.fields['kalorien']?.value));
}
2023-06-03 00:40:40 +02:00
2023-06-06 23:45:02 +02:00
Future.delayed(
const Duration(seconds: 2),
() => Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (BuildContext context) =>
const MainPage())));
2023-06-03 00:40:40 +02:00
},
child: const Text("Eingaben bestätigen"),
)),
],
)),
));
}
}