Skip to content

Commit 2a801d5

Browse files
committed
feat(authenticator): expose optional TextEditingController and improve sync timing
- Add nullable controller getter to AuthenticatorFormField and include it in diagnostics - Add debugFillProperties implementations to form field widgets to surface the controller prop - Rework text controller sync logic in AuthenticatorTextField and AuthenticatorUsernameField: - Skip syncing in initState and avoid syncing during build - Schedule post-frame callbacks in didChangeDependencies to first apply controller->state (if controller has initial text) then sync state->controller - Minor conditional/formatting cleanup in username field sync
1 parent 23d0f31 commit 2a801d5

File tree

8 files changed

+127
-22
lines changed

8 files changed

+127
-22
lines changed

packages/authenticator/amplify_authenticator/lib/src/mixins/authenticator_text_field.dart

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,25 @@ mixin AuthenticatorTextField<
6868
void initState() {
6969
super.initState();
7070
_maybeUpdateEffectiveController();
71-
_syncControllerText(force: true);
71+
// Skip sync in initState since 'state' isn't available yet
7272
}
7373

7474
@override
7575
void didChangeDependencies() {
7676
super.didChangeDependencies();
7777
_maybeUpdateEffectiveController();
78-
_syncControllerText();
78+
// Schedule both syncs after build completes
79+
WidgetsBinding.instance.addPostFrameCallback((_) {
80+
if (mounted) {
81+
// First sync controller -> state if controller has initial text
82+
if (_effectiveController != null &&
83+
_lastSyncedControllerValue == null) {
84+
_handleControllerChanged();
85+
}
86+
// Then sync state -> controller to ensure they're in sync
87+
_syncControllerText();
88+
}
89+
});
7990
}
8091

8192
@override
@@ -105,14 +116,13 @@ mixin AuthenticatorTextField<
105116
).obscureTextToggleValue,
106117
builder: (BuildContext context, bool toggleObscureText, Widget? _) {
107118
final obscureText = this.obscureText && toggleObscureText;
108-
_syncControllerText();
119+
// Don't sync during build
109120
return TextFormField(
110121
style: enabled
111122
? null
112123
: TextStyle(color: Theme.of(context).disabledColor),
113124
controller: _effectiveController,
114-
initialValue:
115-
_effectiveController == null ? initialValue : null,
125+
initialValue: _effectiveController == null ? initialValue : null,
116126
enabled: enabled,
117127
validator: widget.validatorOverride ?? validator,
118128
onChanged: onChanged,

packages/authenticator/amplify_authenticator/lib/src/mixins/authenticator_username_field.dart

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ mixin AuthenticatorUsernameField<
6969
}
7070

7171
void _syncControllerText({bool force = false}) {
72-
if (_controller == null || selectedUsernameType == UsernameType.phoneNumber) {
72+
if (_controller == null ||
73+
selectedUsernameType == UsernameType.phoneNumber) {
7374
return;
7475
}
7576

@@ -93,14 +94,24 @@ mixin AuthenticatorUsernameField<
9394
void initState() {
9495
super.initState();
9596
_updateController();
96-
_syncControllerText(force: true);
97+
// Skip sync in initState since 'state' isn't available yet
9798
}
9899

99100
@override
100101
void didChangeDependencies() {
101102
super.didChangeDependencies();
102103
_updateController();
103-
_syncControllerText();
104+
// Schedule both syncs after build completes
105+
WidgetsBinding.instance.addPostFrameCallback((_) {
106+
if (mounted) {
107+
// First sync controller -> state if controller has initial text
108+
if (_controller != null && _lastSyncedText == null) {
109+
_handleControllerChanged();
110+
}
111+
// Then sync state -> controller to ensure they're in sync
112+
_syncControllerText();
113+
}
114+
});
104115
}
105116

106117
@override
@@ -327,7 +338,7 @@ mixin AuthenticatorUsernameField<
327338
}
328339

329340
_updateController();
330-
_syncControllerText();
341+
// Don't sync during build
331342

332343
return TextFormField(
333344
style: enabled ? null : TextStyle(color: Theme.of(context).disabledColor),

packages/authenticator/amplify_authenticator/lib/src/widgets/form_field.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ abstract class AuthenticatorFormField<
9696
/// Autocomplete hints to override the default value
9797
final Iterable<String>? autofillHints;
9898

99+
/// Optional text controller exposed by text-driven form fields.
100+
TextEditingController? get controller => null;
101+
99102
/// Whether the field is required in the form.
100103
///
101104
/// Defaults to `false`.
@@ -126,6 +129,9 @@ abstract class AuthenticatorFormField<
126129
..add(DiagnosticsProperty<bool?>('requiredOverride', requiredOverride))
127130
..add(EnumProperty<UsernameType?>('usernameType', usernameType))
128131
..add(IterableProperty<String>('autofillHints', autofillHints));
132+
properties.add(
133+
DiagnosticsProperty<TextEditingController?>('controller', controller),
134+
);
129135
}
130136
}
131137

packages/authenticator/amplify_authenticator/lib/src/widgets/form_fields/confirm_sign_in_form_field.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,14 @@ class _ConfirmSignInPhoneField extends ConfirmSignInFormField<String> {
529529
@override
530530
_ConfirmSignInPhoneFieldState createState() =>
531531
_ConfirmSignInPhoneFieldState();
532+
533+
@override
534+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
535+
super.debugFillProperties(properties);
536+
properties.add(
537+
DiagnosticsProperty<TextEditingController?>('controller', controller),
538+
);
539+
}
532540
}
533541

