From 5f39487740c9b0db2ccc1bcbdca5354941c67b4d Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Wed, 12 Jun 2019 17:55:31 -0700 Subject: [PATCH] Don't show scrollbar if there isn't enough content (#34175) --- .../flutter/lib/src/cupertino/scrollbar.dart | 5 +++ .../flutter/lib/src/material/scrollbar.dart | 5 +++ .../test/cupertino/scrollbar_paint_test.dart | 42 ++++++++++++++++++- .../test/material/scrollbar_paint_test.dart | 28 +++++++++++++ .../flutter/test/material/scrollbar_test.dart | 5 ++- 5 files changed, 82 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/scrollbar.dart b/packages/flutter/lib/src/cupertino/scrollbar.dart index 9a54fb2a06..119aecd14b 100644 --- a/packages/flutter/lib/src/cupertino/scrollbar.dart +++ b/packages/flutter/lib/src/cupertino/scrollbar.dart @@ -103,6 +103,11 @@ class _CupertinoScrollbarState extends State with TickerProv } bool _handleScrollNotification(ScrollNotification notification) { + final ScrollMetrics metrics = notification.metrics; + if (metrics.maxScrollExtent <= metrics.minScrollExtent) { + return false; + } + if (notification is ScrollUpdateNotification || notification is OverscrollNotification) { // Any movements always makes the scrollbar start showing up. diff --git a/packages/flutter/lib/src/material/scrollbar.dart b/packages/flutter/lib/src/material/scrollbar.dart index 60dcfcb0d5..cfa1aee47b 100644 --- a/packages/flutter/lib/src/material/scrollbar.dart +++ b/packages/flutter/lib/src/material/scrollbar.dart @@ -109,6 +109,11 @@ class _ScrollbarState extends State with TickerProviderStateMixin { } bool _handleScrollNotification(ScrollNotification notification) { + final ScrollMetrics metrics = notification.metrics; + if (metrics.maxScrollExtent <= metrics.minScrollExtent) { + return false; + } + // iOS sub-delegates to the CupertinoScrollbar instead and doesn't handle // scroll notifications here. if (_currentPlatform != TargetPlatform.iOS diff --git a/packages/flutter/test/cupertino/scrollbar_paint_test.dart b/packages/flutter/test/cupertino/scrollbar_paint_test.dart index b82728f395..3de11ec1d7 100644 --- a/packages/flutter/test/cupertino/scrollbar_paint_test.dart +++ b/packages/flutter/test/cupertino/scrollbar_paint_test.dart @@ -77,7 +77,7 @@ void main() { final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(ListView))); await gesture.moveBy(_kGestureOffset); // Move back to original position. - await gesture.moveBy(Offset.zero.translate(-_kGestureOffset.dx, -_kGestureOffset.dy)); + await gesture.moveBy(Offset(-_kGestureOffset.dx, -_kGestureOffset.dy)); await tester.pump(); await tester.pump(const Duration(milliseconds: 500)); @@ -96,4 +96,44 @@ void main() { ), )); }); + + testWidgets("should not paint when there isn't enough space", (WidgetTester tester) async { + await tester.pumpWidget( + CupertinoApp( + home: MediaQuery( + data: const MediaQueryData( + padding: EdgeInsets.fromLTRB(0, 20, 0, 34), + ), + child: CupertinoPageScaffold( + navigationBar: const CupertinoNavigationBar( + middle: Text('Title'), + backgroundColor: Color(0x11111111), + ), + child: CupertinoScrollbar( + child: ListView( + physics: const AlwaysScrollableScrollPhysics(parent: BouncingScrollPhysics()), + children: const [SizedBox(width: 10, height: 10)], + ), + ), + ), + ), + ), + ); + final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(ListView))); + await gesture.moveBy(_kGestureOffset); + // Move back to original position. + await gesture.moveBy(Offset(-_kGestureOffset.dx, -_kGestureOffset.dy)); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 500)); + + expect(find.byType(CupertinoScrollbar), isNot(paints..rrect())); + + // The scrollbar should not appear even when overscrolled. + final TestGesture overscrollGesture = await tester.startGesture(tester.getCenter(find.byType(ListView))); + await overscrollGesture.moveBy(_kGestureOffset); + + await tester.pump(); + await tester.pump(const Duration(milliseconds: 500)); + expect(find.byType(CupertinoScrollbar), isNot(paints..rrect())); + }); } diff --git a/packages/flutter/test/material/scrollbar_paint_test.dart b/packages/flutter/test/material/scrollbar_paint_test.dart index e1e6d81045..c2bf525446 100644 --- a/packages/flutter/test/material/scrollbar_paint_test.dart +++ b/packages/flutter/test/material/scrollbar_paint_test.dart @@ -77,4 +77,32 @@ void main() { ), )); }); + + testWidgets("should not paint when there isn't enough space", (WidgetTester tester) async { + await tester.pumpWidget(MaterialApp( + home: MediaQuery( + data: const MediaQueryData( + padding: EdgeInsets.fromLTRB(0, 20, 0, 34) + ), + child: Scaffold( + appBar: AppBar(title: const Text('Title')), + body: Scrollbar( + child: ListView( + children: const [SizedBox(width: 40, height: 40)] + ), + ), + ), + ), + )); + + final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(ListView))); + // On Android it should not overscroll. + await gesture.moveBy(const Offset(0, 100)); + // Trigger fade in animation. + await tester.pump(); + await tester.pump(const Duration(milliseconds: 500)); + + expect(find.byType(Scrollbar), isNot(paints..rect())); + }); + } diff --git a/packages/flutter/test/material/scrollbar_test.dart b/packages/flutter/test/material/scrollbar_test.dart index 1370ab39d1..e523865da4 100644 --- a/packages/flutter/test/material/scrollbar_test.dart +++ b/packages/flutter/test/material/scrollbar_test.dart @@ -115,8 +115,9 @@ void main() { final List invocations = []; final TestCanvas canvas = TestCanvas(invocations); scrollPainter.paint(canvas, const Size(10.0, 100.0)); - final Rect thumbRect = invocations.single.positionalArguments[0]; - expect(thumbRect.isFinite, isTrue); + + // Scrollbar is not supposed to draw anything if there isn't enough content. + expect(invocations.isEmpty, isTrue); }); testWidgets('Adaptive scrollbar', (WidgetTester tester) async {