Add DropdownButtonFormField value param test (#170518)

This PR adds a new test that verifies that the value param is only used
on initial build and when resetting the field.

Test for
https://github.com/flutter/flutter/pull/170050#issuecomment-2965486000

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

---------

Co-authored-by: Bruno Leroux <bruno.leroux@gmail.com>
This commit is contained in:
Alex Medinsh 2025-06-16 19:18:05 +03:00 committed by GitHub
parent a33b81c3f9
commit fc7c97c146
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -588,6 +588,55 @@ void main() {
expect(value, equals('three'));
});
testWidgets('Dropdown form field only uses value parameter when first built and when reset', (
WidgetTester tester,
) async {
final GlobalKey<FormFieldState<String>> fieldKey = GlobalKey<FormFieldState<String>>();
await tester.pumpWidget(
StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return MaterialApp(
home: Material(
child: DropdownButtonFormField<String>(
key: fieldKey,
value: 'one',
hint: const Text('Select Value'),
items:
menuItems.map((String val) {
return DropdownMenuItem<String>(value: val, child: Text(val));
}).toList(),
onChanged: (String? newValue) {
setState(() {
// Do nothing, just to trigger a rebuild.
});
},
),
),
);
},
),
);
expect(fieldKey.currentState!.value, 'one');
// Open the dropdown menu.
await tester.tap(find.text('one'));
await tester.pumpAndSettle();
await tester.tap(find.text('three').last);
await tester.pumpAndSettle();
// The value should update to selected, not the initial value.
expect(find.text('three'), findsOneWidget);
expect(fieldKey.currentState!.value, 'three');
fieldKey.currentState!.reset();
await tester.pump();
// Reset to the initial value.
expect(find.text('one'), findsOneWidget);
expect(fieldKey.currentState!.value, 'one');
});
testWidgets('Dropdown in ListView', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/12053
// Positions a DropdownButton at the left and right edges of the screen,