Revert DropdownMenu non-nullable breaking change (#181074)

As explained in #180121 the PR #176711 is a breaking change, which must
be properly documented.

Closes #180121

*If you had to change anything in the [flutter/tests] repo, include a
link to the migration guide as per the [breaking change policy].*

## 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.
- [ ] 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.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
This commit is contained in:
August 2026-01-23 03:12:09 +01:00 committed by GitHub
parent 0161fc7caf
commit 91a8eb92eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 98 additions and 4 deletions

View File

@ -173,7 +173,7 @@ enum DropdownMenuCloseBehavior {
/// The [DropdownMenu] uses a [TextField] as the "anchor".
/// * [TextField], which is a text input widget that uses an [InputDecoration].
/// * [DropdownMenuEntry], which is used to build the [MenuItemButton] in the [DropdownMenu] list.
class DropdownMenu<T extends Object> extends StatefulWidget {
class DropdownMenu<T> extends StatefulWidget {
/// Creates a const [DropdownMenu].
///
/// The leading and trailing icons in the text field can be customized by using
@ -690,7 +690,7 @@ class DropdownMenu<T extends Object> extends StatefulWidget {
State<DropdownMenu<T>> createState() => _DropdownMenuState<T>();
}
class _DropdownMenuState<T extends Object> extends State<DropdownMenu<T>> {
class _DropdownMenuState<T> extends State<DropdownMenu<T>> {
static const Map<ShortcutActivator, Intent> _editableShortcuts = <ShortcutActivator, Intent>{
SingleActivator(LogicalKeyboardKey.arrowLeft): ExtendSelectionByCharacterIntent(
forward: false,

View File

@ -28,7 +28,7 @@ import 'menu_style.dart';
///
/// * [DropdownMenu], which is the underlying text field without the [Form]
/// integration.
class DropdownMenuFormField<T extends Object> extends FormField<T> {
class DropdownMenuFormField<T> extends FormField<T> {
/// Creates a [DropdownMenu] widget that is a [FormField].
///
/// For a description of the `onSaved`, `validator`, or `autovalidateMode`
@ -164,7 +164,7 @@ class DropdownMenuFormField<T extends Object> extends FormField<T> {
FormFieldState<T> createState() => _DropdownMenuFormFieldState<T>();
}
class _DropdownMenuFormFieldState<T extends Object> extends FormFieldState<T> {
class _DropdownMenuFormFieldState<T> extends FormFieldState<T> {
DropdownMenuFormField<T> get _dropdownMenuFormField => widget as DropdownMenuFormField<T>;
// The controller used to restore the selected item.

View File

@ -32,6 +32,14 @@ void main() {
return find.widgetWithText(MenuItemButton, menuItem.label).last;
}
Finder findMenuItemButton(String label) {
// For each menu items there are two MenuItemButton widgets.
// The last one is the real button item in the menu.
// The first one is not visible, it is part of _DropdownMenuBody
// which is used to compute the dropdown width.
return find.widgetWithText(MenuItemButton, label).last;
}
testWidgets('Creates an underlying DropdownMenu', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
@ -1394,4 +1402,47 @@ void main() {
);
expect(tester.getSize(find.byType(DropdownMenuFormField<MenuItem>)), Size.zero);
});
// Regression test for https://github.com/flutter/flutter/issues/180121.
testWidgets('Allow null entry to clear selection', (WidgetTester tester) async {
final controller = TextEditingController();
addTearDown(controller.dispose);
const selectNoneLabel = 'Select none';
final nullableMenuItems = <DropdownMenuEntry<String?>>[
const DropdownMenuEntry<String?>(value: null, label: selectNoneLabel),
const DropdownMenuEntry<String?>(value: 'a', label: 'A'),
const DropdownMenuEntry<String?>(value: 'b', label: 'B'),
];
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return DropdownMenuFormField<String?>(
controller: controller,
requestFocusOnTap: true,
enableFilter: true,
dropdownMenuEntries: nullableMenuItems,
onSelected: (_) {
setState(() {});
},
);
},
),
),
),
);
// Open the menu.
await tester.tap(find.byType(DropdownMenu<String?>));
await tester.pump();
// Select the 'None' item.
await tester.tap(findMenuItemButton(selectNoneLabel));
await tester.pumpAndSettle();
expect(controller.text, selectNoneLabel);
});
}

View File

@ -5333,6 +5333,49 @@ void main() {
shouldFocusPrevious: textInputAction == TextInputAction.previous,
);
}, variant: focusVariants);
// Regression test for https://github.com/flutter/flutter/issues/180121.
testWidgets('Allow null entry to clear selection', (WidgetTester tester) async {
final controller = TextEditingController();
addTearDown(controller.dispose);
const selectNoneLabel = 'Select none';
final nullableMenuItems = <DropdownMenuEntry<String?>>[
const DropdownMenuEntry<String?>(value: null, label: selectNoneLabel),
const DropdownMenuEntry<String?>(value: 'a', label: 'A'),
const DropdownMenuEntry<String?>(value: 'b', label: 'B'),
];
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return DropdownMenu<String?>(
controller: controller,
requestFocusOnTap: true,
enableFilter: true,
dropdownMenuEntries: nullableMenuItems,
onSelected: (_) {
setState(() {});
},
);
},
),
),
),
);
// Open the menu.
await tester.tap(find.byType(DropdownMenu<String?>));
await tester.pump();
// Select the 'None' item.
await tester.tap(findMenuItemButton(selectNoneLabel));
await tester.pumpAndSettle();
expect(controller.text, selectNoneLabel);
});
}
enum TestMenu {