534542
class _ConfirmSignInPhoneFieldState extends _ConfirmSignInTextFieldState
@@ -586,6 +594,14 @@ class _ConfirmSignInTextField extends ConfirmSignInFormField<String> {
586594

587595
@override
588596
_ConfirmSignInTextFieldState createState() => _ConfirmSignInTextFieldState();
597+
598+
@override
599+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
600+
super.debugFillProperties(properties);
601+
properties.add(
602+
DiagnosticsProperty<TextEditingController?>('controller', controller),
603+
);
604+
}
589605
}
590606

591607
class _ConfirmSignInTextFieldState extends _ConfirmSignInFormFieldState<String>

packages/authenticator/amplify_authenticator/lib/src/widgets/form_fields/confirm_sign_up_form_field.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ class _ConfirmSignUpTextField extends ConfirmSignUpFormField<String> {
142142

143143
@override
144144
_ConfirmSignUpTextFieldState createState() => _ConfirmSignUpTextFieldState();
145+
146+
@override
147+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
148+
super.debugFillProperties(properties);
149+
properties.add(
150+
DiagnosticsProperty<TextEditingController?>('controller', controller),
151+
);
152+
}
145153
}
146154

147155
class _ConfirmSignUpTextFieldState extends _ConfirmSignUpFormFieldState<String>
@@ -217,6 +225,14 @@ class _ConfirmSignUpUsernameField
217225
@override
218226
_ConfirmSignUpUsernameFieldState createState() =>
219227
_ConfirmSignUpUsernameFieldState();
228+
229+
@override
230+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
231+
super.debugFillProperties(properties);
232+
properties.add(
233+
DiagnosticsProperty<TextEditingController?>('controller', controller),
234+
);
235+
}
220236
}
221237

222238
class _ConfirmSignUpUsernameFieldState

packages/authenticator/amplify_authenticator/lib/src/widgets/form_fields/sign_in_form_field.dart

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ class _SignInTextField extends SignInFormField<String> {
141141

142142
@override
143143
_SignInTextFieldState createState() => _SignInTextFieldState();
144+
145+
@override
146+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
147+
super.debugFillProperties(properties);
148+
properties.add(
149+
DiagnosticsProperty<TextEditingController?>('controller', controller),
150+
);
151+
}
144152
}
145153

