import 'package:basic_utils/basic_utils.dart'; import 'package:flutter/material.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:flutter_form_builder/flutter_form_builder.dart'; import 'package:form_builder_validators/form_builder_validators.dart'; import 'package:hive/hive.dart'; import '../../models/form_builder.dart'; import '../../models/user.dart'; class FormBuilderTextFieldComponent extends StatefulWidget { final bool isSettingsPage; final FormTextField formTextField; const FormBuilderTextFieldComponent(this.isSettingsPage, this.formTextField, {super.key}); @override State createState() => _FormBuilderTextFieldComponentState(); } class _FormBuilderTextFieldComponentState extends State { 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: TextFieldChild(widget.isSettingsPage, widget.formTextField, enable: enable,)), 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}); dynamic getInitialValue(FormTextField textField){ final User user = Hive.box(dotenv.env['USER_BOX'] ?? 'USER').values.last; 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; } } @override Widget build(BuildContext context) { return FormBuilderTextField( name: formTextField.title, enabled: enable!, initialValue: isSettingsPage ? getInitialValue(formTextField).toString() : "", 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), keyboardType: formTextField.textInputType, maxLength: TextInputType.number == formTextField.textInputType ? 7 : 30, autovalidateMode: AutovalidateMode.onUserInteraction, validator: FormBuilderValidators.compose([ FormBuilderValidators.required(), TextInputType.number == formTextField.textInputType ? FormBuilderValidators.max(formTextField.numericMax!, errorText: formTextField.numericMaxErrorMessage) : FormBuilderValidators.maxLength(30, errorText: formTextField.maxLengthErrorMessage), TextInputType.number == formTextField.textInputType ? FormBuilderValidators.min(formTextField.numericMin!, errorText: formTextField.numericMinErrorMessage) : FormBuilderValidators.minLength(2, errorText: formTextField.minLengthErrorMessage) ]), ); } }