From 85e5b210b4a2278b7a4104acc0bf1fa4c3196e49 Mon Sep 17 00:00:00 2001 From: Shi-Hao Hong Date: Mon, 30 Mar 2020 13:48:40 -0700 Subject: [PATCH] Implement DropdownMenuItem.onTap (#53368) * Implement DropdownMenuItem.onTap --- .../flutter/lib/src/material/dropdown.dart | 12 ++- .../flutter/test/material/dropdown_test.dart | 85 +++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index 705b770d44..f4eb3421fa 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -145,9 +145,15 @@ class _DropdownMenuItemButtonState extends State<_DropdownMenuItemButton> } void _handleOnTap() { + final DropdownMenuItem dropdownMenuItem = widget.route.items[widget.itemIndex].item; + + if (dropdownMenuItem.onTap != null) { + dropdownMenuItem.onTap(); + } + Navigator.pop( context, - _DropdownRouteResult(widget.route.items[widget.itemIndex].item.value), + _DropdownRouteResult(dropdownMenuItem.value), ); } @@ -656,11 +662,15 @@ class DropdownMenuItem extends _DropdownMenuItemContainer { /// The [child] argument is required. const DropdownMenuItem({ Key key, + this.onTap, this.value, @required Widget child, }) : assert(child != null), super(key: key, child: child); + /// Called when the dropdown menu item is tapped. + final VoidCallback onTap; + /// The value to return if the user selects this menu item. /// /// Eventually returned in a call to [DropdownButton.onChanged]. diff --git a/packages/flutter/test/material/dropdown_test.dart b/packages/flutter/test/material/dropdown_test.dart index b8fd69b124..6a78cafb40 100644 --- a/packages/flutter/test/material/dropdown_test.dart +++ b/packages/flutter/test/material/dropdown_test.dart @@ -2361,4 +2361,89 @@ void main() { expect(value, equals('two')); expect(dropdownButtonTapCounter, 2); // Should not change. }); + + testWidgets('DropdownMenuItem onTap callback is called when defined', (WidgetTester tester) async { + String value = 'one'; + final List menuItemTapCounters = [0, 0, 0, 0]; + void onChanged(String newValue) { value = newValue; } + + final List onTapCallbacks = [ + () { menuItemTapCounters[0] += 1; }, + () { menuItemTapCounters[1] += 1; }, + () { menuItemTapCounters[2] += 1; }, + () { menuItemTapCounters[3] += 1; }, + ]; + + int currentIndex = -1; + await tester.pumpWidget( + TestApp( + textDirection: TextDirection.ltr, + child: Material( + child: RepaintBoundary( + child: DropdownButton( + value: value, + onChanged: onChanged, + items: menuItems.map>((String item) { + currentIndex += 1; + return DropdownMenuItem( + value: item, + onTap: onTapCallbacks[currentIndex], + child: Text(item), + ); + }).toList(), + ), + ), + ), + ), + ); + + // Tap dropdown button. + await tester.tap(find.text('one')); + await tester.pumpAndSettle(); + + expect(value, equals('one')); + // Counters should still be zero. + expect(menuItemTapCounters, [0, 0, 0, 0]); + + // Tap dropdown menu item. + await tester.tap(find.text('three').last); + await tester.pumpAndSettle(); + + // Should update the counter for the third item (second index). + expect(value, equals('three')); + expect(menuItemTapCounters, [0, 0, 1, 0]); + + // Tap dropdown button again. + await tester.tap(find.text('three')); + await tester.pumpAndSettle(); + + // Should not change. + expect(value, equals('three')); + expect(menuItemTapCounters, [0, 0, 1, 0]); + + // Tap dropdown menu item. + await tester.tap(find.text('two').last); + await tester.pumpAndSettle(); + + // Should update the counter for the second item (first index). + expect(value, equals('two')); + expect(menuItemTapCounters, [0, 1, 1, 0]); + + // Tap dropdown button again. + await tester.tap(find.text('two')); + await tester.pumpAndSettle(); + + // Should not change. + expect(value, equals('two')); + expect(menuItemTapCounters, [0, 1, 1, 0]); + + // Tap the already selected menu item + await tester.tap(find.text('two').last); + await tester.pumpAndSettle(); + + // Should update the counter for the second item (first index), even + // though it was already selected. + expect(value, equals('two')); + expect(menuItemTapCounters, [0, 2, 1, 0]); + }); }