diff --git a/packages/flutter/lib/src/material/dropdown_menu.dart b/packages/flutter/lib/src/material/dropdown_menu.dart index b86806e7fcd..bb53c5f6351 100644 --- a/packages/flutter/lib/src/material/dropdown_menu.dart +++ b/packages/flutter/lib/src/material/dropdown_menu.dart @@ -561,16 +561,17 @@ class _DropdownMenuState extends State> { double? leadingPadding; bool _menuHasEnabledItem = false; TextEditingController? _localTextEditingController; + TextEditingController get _effectiveTextEditingController => + widget.controller ?? (_localTextEditingController ??= TextEditingController()); final FocusNode _internalFocudeNode = FocusNode(); + int? _selectedEntryIndex; + late final void Function() _clearSelectedEntryIndex; @override void initState() { super.initState(); - if (widget.controller != null) { - _localTextEditingController = widget.controller; - } else { - _localTextEditingController = TextEditingController(); - } + _clearSelectedEntryIndex = () => _selectedEntryIndex = null; + _effectiveTextEditingController.addListener(_clearSelectedEntryIndex); _enableSearch = widget.enableSearch; filteredEntries = widget.dropdownMenuEntries; buttonItemKeys = List.generate(filteredEntries.length, (int index) => GlobalKey()); @@ -579,20 +580,20 @@ class _DropdownMenuState extends State> { (DropdownMenuEntry entry) => entry.value == widget.initialSelection, ); if (index != -1) { - _localTextEditingController?.value = TextEditingValue( + _effectiveTextEditingController.value = TextEditingValue( text: filteredEntries[index].label, selection: TextSelection.collapsed(offset: filteredEntries[index].label.length), ); + _selectedEntryIndex = index; } refreshLeadingPadding(); } @override void dispose() { - if (widget.controller == null) { - _localTextEditingController?.dispose(); - _localTextEditingController = null; - } + widget.controller?.removeListener(_clearSelectedEntryIndex); + _localTextEditingController?.dispose(); + _localTextEditingController = null; _internalFocudeNode.dispose(); super.dispose(); } @@ -601,10 +602,11 @@ class _DropdownMenuState extends State> { void didUpdateWidget(DropdownMenu oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.controller != widget.controller) { - if (widget.controller != null && oldWidget.controller == null) { - _localTextEditingController?.dispose(); - } - _localTextEditingController = widget.controller ?? TextEditingController(); + oldWidget.controller?.removeListener(_clearSelectedEntryIndex); + _localTextEditingController?.dispose(); + _localTextEditingController = null; + _effectiveTextEditingController.addListener(_clearSelectedEntryIndex); + _selectedEntryIndex = null; } if (oldWidget.enableFilter != widget.enableFilter) { if (!widget.enableFilter) { @@ -622,6 +624,21 @@ class _DropdownMenuState extends State> { filteredEntries = widget.dropdownMenuEntries; buttonItemKeys = List.generate(filteredEntries.length, (int index) => GlobalKey()); _menuHasEnabledItem = filteredEntries.any((DropdownMenuEntry entry) => entry.enabled); + if (_selectedEntryIndex != null) { + final T oldSelectionValue = oldWidget.dropdownMenuEntries[_selectedEntryIndex!].value; + final int index = filteredEntries.indexWhere( + (DropdownMenuEntry entry) => entry.value == oldSelectionValue, + ); + if (index != -1) { + _effectiveTextEditingController.value = TextEditingValue( + text: filteredEntries[index].label, + selection: TextSelection.collapsed(offset: filteredEntries[index].label.length), + ); + _selectedEntryIndex = index; + } else { + _selectedEntryIndex = null; + } + } } if (oldWidget.leadingIcon != widget.leadingIcon) { refreshLeadingPadding(); @@ -631,10 +648,11 @@ class _DropdownMenuState extends State> { (DropdownMenuEntry entry) => entry.value == widget.initialSelection, ); if (index != -1) { - _localTextEditingController?.value = TextEditingValue( + _effectiveTextEditingController.value = TextEditingValue( text: filteredEntries[index].label, selection: TextSelection.collapsed(offset: filteredEntries[index].label.length), ); + _selectedEntryIndex = index; } } } @@ -690,7 +708,7 @@ class _DropdownMenuState extends State> { } bool _shouldUpdateCurrentHighlight(List> entries) { - final String searchText = _localTextEditingController!.value.text.toLowerCase(); + final String searchText = _effectiveTextEditingController.value.text.toLowerCase(); if (searchText.isEmpty) { return true; } @@ -821,10 +839,22 @@ class _DropdownMenuState extends State> { onPressed: entry.enabled && widget.enabled ? () { - _localTextEditingController?.value = TextEditingValue( + if (!mounted) { + // In some cases (e.g., nested menus), calling onSelected from MenuAnchor inside a postFrameCallback + // can result in the MenuItemButton's onPressed callback being triggered after the state has been disposed. + // TODO(ahmedrasar): MenuAnchor should avoid calling onSelected inside a postFrameCallback. + widget.controller?.value = TextEditingValue( + text: entry.label, + selection: TextSelection.collapsed(offset: entry.label.length), + ); + widget.onSelected?.call(entry.value); + return; + } + _effectiveTextEditingController.value = TextEditingValue( text: entry.label, selection: TextSelection.collapsed(offset: entry.label.length), ); + _selectedEntryIndex = i; currentHighlight = widget.enableSearch ? i : null; widget.onSelected?.call(entry.value); _enableFilter = false; @@ -864,7 +894,7 @@ class _DropdownMenuState extends State> { currentHighlight = (currentHighlight! - 1) % filteredEntries.length; } final String currentLabel = filteredEntries[currentHighlight!].label; - _localTextEditingController?.value = TextEditingValue( + _effectiveTextEditingController.value = TextEditingValue( text: currentLabel, selection: TextSelection.collapsed(offset: currentLabel.length), ); @@ -884,7 +914,7 @@ class _DropdownMenuState extends State> { currentHighlight = (currentHighlight! + 1) % filteredEntries.length; } final String currentLabel = filteredEntries[currentHighlight!].label; - _localTextEditingController?.value = TextEditingValue( + _effectiveTextEditingController.value = TextEditingValue( text: currentLabel, selection: TextSelection.collapsed(offset: currentLabel.length), ); @@ -898,7 +928,7 @@ class _DropdownMenuState extends State> { } else { filteredEntries = widget.dropdownMenuEntries; // close to open - if (_localTextEditingController!.text.isNotEmpty) { + if (_effectiveTextEditingController.text.isNotEmpty) { _enableFilter = false; } controller.open(); @@ -913,10 +943,11 @@ class _DropdownMenuState extends State> { if (currentHighlight != null) { final DropdownMenuEntry entry = filteredEntries[currentHighlight!]; if (entry.enabled) { - _localTextEditingController?.value = TextEditingValue( + _effectiveTextEditingController.value = TextEditingValue( text: entry.label, selection: TextSelection.collapsed(offset: entry.label.length), ); + _selectedEntryIndex = currentHighlight; widget.onSelected?.call(entry.value); } } else { @@ -947,8 +978,8 @@ class _DropdownMenuState extends State> { if (_enableFilter) { filteredEntries = - widget.filterCallback?.call(filteredEntries, _localTextEditingController!.text) ?? - filter(widget.dropdownMenuEntries, _localTextEditingController!); + widget.filterCallback?.call(filteredEntries, _effectiveTextEditingController.text) ?? + filter(widget.dropdownMenuEntries, _effectiveTextEditingController); } _menuHasEnabledItem = filteredEntries.any((DropdownMenuEntry entry) => entry.enabled); @@ -956,12 +987,12 @@ class _DropdownMenuState extends State> { if (widget.searchCallback != null) { currentHighlight = widget.searchCallback!( filteredEntries, - _localTextEditingController!.text, + _effectiveTextEditingController.text, ); } else { final bool shouldUpdateCurrentHighlight = _shouldUpdateCurrentHighlight(filteredEntries); if (shouldUpdateCurrentHighlight) { - currentHighlight = search(filteredEntries, _localTextEditingController!); + currentHighlight = search(filteredEntries, _effectiveTextEditingController); } } if (currentHighlight != null) { @@ -1060,7 +1091,7 @@ class _DropdownMenuState extends State> { maxLines: widget.maxLines, textInputAction: widget.textInputAction, style: effectiveTextStyle, - controller: _localTextEditingController, + controller: _effectiveTextEditingController, onEditingComplete: _handleEditingComplete, onTap: !widget.enabled diff --git a/packages/flutter/test/material/dropdown_menu_test.dart b/packages/flutter/test/material/dropdown_menu_test.dart index ec815789e96..b2afab814b4 100644 --- a/packages/flutter/test/material/dropdown_menu_test.dart +++ b/packages/flutter/test/material/dropdown_menu_test.dart @@ -2442,6 +2442,178 @@ void main() { }, ); + testWidgets('Rematch selection against the first entry with the same value', ( + WidgetTester tester, + ) async { + final TextEditingController controller = TextEditingController(); + addTearDown(controller.dispose); + + String selectionLabel = 'Initial label'; + + await tester.pumpWidget( + MaterialApp( + home: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return Scaffold( + body: DropdownMenu( + initialSelection: TestMenu.mainMenu0, + dropdownMenuEntries: >[ + DropdownMenuEntry( + value: TestMenu.mainMenu0, + label: '$selectionLabel 0', + ), + DropdownMenuEntry( + value: TestMenu.mainMenu1, + label: '$selectionLabel 1', + ), + ], + controller: controller, + ), + floatingActionButton: FloatingActionButton( + onPressed: () => setState(() => selectionLabel = 'Updated label'), + ), + ); + }, + ), + ), + ); + + // Open the menu + await tester.tap(find.byType(DropdownMenu)); + await tester.pump(); + + // Select the second item + await tester.tap(findMenuItemButton('$selectionLabel 1')); + await tester.pump(); + + // Update dropdownMenuEntries labels + await tester.tap(find.byType(FloatingActionButton)); + await tester.pump(); + + expect(controller.text, 'Updated label 1'); + }); + + testWidgets('Forget selection if its value does not map to any entry', ( + WidgetTester tester, + ) async { + final TextEditingController controller = TextEditingController(); + addTearDown(controller.dispose); + + String selectionLabel = 'Initial label'; + bool selectionInEntries = true; + + await tester.pumpWidget( + MaterialApp( + home: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return Scaffold( + body: Column( + children: [ + DropdownMenu( + initialSelection: TestMenu.mainMenu0, + dropdownMenuEntries: >[ + DropdownMenuEntry( + value: TestMenu.mainMenu0, + label: '$selectionLabel 0', + ), + if (selectionInEntries) + DropdownMenuEntry( + value: TestMenu.mainMenu1, + label: '$selectionLabel 1', + ), + ], + controller: controller, + ), + ElevatedButton( + onPressed: () => setState(() => selectionInEntries = !selectionInEntries), + child: null, + ), + ], + ), + floatingActionButton: FloatingActionButton( + onPressed: () => setState(() => selectionLabel = 'Updated label'), + ), + ); + }, + ), + ), + ); + + // Open the menu + await tester.tap(find.byType(DropdownMenu)); + await tester.pump(); + + // Select the second item + await tester.tap(findMenuItemButton('$selectionLabel 1')); + await tester.pump(); + + // Update dropdownMenuEntries labels + await tester.tap(find.byType(FloatingActionButton)); + // Remove second item from entires + await tester.tap(find.byType(ElevatedButton)); + await tester.pump(); + + expect(controller.text, 'Initial label 1'); + + // Put second item back into entries + await tester.tap(find.byType(ElevatedButton)); + await tester.pump(); + + expect(controller.text, 'Initial label 1'); + }); + + testWidgets( + 'Do not rematch selection if the text field was edited progrmaticlly via controller', + (WidgetTester tester) async { + final TextEditingController controller = TextEditingController(); + addTearDown(controller.dispose); + + String selectionLabel = 'Initial label'; + + await tester.pumpWidget( + MaterialApp( + home: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return Scaffold( + body: Column( + children: [ + DropdownMenu( + initialSelection: TestMenu.mainMenu0, + dropdownMenuEntries: >[ + DropdownMenuEntry( + value: TestMenu.mainMenu0, + label: '$selectionLabel 0', + ), + ], + controller: controller, + ), + ElevatedButton( + onPressed: () => setState(() => controller.text = 'Controller Value'), + child: null, + ), + ], + ), + floatingActionButton: FloatingActionButton( + onPressed: () => setState(() => selectionLabel = 'Updated label'), + ), + ); + }, + ), + ), + ); + + // Change the text field value via controller + await tester.tap(find.byType(ElevatedButton)); + await tester.pump(); + + // Update dropdownMenuEntries labels + await tester.tap(find.byType(FloatingActionButton)); + await tester.pump(); + + expect(controller.text, 'Controller Value'); + }, + ); + testWidgets('The default text input field should not be focused on mobile platforms ' 'when it is tapped', (WidgetTester tester) async { final ThemeData themeData = ThemeData();