146154
class _SignInTextFieldState extends _SignInFormFieldState<String>
@@ -197,18 +205,25 @@ class _SignInUsernameField extends SignInFormField<UsernameInput> {
197205
super.validator,
198206
super.autofillHints,
199207
this.controller,
200-
})
201-
: super._(
202-
key: key ?? keyUsernameSignInFormField,
203-
titleKey: InputResolverKey.usernameTitle,
204-
hintTextKey: InputResolverKey.usernameHint,
205-
field: SignInField.username,
206-
);
208+
}) : super._(
209+
key: key ?? keyUsernameSignInFormField,
210+
titleKey: InputResolverKey.usernameTitle,
211+
hintTextKey: InputResolverKey.usernameHint,
212+
field: SignInField.username,
213+
);
207214

208215
final TextEditingController? controller;
209216

210217
@override
211218
_SignInUsernameFieldState createState() => _SignInUsernameFieldState();
219+
220+
@override
221+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
222+
super.debugFillProperties(properties);
223+
properties.add(
224+
DiagnosticsProperty<TextEditingController?>('controller', controller),
225+
);
226+
}
212227
}
213228

214229
class _SignInUsernameFieldState extends _SignInFormFieldState<UsernameInput>

packages/authenticator/amplify_authenticator/lib/src/widgets/form_fields/sign_up_form_field.dart

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,14 @@ class _SignUpTextField extends SignUpFormField<String> {
489489

490490
@override
491491
_SignUpTextFieldState createState() => _SignUpTextFieldState();
492+
493+
@override
494+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
495+
super.debugFillProperties(properties);
496+
properties.add(
497+
DiagnosticsProperty<TextEditingController?>('controller', controller),
498+
);
499+
}
492500
}
493501

494502
class _SignUpTextFieldState extends _SignUpFormFieldState<String>
@@ -668,17 +676,24 @@ class _SignUpUsernameField extends SignUpFormField<UsernameInput> {
668676
super.validator,
669677
super.autofillHints,
670678
this.controller,
671-
})
672-
: super._(
673-
field: SignUpField.username,
674-
titleKey: InputResolverKey.usernameTitle,
675-
hintTextKey: InputResolverKey.usernameHint,
676-
);
679+
}) : super._(
680+
field: SignUpField.username,
681+
titleKey: InputResolverKey.usernameTitle,
682+
hintTextKey: InputResolverKey.usernameHint,
683+
);
677684

678685
final TextEditingController? controller;
679686

680687
@override
681688
_SignUpUsernameFieldState createState() => _SignUpUsernameFieldState();
689+
690+
@override
691+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
692+
super.debugFillProperties(properties);
693+
properties.add(
694+
DiagnosticsProperty<TextEditingController?>('controller', controller),
695+
);
696+
}
682697
}
683698

684699
class _SignUpUsernameFieldState extends _SignUpFormFieldState<UsernameInput>
@@ -704,6 +719,14 @@ class _SignUpPhoneField extends SignUpFormField<String> {
704719

705720
@override
706721
_SignUpPhoneFieldState createState() => _SignUpPhoneFieldState();
722+
723+
@override
724+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
725+
super.debugFillProperties(properties);
726+
properties.add(
727+
DiagnosticsProperty<TextEditingController?>('controller', controller),
728+
);
729+
}
707730
}
708731

709732
class _SignUpPhoneFieldState extends _SignUpTextFieldState

packages/authenticator/amplify_authenticator/lib/src/widgets/form_fields/verify_user_form_field.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ class _VerifyUserTextField extends VerifyUserFormField<String> {
7878

7979
@override
8080
_VerifyUserTextFieldState createState() => _VerifyUserTextFieldState();
81+
82+
@override
83+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
84+
super.debugFillProperties(properties);
85+
properties.add(
86+
DiagnosticsProperty<TextEditingController?>('controller', controller),
87+
);
88+
}
8189
}
8290

8391
class _VerifyUserTextFieldState extends _VerifyUserFormFieldState<String>

0 commit comments

Comments
 (0)