176 lines
6.2 KiB
Dart
176 lines
6.2 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_dotenv/flutter_dotenv.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 'form_builder_text_field.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(
|
|
widget.lockTextFields, 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: CupertinoColors.systemGreen,
|
|
foregroundColor: const Color(0xFFffffff),
|
|
shape: const StadiumBorder(),
|
|
),
|
|
onPressed: () {
|
|
final Box box = Hive.box<User>(dotenv.env['USER_BOX'] ?? 'USER');
|
|
|
|
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(),
|
|
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)));
|
|
|
|
ScaffoldMessenger.of(context)
|
|
.showSnackBar(const SnackBar(
|
|
duration: Duration(seconds: 2),
|
|
content: Text('Die Daten wurden erfolgreich gespeichert'),
|
|
));
|
|
} else {
|
|
box.put(
|
|
"USER",
|
|
User(
|
|
formKey.currentState?.fields['vorname']?.value,
|
|
formKey.currentState?.fields['nachname']?.value,
|
|
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)
|
|
));
|
|
}
|
|
Future.delayed(
|
|
const Duration(seconds: 2),
|
|
() => Navigator.of(context)
|
|
.pushNamedAndRemoveUntil('/navigation', (Route<dynamic> route)
|
|
=> false)
|
|
);
|
|
},
|
|
child: const Text("Eingaben bestätigen"),
|
|
)),
|
|
],
|
|
)),
|
|
));
|
|
}
|
|
}
|