mirror of
https://github.com/flutter/flutter
synced 2024-10-13 11:42:54 +00:00
Improve Dropdown Menu (#74906)
This commit is contained in:
parent
9fd3c22eb9
commit
0a4618943c
|
@ -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: ListView(
|
||||
padding: kMaterialListPadding,
|
||||
shrinkWrap: true,
|
||||
children: children,
|
||||
),
|
||||
);
|
||||
},
|
||||
child: Scrollbar(
|
||||
isAlwaysShown: true,
|
||||
child: ListView(
|
||||
padding: kMaterialListPadding,
|
||||
shrinkWrap: true,
|
||||
children: children,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
|
|
@ -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());
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue