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

120 lines
4.1 KiB
Dart
Raw Normal View History

2023-06-03 00:40:40 +02:00
import 'package:basic_utils/basic_utils.dart';
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:form_builder_validators/form_builder_validators.dart';
2023-06-19 20:23:27 +02:00
import 'package:hive/hive.dart';
2023-06-25 14:43:07 +02:00
import '../../models/form_builder.dart';
import '../../models/user.dart';
2023-06-03 00:40:40 +02:00
class FormBuilderTextFieldComponent extends StatefulWidget {
final bool isSettingsPage;
final FormTextField formTextField;
const FormBuilderTextFieldComponent(this.isSettingsPage, this.formTextField,
{super.key});
@override
State<FormBuilderTextFieldComponent> createState() => _FormBuilderTextFieldComponentState();
}
class _FormBuilderTextFieldComponentState extends State<FormBuilderTextFieldComponent> {
bool enable = false;
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 0),
child: widget.isSettingsPage
? Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child:
2023-06-19 20:23:27 +02:00
TextFieldChild(widget.isSettingsPage, widget.formTextField, enable: enable,)),
2023-06-03 00:40:40 +02:00
enable
? IconButton(
onPressed: () async {
setState(() {
enable = !enable;
});
},
icon: const Icon(Icons.lock_open))
: IconButton(
onPressed: () async {
setState(() {
enable = !enable;
});
},
icon: const Icon(Icons.lock))
],
)
: TextFieldChild(widget.isSettingsPage, widget.formTextField),
);
}
}
class TextFieldChild extends StatelessWidget {
final bool isSettingsPage;
final FormTextField formTextField;
final bool? enable;
const TextFieldChild(this.isSettingsPage, this.formTextField,
{super.key, this.enable = true});
2023-06-19 20:23:27 +02:00
dynamic getInitialValue(FormTextField textField){
2023-06-25 14:43:07 +02:00
final User user = Hive.box<User>(dotenv.env['USER_BOX'] ?? 'USER').values.last;
2023-06-19 20:23:27 +02:00
if (textField.title == "vorname"){
return user.vorname;
}else if (textField.title == "nachname"){
return user.nachname;
}else if (textField.title == "gewicht"){
return user.gewicht;
}else if (textField.title == "groesse"){
return user.groesse;
}else if (textField.title == "alter"){
return user.alter;
}else if (textField.title == "kalorien"){
return user.kalorien;
}
}
2023-06-03 00:40:40 +02:00
@override
Widget build(BuildContext context) {
return FormBuilderTextField(
name: formTextField.title,
enabled: enable!,
2023-06-19 20:23:27 +02:00
initialValue: isSettingsPage ? getInitialValue(formTextField).toString() : "",
2023-06-03 00:40:40 +02:00
decoration: InputDecoration(
border: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0))),
filled: true,
hintStyle: TextStyle(color: Colors.grey.shade400),
hintText: StringUtils.capitalize(formTextField.title),
labelText: StringUtils.capitalize(formTextField.title),
fillColor: Colors.white70),
2023-06-03 01:06:38 +02:00
keyboardType: formTextField.textInputType,
2023-06-03 00:40:40 +02:00
maxLength: TextInputType.number == formTextField.textInputType ? 7 : 30,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: FormBuilderValidators.compose([
FormBuilderValidators.required(),
2023-06-03 01:06:38 +02:00
2023-06-03 00:40:40 +02:00
TextInputType.number == formTextField.textInputType
? FormBuilderValidators.max(formTextField.numericMax!,
errorText: formTextField.numericMaxErrorMessage)
: FormBuilderValidators.maxLength(30,
errorText: formTextField.maxLengthErrorMessage),
2023-06-03 01:06:38 +02:00
2023-06-03 00:40:40 +02:00
TextInputType.number == formTextField.textInputType
? FormBuilderValidators.min(formTextField.numericMin!,
errorText: formTextField.numericMinErrorMessage)
: FormBuilderValidators.minLength(2,
errorText: formTextField.minLengthErrorMessage)
]),
);
}
}