From e796d9a2090e6ab3392c4e01e2d15e0d5a5f3b8a Mon Sep 17 00:00:00 2001 From: Colton Grubbs Date: Fri, 19 Jul 2024 21:23:30 -0400 Subject: [PATCH 1/2] feat: onLongPress --- .../lib/src/dropdown_menu_item.dart | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/packages/dropdown_button2/lib/src/dropdown_menu_item.dart b/packages/dropdown_button2/lib/src/dropdown_menu_item.dart index aea156a..bed45a3 100644 --- a/packages/dropdown_button2/lib/src/dropdown_menu_item.dart +++ b/packages/dropdown_button2/lib/src/dropdown_menu_item.dart @@ -14,6 +14,7 @@ class DropdownItem extends _DropdownMenuItemContainer { super.intrinsicHeight, super.alignment, this.onTap, + this.onLongPress, this.value, this.enabled = true, this.closeOnTap = true, @@ -23,6 +24,9 @@ class DropdownItem extends _DropdownMenuItemContainer { /// Called when the dropdown menu item is tapped. final VoidCallback? onTap; + /// Called when the dropdown menu item is long pressed. + final VoidCallback? onLongPress; + /// The value to return if the user selects this menu item. /// /// Eventually returned in a call to [DropdownButton.onChanged]. @@ -185,14 +189,11 @@ class _DropdownItemButtonState extends State<_DropdownItemButton> { } } - static const Map _webShortcuts = - { + static const Map _webShortcuts = { // On the web, up/down don't change focus, *except* in a // element, which is what a dropdown emulates. - SingleActivator(LogicalKeyboardKey.arrowDown): DirectionalFocusIntent(TraversalDirection.down), - SingleActivator(LogicalKeyboardKey.arrowUp): DirectionalFocusIntent(TraversalDirection.up), + SingleActivator(LogicalKeyboardKey.arrowDown): + DirectionalFocusIntent(TraversalDirection.down), + SingleActivator(LogicalKeyboardKey.arrowUp): + DirectionalFocusIntent(TraversalDirection.up), }; MenuItemStyleData get menuItemStyle => widget.route.menuItemStyle; @@ -204,18 +207,22 @@ class _DropdownItemButtonState extends State<_DropdownItemButton> { final DropdownItem dropdownItem = widget.route.items[widget.itemIndex]; final double unit = 0.5 / (widget.route.items.length + 1.5); - final double start = clampDouble(menuCurveEnd + (widget.itemIndex + 1) * unit, 0.0, 1.0); + final double start = + clampDouble(menuCurveEnd + (widget.itemIndex + 1) * unit, 0.0, 1.0); final double end = clampDouble(start + 1.5 * unit, 0.0, 1.0); - final CurvedAnimation opacity = CurvedAnimation(parent: widget.route.animation!, curve: Interval(start, end)); + final CurvedAnimation opacity = CurvedAnimation( + parent: widget.route.animation!, curve: Interval(start, end)); Widget child = Container( - padding: (menuItemStyle.padding ?? _kMenuItemPadding).resolve(widget.textDirection), + padding: (menuItemStyle.padding ?? _kMenuItemPadding) + .resolve(widget.textDirection), child: dropdownItem, ); // An [InkWell] is added to the item only if it is enabled // isNoSelectedItem to avoid first item highlight when no item selected if (dropdownItem.enabled) { - final bool isSelectedItem = !widget.route.isNoSelectedItem && widget.itemIndex == widget.route.selectedIndex; + final bool isSelectedItem = !widget.route.isNoSelectedItem && + widget.itemIndex == widget.route.selectedIndex; child = InkWell( autofocus: isSelectedItem, enableFeedback: widget.enableFeedback, @@ -224,7 +231,10 @@ class _DropdownItemButtonState extends State<_DropdownItemButton> { onFocusChange: _handleFocusChange, borderRadius: menuItemStyle.borderRadius, overlayColor: menuItemStyle.overlayColor, - child: isSelectedItem ? menuItemStyle.selectedMenuItemBuilder?.call(context, child) ?? child : child, + child: isSelectedItem + ? menuItemStyle.selectedMenuItemBuilder?.call(context, child) ?? + child + : child, ); } child = FadeTransition(opacity: opacity, child: child);