diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index 2106e6fb87..561cbf2245 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -100,15 +100,109 @@ class _DropdownScrollBehavior extends ScrollBehavior { ScrollPhysics getScrollPhysics(BuildContext context) => const ClampingScrollPhysics(); } +// The widget that is the button wrapping the menu items. +class _DropdownMenuItemButton extends StatefulWidget { + const _DropdownMenuItemButton({ + Key key, + @required this.padding, + @required this.route, + @required this.buttonRect, + @required this.constraints, + @required this.itemIndex, + }) : super(key: key); + + final _DropdownRoute route; + final EdgeInsets padding; + final Rect buttonRect; + final BoxConstraints constraints; + final int itemIndex; + + @override + _DropdownMenuItemButtonState createState() => _DropdownMenuItemButtonState(); +} + +class _DropdownMenuItemButtonState extends State<_DropdownMenuItemButton> { + void _handleFocusChange(bool focused) { + bool inTraditionalMode; + switch (FocusManager.instance.highlightMode) { + case FocusHighlightMode.touch: + inTraditionalMode = false; + break; + case FocusHighlightMode.traditional: + inTraditionalMode = true; + break; + } + + if (focused && inTraditionalMode) { + final _MenuLimits menuLimits = widget.route.getMenuLimits( + widget.buttonRect, widget.constraints.maxHeight, widget.itemIndex); + widget.route.scrollController.animateTo( + menuLimits.scrollOffset, + curve: Curves.easeInOut, + duration: const Duration(milliseconds: 100), + ); + } + } + + void _handleOnTap() { + Navigator.pop( + context, + _DropdownRouteResult(widget.route.items[widget.itemIndex].item.value), + ); + } + + static final Map _webShortcuts ={ + LogicalKeySet(LogicalKeyboardKey.enter): const Intent(SelectAction.key), + }; + + @override + Widget build(BuildContext context) { + CurvedAnimation opacity; + final double unit = 0.5 / (widget.route.items.length + 1.5); + if (widget.itemIndex == widget.route.selectedIndex) { + opacity = CurvedAnimation(parent: widget.route.animation, curve: const Threshold(0.0)); + } else { + final double start = (0.5 + (widget.itemIndex + 1) * unit).clamp(0.0, 1.0); + final double end = (start + 1.5 * unit).clamp(0.0, 1.0); + opacity = CurvedAnimation(parent: widget.route.animation, curve: Interval(start, end)); + } + Widget child = FadeTransition( + opacity: opacity, + child: InkWell( + autofocus: widget.itemIndex == widget.route.selectedIndex, + child: Container( + padding: widget.padding, + child: widget.route.items[widget.itemIndex], + ), + onTap: _handleOnTap, + onFocusChange: _handleFocusChange, + ), + ); + if (kIsWeb) { + // On the web, enter doesn't select things, *except* in a - // element, which is what a dropdown emulates. - static final Map _webShortcuts ={ - LogicalKeySet(LogicalKeyboardKey.enter): const Intent(SelectAction.key), - }; - @override void initState() { super.initState(); @@ -156,40 +244,16 @@ class _DropdownMenuState extends State<_DropdownMenu> { assert(debugCheckHasMaterialLocalizations(context)); final MaterialLocalizations localizations = MaterialLocalizations.of(context); final _DropdownRoute route = widget.route; - final double unit = 0.5 / (route.items.length + 1.5); - final List children = []; - for (int itemIndex = 0; itemIndex < route.items.length; ++itemIndex) { - CurvedAnimation opacity; - if (itemIndex == route.selectedIndex) { - opacity = CurvedAnimation(parent: route.animation, curve: const Threshold(0.0)); - } else { - final double start = (0.5 + (itemIndex + 1) * unit).clamp(0.0, 1.0); - final double end = (start + 1.5 * unit).clamp(0.0, 1.0); - opacity = CurvedAnimation(parent: route.animation, curve: Interval(start, end)); - } - Widget child = FadeTransition( - opacity: opacity, - child: InkWell( - child: Container( - padding: widget.padding, - child: route.items[itemIndex], - ), - onTap: () => Navigator.pop( - context, - _DropdownRouteResult(route.items[itemIndex].item.value), - ), + final List children = [ + for (int itemIndex = 0; itemIndex < route.items.length; ++itemIndex) + _DropdownMenuItemButton( + route: widget.route, + padding: widget.padding, + buttonRect: widget.buttonRect, + constraints: widget.constraints, + itemIndex: itemIndex, ), - ); - if (kIsWeb) { - // On the web, enter doesn't select things, *except* in a