From b94196b41ef08865aa695cf6087be6dcc0e1109d Mon Sep 17 00:00:00 2001 From: Yasien Mac Mini Date: Wed, 4 Jun 2025 11:12:14 +0200 Subject: [PATCH] fix text field to hanlde controller changes --- .../mih_text_form_field.dart | 81 ++++++++----------- 1 file changed, 34 insertions(+), 47 deletions(-) diff --git a/Frontend/lib/mih_components/mih_package_components/mih_text_form_field.dart b/Frontend/lib/mih_components/mih_package_components/mih_text_form_field.dart index b5257660..347ad768 100644 --- a/Frontend/lib/mih_components/mih_package_components/mih_text_form_field.dart +++ b/Frontend/lib/mih_components/mih_package_components/mih_text_form_field.dart @@ -46,11 +46,40 @@ class MihTextFormField extends StatefulWidget { class _MihTextFormFieldState extends State { late bool _obscureText; + FormFieldState? _formFieldState; @override void initState() { super.initState(); _obscureText = widget.passwordMode ?? false; + widget.controller.addListener(_onControllerTextChanged); + } + + @override + void didUpdateWidget(covariant MihTextFormField oldWidget) { + super.didUpdateWidget(oldWidget); + // If the controller itself changes, remove listener from old and add to new + if (widget.controller != oldWidget.controller) { + oldWidget.controller.removeListener(_onControllerTextChanged); + widget.controller.addListener(_onControllerTextChanged); + // Immediately update form field state if controller changed and has value + _formFieldState?.didChange(widget.controller.text); + } + } + + void _onControllerTextChanged() { + // Only update the FormField's value if it's not already the same + // and if the formFieldState is available. + if (_formFieldState != null && + _formFieldState!.value != widget.controller.text) { + _formFieldState!.didChange(widget.controller.text); + } + } + + @override + void dispose() { + widget.controller.removeListener(_onControllerTextChanged); + super.dispose(); } @override @@ -93,9 +122,11 @@ class _MihTextFormFieldState extends State { ), const SizedBox(height: 4), FormField( + initialValue: widget.controller.text, validator: widget.validator, autovalidateMode: AutovalidateMode.onUserInteraction, builder: (field) { + _formFieldState = field; return Column( crossAxisAlignment: CrossAxisAlignment.start, // <-- Add this line @@ -150,12 +181,6 @@ class _MihTextFormFieldState extends State { : null, errorStyle: const TextStyle( height: 0, fontSize: 0), // <-- Add this line - // errorStyle: TextStyle( - // color: MzanziInnovationHub.of(context)! - // .theme - // .errorColor(), - // fontWeight: FontWeight.bold, - // ), contentPadding: const EdgeInsets.symmetric( horizontal: 10.0, vertical: 8.0), filled: true, @@ -186,7 +211,7 @@ class _MihTextFormFieldState extends State { .theme .errorColor() : widget.inputColor, - width: 2.0, + width: 3.0, ), ), errorBorder: OutlineInputBorder( @@ -196,7 +221,7 @@ class _MihTextFormFieldState extends State { color: MzanziInnovationHub.of(context)! .theme .errorColor(), - width: 2.0, + width: 3.0, ), ), focusedErrorBorder: OutlineInputBorder( @@ -206,47 +231,9 @@ class _MihTextFormFieldState extends State { color: MzanziInnovationHub.of(context)! .theme .errorColor(), - width: 2.0, + width: 3.0, ), ), - // border: OutlineInputBorder( - // borderRadius: BorderRadius.circular( - // widget.borderRadius ?? 8.0), - // borderSide: BorderSide.none, - // ), - // enabledBorder: OutlineInputBorder( - // borderRadius: BorderRadius.circular( - // widget.borderRadius ?? 8.0), - // borderSide: BorderSide.none, - // ), - // focusedBorder: OutlineInputBorder( - // borderRadius: BorderRadius.circular( - // widget.borderRadius ?? 8.0), - // borderSide: BorderSide( - // color: widget.inputColor, - // width: 2.0, - // ), - // ), - // errorBorder: OutlineInputBorder( - // borderRadius: BorderRadius.circular( - // widget.borderRadius ?? 8.0), - // borderSide: BorderSide( - // color: MzanziInnovationHub.of(context)! - // .theme - // .errorColor(), - // width: 2.0, - // ), - // ), - // focusedErrorBorder: OutlineInputBorder( - // borderRadius: BorderRadius.circular( - // widget.borderRadius ?? 8.0), - // borderSide: BorderSide( - // color: MzanziInnovationHub.of(context)! - // .theme - // .errorColor(), - // width: 2.0, - // ), - // ), ), onChanged: (value) { field.didChange(value);