Skip to content

Commit 95c83e5

Browse files
authored
[material_ui] Fix MenuAnchor scrollbar safe-area padding (flutter#12470)
Material `Scrollbar` uses the surrounding `MediaQuery.padding` as painter padding when no explicit padding is provided. A `MenuAnchor` overlay is already positioned within the safe area, so inheriting the view padding again shortens the scrollbar track and prevents the thumb from reaching its visual end. This change removes safe-area padding only for the menu's `Scrollbar`, then restores the original `MediaQueryData` below it so custom menu children continue to observe the same media query values as before. It also adds a regression test and a patch changelog entry. Fixes flutter#188155. This supersedes flutter#191085, which cannot land in `flutter/flutter` because Material framework changes have moved to `material_ui`. Validation: - The regression test failed before the source change with a 24 logical pixel bottom scrollbar padding and passes after the change. - `flutter test test/menu_anchor_test.dart` (171 passed, 1 skipped) - `flutter analyze --no-pub` - `flutter_plugin_tools format --packages=material_ui` - `flutter_plugin_tools validate --packages=material_ui --check-for-missing-changes` ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 6c3361d commit 95c83e5

3 files changed

Lines changed: 112 additions & 11 deletions

File tree

‎packages/material_ui/lib/src/menu_anchor.dart‎

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3716,6 +3716,18 @@ class _MenuPanelState extends State<_MenuPanel> {
37163716
final EdgeInsetsGeometry padding =
37173717
resolve<EdgeInsetsGeometry?>((MenuStyle? style) => style?.padding) ?? EdgeInsets.zero;
37183718
final Offset densityAdjustment = visualDensity.baseSizeAdjustment;
3719+
// Scrollbar uses the `MediaQuery.padding` as its painter padding by
3720+
// default. Menu overlays are already positioned within the safe area, so
3721+
// applying that padding again shortens the scrollbar track and stops the
3722+
// thumb from reaching the end of the menu. Remove the padding only for the
3723+
// Scrollbar and restore the ambient MediaQuery for menu children.
3724+
final MediaQueryData ambientMediaQuery = MediaQuery.of(context);
3725+
final MediaQueryData scrollbarMediaQuery = ambientMediaQuery.removePadding(
3726+
removeLeft: true,
3727+
removeTop: true,
3728+
removeRight: true,
3729+
removeBottom: true,
3730+
);
37193731
// Per the Material Design team: don't allow the VisualDensity
37203732
// adjustment to reduce the width of the left/right padding. If we
37213733
// did, VisualDensity.compact, the default for desktop/web, would
@@ -3775,17 +3787,23 @@ class _MenuPanelState extends State<_MenuPanel> {
37753787
.copyWith(scrollbars: false, overscroll: false, physics: const ClampingScrollPhysics()),
37763788
child: PrimaryScrollController(
37773789
controller: scrollController,
3778-
child: Scrollbar(
3779-
thumbVisibility: displayScrollbar,
3780-
child: SingleChildScrollView(
3781-
controller: scrollController,
3782-
scrollDirection: widget.orientation,
3783-
child: Flex(
3784-
crossAxisAlignment: CrossAxisAlignment.start,
3785-
textDirection: Directionality.of(context),
3786-
direction: widget.orientation,
3787-
mainAxisSize: MainAxisSize.min,
3788-
children: children,
3790+
child: MediaQuery(
3791+
data: scrollbarMediaQuery,
3792+
child: Scrollbar(
3793+
thumbVisibility: displayScrollbar,
3794+
child: MediaQuery(
3795+
data: ambientMediaQuery,
3796+
child: SingleChildScrollView(
3797+
controller: scrollController,
3798+
scrollDirection: widget.orientation,
3799+
child: Flex(
3800+
crossAxisAlignment: CrossAxisAlignment.start,
3801+
textDirection: Directionality.of(context),
3802+
direction: widget.orientation,
3803+
mainAxisSize: MainAxisSize.min,
3804+
children: children,
3805+
),
3806+
),
37893807
),
37903808
),
37913809
),
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fixes `MenuAnchor` scrollbars inheriting system safe-area padding.
3+
version: patch

‎packages/material_ui/test/menu_anchor_test.dart‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,86 @@ void main() {
566566
expect(find.byType(Scrollbar).last, paints..rrect(color: const Color(0xff00ff00)));
567567
}, variant: TargetPlatformVariant.desktop());
568568

569+
// Regression test for https://github.com/flutter/flutter/issues/188155.
570+
testWidgets('Menu scrollbar does not inherit MediaQuery padding', (WidgetTester tester) async {
571+
addTearDown(tester.view.reset);
572+
tester.view.devicePixelRatio = 1.0;
573+
tester.view.padding = const FakeViewPadding(left: 8.0, top: 16.0, right: 32.0, bottom: 24.0);
574+
EdgeInsets? menuItemPadding;
575+
576+
await tester.pumpWidget(
577+
MaterialApp(
578+
home: Material(
579+
child: MenuAnchor(
580+
controller: controller,
581+
style: const MenuStyle(
582+
maximumSize: WidgetStatePropertyAll<Size>(Size.fromHeight(100.0)),
583+
),
584+
menuChildren: <Widget>[
585+
Builder(
586+
builder: (BuildContext context) {
587+
menuItemPadding = MediaQuery.paddingOf(context);
588+
return MenuItemButton(onPressed: () {}, child: const Text('Item 0'));
589+
},
590+
),
591+
for (int i = 1; i < 4; i++) MenuItemButton(onPressed: () {}, child: Text('Item $i')),
592+
],
593+
builder: (BuildContext context, MenuController controller, Widget? child) {
594+
return TextButton(onPressed: controller.open, child: const Text('Open menu'));
595+
},
596+
),
597+
),
598+
),
599+
);
600+
601+
await tester.tap(find.text('Open menu'));
602+
await tester.pumpAndSettle();
603+
604+
final CustomPaint scrollbarPaint = tester.widget<CustomPaint>(
605+
find.descendant(
606+
of: find.byType(Scrollbar),
607+
matching: find.byWidgetPredicate(
608+
(Widget widget) => widget is CustomPaint && widget.foregroundPainter is ScrollbarPainter,
609+
),
610+
),
611+
);
612+
// The scrollbar ignores the view padding, but the menu children still
613+
// observe it.
614+
final scrollbarPainter = scrollbarPaint.foregroundPainter! as ScrollbarPainter;
615+
expect(scrollbarPainter.padding, EdgeInsets.zero);
616+
expect(menuItemPadding, const EdgeInsets.fromLTRB(8.0, 16.0, 32.0, 24.0));
617+
618+
// The menu scrolls all the way to the bottom and the scrollbar thumb
619+
// reaches the end of its track instead of stopping short of it.
620+
final ScrollableState scrollable = tester.state<ScrollableState>(
621+
find.descendant(of: find.byType(Scrollbar), matching: find.byType(Scrollable)),
622+
);
623+
expect(scrollable.position.maxScrollExtent, greaterThan(0.0));
624+
await tester.drag(find.byType(Scrollbar), const Offset(0.0, -200.0));
625+
await tester.pumpAndSettle();
626+
expect(scrollable.position.pixels, scrollable.position.maxScrollExtent);
627+
628+
// The thumb is painted at the very end of the track rather than stopping
629+
// short of it by the height of the removed bottom padding.
630+
final double scrollbarExtent = tester.getSize(find.byType(Scrollbar)).height;
631+
expect(
632+
find.byType(Scrollbar),
633+
paints..something((Symbol method, List<dynamic> arguments) {
634+
// The thumb is a rect on Android and a rounded rect elsewhere. The
635+
// track shares the same shape but always starts at the top, so only
636+
// the thumb is considered here.
637+
final Rect? shape = switch (method) {
638+
#drawRect => arguments[0] as Rect,
639+
#drawRRect => (arguments[0] as RRect).outerRect,
640+
_ => null,
641+
};
642+
return shape != null &&
643+
shape.top > 0.0 &&
644+
(shape.bottom - scrollbarExtent).abs() < precisionErrorTolerance;
645+
}),
646+
);
647+
});
648+
569649
testWidgets('Focus is returned to previous focus before invoking onPressed', (
570650
WidgetTester tester,
571651
) async {

0 commit comments

Comments
 (0)