Improve Dropdown Menu (#74906)

This commit is contained in:
YeungKC 2021-02-02 14:56:03 +08:00 committed by GitHub
parent 9fd3c22eb9
commit 0a4618943c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 14 deletions

View file

@ -290,19 +290,13 @@ class _DropdownMenuState<T> extends State<_DropdownMenu<T>> {
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: Scrollbar(
isAlwaysShown: true,
child: ListView(
padding: kMaterialListPadding,
shrinkWrap: true,
children: children,
),
);
},
),
),
),

View file

@ -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<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()),
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());
});