From 0a4618943c0360615c02443cecd95656eb2c7892 Mon Sep 17 00:00:00 2001 From: YeungKC Date: Tue, 2 Feb 2021 14:56:03 +0800 Subject: [PATCH] Improve Dropdown Menu (#74906) --- .../flutter/lib/src/material/dropdown.dart | 20 +++++++------------ .../flutter/test/material/dropdown_test.dart | 20 ++++++++++++++++++- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index 3fe86aeab2..89f781fc94 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -290,19 +290,13 @@ class _DropdownMenuState extends State<_DropdownMenu> { behavior: const _DropdownScrollBehavior(), child: PrimaryScrollController( controller: widget.route.scrollController!, - child: LayoutBuilder( - builder: (BuildContext context, BoxConstraints constraints) { - final double menuTotalHeight = widget.route.itemHeights.reduce((double total, double height) => total + height); - final bool isScrollable = kMaterialListPadding.vertical + menuTotalHeight > constraints.maxHeight; - return Scrollbar( - isAlwaysShown: isScrollable, - child: ListView( - padding: kMaterialListPadding, - shrinkWrap: true, - children: children, - ), - ); - }, + child: Scrollbar( + isAlwaysShown: true, + child: ListView( + padding: kMaterialListPadding, + shrinkWrap: true, + children: children, + ), ), ), ), diff --git a/packages/flutter/test/material/dropdown_test.dart b/packages/flutter/test/material/dropdown_test.dart index 2b2999a006..b90d03bcd1 100644 --- a/packages/flutter/test/material/dropdown_test.dart +++ b/packages/flutter/test/material/dropdown_test.dart @@ -2923,13 +2923,31 @@ void main() { testWidgets('Dropdown menu should persistently show a scrollbar if it is scrollable', (WidgetTester tester) async { await tester.pumpWidget(buildFrame( value: '0', + // menu is short enough to fit onto the screen. + items: List.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.generate(/*length=*/100, (int index) => index.toString()), onChanged: onChanged, )); await tester.tap(find.text('0')); 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(find.byType(Scrollbar), paints..rect()); });