Skip to content

Commit

Permalink
Fixing using controller's value as initial value (#218)
Browse files Browse the repository at this point in the history
* Initializer PhoneFormField super.initialValue also with controller.initialValue

* PhoneFormFieldState.didUpdateWidget

* Revert "PhoneFormFieldState.didUpdateWidget"

This reverts commit c2daad1.

* Using `controller.value` as `initialValue`

* Fix formatting

* Adding a test inspecting widget state value

* Fix formatting

---------

Co-authored-by: Muccy <muccymac@gmail.com>
  • Loading branch information
muccy-timeware and muccy authored Feb 12, 2024
1 parent 4e210e1 commit 2e187c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/src/phone_form_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class PhoneFormField extends FormField<PhoneNumber> {
this.countryButtonPadding,
// form field inputs
super.validator,
super.initialValue,
PhoneNumber? initialValue,
super.onSaved,
super.autovalidateMode = AutovalidateMode.onUserInteraction,
super.restorationId,
Expand Down Expand Up @@ -188,6 +188,7 @@ class PhoneFormField extends FormField<PhoneNumber> {
isCountryButtonPersistent ?? isCountryChipPersistent ?? true,
super(
builder: (state) => (state as PhoneFormFieldState).builder(),
initialValue: controller?.value ?? initialValue,
);

@override
Expand Down
10 changes: 10 additions & 0 deletions test/phone_form_field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ void main() {
);
});

testWidgets('Should get value of controller as initial value', (tester) async {
final controller = PhoneController();
final phoneNumber = PhoneNumber.parse('+33488997722');
controller.value = phoneNumber;
await tester.pumpWidget(getWidget(controller: controller));

final PhoneFormFieldState phoneFieldState = tester.state(find.byType(PhoneFormField));
expect(phoneFieldState.value, equals(phoneNumber));
});

testWidgets('Should change value of input when controller changes',
(tester) async {
final controller = PhoneController();
Expand Down

0 comments on commit 2e187c8

Please sign in to comment.