Unverified Commit 0a461894 authored by YeungKC's avatar YeungKC Committed by GitHub

Improve Dropdown Menu (#74906)

parent 9fd3c22e
...@@ -290,19 +290,13 @@ class _DropdownMenuState<T> extends State<_DropdownMenu<T>> { ...@@ -290,19 +290,13 @@ class _DropdownMenuState<T> extends State<_DropdownMenu<T>> {
behavior: const _DropdownScrollBehavior(), behavior: const _DropdownScrollBehavior(),
child: PrimaryScrollController( child: PrimaryScrollController(
controller: widget.route.scrollController!, controller: widget.route.scrollController!,
child: LayoutBuilder( child: Scrollbar(
builder: (BuildContext context, BoxConstraints constraints) { isAlwaysShown: true,
final double menuTotalHeight = widget.route.itemHeights.reduce((double total, double height) => total + height); child: ListView(
final bool isScrollable = kMaterialListPadding.vertical + menuTotalHeight > constraints.maxHeight; padding: kMaterialListPadding,
return Scrollbar( shrinkWrap: true,
isAlwaysShown: isScrollable, children: children,
child: ListView( ),
padding: kMaterialListPadding,
shrinkWrap: true,
children: children,
),
);
},
), ),
), ),
), ),
......
...@@ -2923,13 +2923,31 @@ void main() { ...@@ -2923,13 +2923,31 @@ void main() {
testWidgets('Dropdown menu should persistently show a scrollbar if it is scrollable', (WidgetTester tester) async { testWidgets('Dropdown menu should persistently show a scrollbar if it is scrollable', (WidgetTester tester) async {
await tester.pumpWidget(buildFrame( await tester.pumpWidget(buildFrame(
value: '0', value: '0',
// menu is short enough to fit onto the screen.
items: List<String>.generate(/*length=*/10, (int index) => index.toString()),
onChanged: onChanged,
));
await tester.tap(find.text('0'));
await tester.pumpAndSettle();
ScrollController scrollController = PrimaryScrollController.of(tester.element(find.byType(ListView)))!;
// The scrollbar shouldn't show if the list fits into the screen.
expect(scrollController.position.maxScrollExtent, 0);
expect(find.byType(Scrollbar), isNot(paints..rect()));
await tester.tap(find.text('0').last);
await tester.pumpAndSettle();
await tester.pumpWidget(buildFrame(
value: '0',
// menu is too long to fit onto the screen.
items: List<String>.generate(/*length=*/100, (int index) => index.toString()), items: List<String>.generate(/*length=*/100, (int index) => index.toString()),
onChanged: onChanged, onChanged: onChanged,
)); ));
await tester.tap(find.text('0')); await tester.tap(find.text('0'));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
final ScrollController scrollController = PrimaryScrollController.of(tester.element(find.byType(ListView)))!; scrollController = PrimaryScrollController.of(tester.element(find.byType(ListView)))!;
// The scrollbar is shown when the list is longer than the height of the screen.
expect(scrollController.position.maxScrollExtent > 0, isTrue); expect(scrollController.position.maxScrollExtent > 0, isTrue);
expect(find.byType(Scrollbar), paints..rect()); expect(find.byType(Scrollbar), paints..rect());
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment