Skip to content

Commit 9a46142

Browse files
Merge pull request #1489 from flutter-form-builder-ecosystem/bugfix/#1365-avoid-ovewrite-field-children
test: #1365 Add test to similar fields
1 parent 84dd066 commit 9a46142

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

example/lib/minimal_code_example.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class _ExamplePageState extends State<_ExamplePage> {
4343
child: FormBuilder(
4444
key: _formKey,
4545
child: Column(
46+
spacing: 16,
4647
children: [
4748
FormBuilderFilterChips<String>(
4849
decoration: const InputDecoration(
@@ -77,7 +78,6 @@ class _ExamplePageState extends State<_ExamplePage> {
7778
FormBuilderValidators.maxLength(3),
7879
]),
7980
),
80-
const SizedBox(height: 10),
8181
ElevatedButton(
8282
onPressed: () {
8383
_formKey.currentState?.saveAndValidate();

test/src/form_builder_test.dart

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,61 @@ void main() {
620620
expect(formKey.currentState?.errors, equals({}));
621621
});
622622
});
623+
624+
group('multiple fields interaction -', () {
625+
testWidgets('Should update form builder fields when has similar fields', (
626+
tester,
627+
) async {
628+
// Arrange
629+
const firstDropdownName = '1_dropdown';
630+
const secondDropdownName = '2_dropdown';
631+
final firstDropdown = FormBuilderDropdown<int>(
632+
name: firstDropdownName,
633+
items: const [
634+
DropdownMenuItem(value: 1, child: Text('1')),
635+
DropdownMenuItem(value: 2, child: Text('2')),
636+
DropdownMenuItem(value: 3, child: Text('3')),
637+
],
638+
);
639+
final secondDropdown = FormBuilderDropdown(
640+
name: secondDropdownName,
641+
items: const [
642+
DropdownMenuItem(value: 1, child: Text('1')),
643+
DropdownMenuItem(value: 2, child: Text('2')),
644+
DropdownMenuItem(value: 3, child: Text('3')),
645+
],
646+
);
647+
await tester.pumpWidget(
648+
buildTestableFieldWidget(
649+
Column(children: [firstDropdown, secondDropdown]),
650+
),
651+
);
652+
653+
// Act
654+
final firstDropdownFinder = find.byWidget(firstDropdown);
655+
await tester.tap(firstDropdownFinder);
656+
await tester.pumpAndSettle();
657+
await tester.tap(find.text('1').last);
658+
await tester.pumpAndSettle();
659+
660+
// Assert
661+
expect(formInstantValue(firstDropdownName), 1);
662+
expect(formInstantValue(secondDropdownName), isNull);
663+
664+
// Act
665+
final secondDropdownFinder = find.byWidget(secondDropdown);
666+
await tester.tap(secondDropdownFinder);
667+
await tester.pumpAndSettle();
668+
await tester.tap(find.text('2').last);
669+
await tester.pumpAndSettle();
670+
671+
// Assert
672+
expect(formInstantValue(firstDropdownName), 1);
673+
expect(formInstantValue(secondDropdownName), 2);
674+
expect(formKey.currentState?.fields, contains(firstDropdownName));
675+
expect(formKey.currentState?.fields, contains(secondDropdownName));
676+
});
677+
});
623678
}
624679

625680
// simple stateful widget that can hide and show its child with the intent of

0 commit comments

Comments
 (0)