fix text field to hanlde controller changes
This commit is contained in:
@@ -46,11 +46,40 @@ class MihTextFormField extends StatefulWidget {
|
|||||||
|
|
||||||
class _MihTextFormFieldState extends State<MihTextFormField> {
|
class _MihTextFormFieldState extends State<MihTextFormField> {
|
||||||
late bool _obscureText;
|
late bool _obscureText;
|
||||||
|
FormFieldState<String>? _formFieldState;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_obscureText = widget.passwordMode ?? false;
|
_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
|
@override
|
||||||
@@ -93,9 +122,11 @@ class _MihTextFormFieldState extends State<MihTextFormField> {
|
|||||||
),
|
),
|
||||||
const SizedBox(height: 4),
|
const SizedBox(height: 4),
|
||||||
FormField<String>(
|
FormField<String>(
|
||||||
|
initialValue: widget.controller.text,
|
||||||
validator: widget.validator,
|
validator: widget.validator,
|
||||||
autovalidateMode: AutovalidateMode.onUserInteraction,
|
autovalidateMode: AutovalidateMode.onUserInteraction,
|
||||||
builder: (field) {
|
builder: (field) {
|
||||||
|
_formFieldState = field;
|
||||||
return Column(
|
return Column(
|
||||||
crossAxisAlignment:
|
crossAxisAlignment:
|
||||||
CrossAxisAlignment.start, // <-- Add this line
|
CrossAxisAlignment.start, // <-- Add this line
|
||||||
@@ -150,12 +181,6 @@ class _MihTextFormFieldState extends State<MihTextFormField> {
|
|||||||
: null,
|
: null,
|
||||||
errorStyle: const TextStyle(
|
errorStyle: const TextStyle(
|
||||||
height: 0, fontSize: 0), // <-- Add this line
|
height: 0, fontSize: 0), // <-- Add this line
|
||||||
// errorStyle: TextStyle(
|
|
||||||
// color: MzanziInnovationHub.of(context)!
|
|
||||||
// .theme
|
|
||||||
// .errorColor(),
|
|
||||||
// fontWeight: FontWeight.bold,
|
|
||||||
// ),
|
|
||||||
contentPadding: const EdgeInsets.symmetric(
|
contentPadding: const EdgeInsets.symmetric(
|
||||||
horizontal: 10.0, vertical: 8.0),
|
horizontal: 10.0, vertical: 8.0),
|
||||||
filled: true,
|
filled: true,
|
||||||
@@ -186,7 +211,7 @@ class _MihTextFormFieldState extends State<MihTextFormField> {
|
|||||||
.theme
|
.theme
|
||||||
.errorColor()
|
.errorColor()
|
||||||
: widget.inputColor,
|
: widget.inputColor,
|
||||||
width: 2.0,
|
width: 3.0,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
errorBorder: OutlineInputBorder(
|
errorBorder: OutlineInputBorder(
|
||||||
@@ -196,7 +221,7 @@ class _MihTextFormFieldState extends State<MihTextFormField> {
|
|||||||
color: MzanziInnovationHub.of(context)!
|
color: MzanziInnovationHub.of(context)!
|
||||||
.theme
|
.theme
|
||||||
.errorColor(),
|
.errorColor(),
|
||||||
width: 2.0,
|
width: 3.0,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
focusedErrorBorder: OutlineInputBorder(
|
focusedErrorBorder: OutlineInputBorder(
|
||||||
@@ -206,47 +231,9 @@ class _MihTextFormFieldState extends State<MihTextFormField> {
|
|||||||
color: MzanziInnovationHub.of(context)!
|
color: MzanziInnovationHub.of(context)!
|
||||||
.theme
|
.theme
|
||||||
.errorColor(),
|
.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) {
|
onChanged: (value) {
|
||||||
field.didChange(value);
|
field.didChange(value);
|
||||||
|
|||||||
Reference in New Issue
Block a user