From fc7c97c146a553ef92b4eab9d3b08cd98d0bfc0f Mon Sep 17 00:00:00 2001 From: Alex Medinsh Date: Mon, 16 Jun 2025 19:18:05 +0300 Subject: [PATCH] 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 --- .../flutter/test/material/dropdown_test.dart | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/packages/flutter/test/material/dropdown_test.dart b/packages/flutter/test/material/dropdown_test.dart index 88bc2693815..961ef862706 100644 --- a/packages/flutter/test/material/dropdown_test.dart +++ b/packages/flutter/test/material/dropdown_test.dart @@ -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> fieldKey = GlobalKey>(); + await tester.pumpWidget( + StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return MaterialApp( + home: Material( + child: DropdownButtonFormField( + key: fieldKey, + value: 'one', + hint: const Text('Select Value'), + items: + menuItems.map((String val) { + return DropdownMenuItem(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,