From e854b8cc889f63773d074afb2d2b04b56e54f751 Mon Sep 17 00:00:00 2001 From: Kate Lovett Date: Thu, 27 Jan 2022 18:55:22 -0600 Subject: [PATCH] Update RawScrollbar to support the track (#97335) --- .../flutter/lib/src/material/scrollbar.dart | 10 +-- .../flutter/lib/src/widgets/scrollbar.dart | 41 ++++++++++++ .../flutter/test/widgets/scrollbar_test.dart | 67 +++++++++++++++++++ 3 files changed, 113 insertions(+), 5 deletions(-) diff --git a/packages/flutter/lib/src/material/scrollbar.dart b/packages/flutter/lib/src/material/scrollbar.dart index 90d00c59e8..dc564cd68f 100644 --- a/packages/flutter/lib/src/material/scrollbar.dart +++ b/packages/flutter/lib/src/material/scrollbar.dart @@ -142,7 +142,7 @@ class Scrollbar extends StatelessWidget { ) final bool? isAlwaysShown; - /// Controls the track visibility. + /// {@macro flutter.widgets.Scrollbar.trackVisibility} /// /// If this property is null, then [ScrollbarThemeData.trackVisibility] of /// [ThemeData.scrollbarTheme] is used. If that is also null, the default value @@ -246,7 +246,7 @@ class _MaterialScrollbar extends RawScrollbar { required Widget child, ScrollController? controller, bool? thumbVisibility, - this.trackVisibility, + bool? trackVisibility, this.showTrackOnHover, this.hoverThickness, double? thickness, @@ -261,6 +261,7 @@ class _MaterialScrollbar extends RawScrollbar { thumbVisibility: thumbVisibility, thickness: thickness, radius: radius, + trackVisibility: trackVisibility, fadeDuration: _kScrollbarFadeDuration, timeToFade: _kScrollbarTimeToFade, pressDuration: Duration.zero, @@ -269,7 +270,6 @@ class _MaterialScrollbar extends RawScrollbar { scrollbarOrientation: scrollbarOrientation, ); - final bool? trackVisibility; final bool? showTrackOnHover; final double? hoverThickness; @@ -350,7 +350,7 @@ class _MaterialScrollbarState extends RawScrollbarState<_MaterialScrollbar> { final Color onSurface = _colorScheme.onSurface; final Brightness brightness = _colorScheme.brightness; return MaterialStateProperty.resolveWith((Set states) { - if (_trackVisibility.resolve(states)) { + if (showScrollbar && _trackVisibility.resolve(states)) { return _scrollbarTheme.trackColor?.resolve(states) ?? (brightness == Brightness.light ? onSurface.withOpacity(0.03) @@ -364,7 +364,7 @@ class _MaterialScrollbarState extends RawScrollbarState<_MaterialScrollbar> { final Color onSurface = _colorScheme.onSurface; final Brightness brightness = _colorScheme.brightness; return MaterialStateProperty.resolveWith((Set states) { - if (_trackVisibility.resolve(states)) { + if (showScrollbar && _trackVisibility.resolve(states)) { return _scrollbarTheme.trackBorderColor?.resolve(states) ?? (brightness == Brightness.light ? onSurface.withOpacity(0.1) diff --git a/packages/flutter/lib/src/widgets/scrollbar.dart b/packages/flutter/lib/src/widgets/scrollbar.dart index 57b4c98e9c..12d7dd94b3 100644 --- a/packages/flutter/lib/src/widgets/scrollbar.dart +++ b/packages/flutter/lib/src/widgets/scrollbar.dart @@ -875,6 +875,9 @@ class RawScrollbar extends StatefulWidget { this.thumbColor, this.minThumbLength = _kMinThumbExtent, this.minOverscrollLength, + this.trackVisibility, + this.trackColor, + this.trackBorderColor, this.fadeDuration = _kScrollbarFadeDuration, this.timeToFade = _kScrollbarTimeToFade, this.pressDuration = Duration.zero, @@ -894,6 +897,10 @@ class RawScrollbar extends StatefulWidget { 'Scrollbar thumb appearance should only be controlled with thumbVisibility, ' 'isAlwaysShown is deprecated.' ), + assert( + !((thumbVisibility == false || isAlwaysShown == false) && trackVisibility == true), + 'A scrollbar track cannot be drawn without a scrollbar thumb.' + ), assert(minThumbLength != null), assert(minThumbLength >= 0), assert(minOverscrollLength == null || minOverscrollLength <= minThumbLength), @@ -1199,6 +1206,36 @@ class RawScrollbar extends StatefulWidget { /// When null, it will default to the value of [minThumbLength]. final double? minOverscrollLength; + /// {@template flutter.widgets.Scrollbar.trackVisibility} + /// Indicates that the scrollbar track should be visible. + /// + /// When true, the scrollbar track will always be visible so long as the thumb + /// is visible. If the scrollbar thumb is not visible, the track will not be + /// visible either. + /// + /// Defaults to false when null. + /// {@endtemplate} + /// + /// Subclass [Scrollbar] can hide and show the scrollbar thumb in response to + /// [MaterialState]s by using [ScrollbarThemeData.trackVisibility]. + final bool? trackVisibility; + + /// The color of the scrollbar track. + /// + /// The scrollbar track will only be visible when [trackVisibility] and + /// [thumbVisibility] are true. + /// + /// If null, defaults to Color(0x08000000). + final Color? trackColor; + + /// The color of the scrollbar track's border. + /// + /// The scrollbar track will only be visible when [trackVisibility] and + /// [thumbVisibility] are true. + /// + /// If null, defaults to Color(0x1a000000). + final Color? trackBorderColor; + /// The [Duration] of the fade animation. /// /// Cannot be null, defaults to a [Duration] of 300 milliseconds. @@ -1304,6 +1341,8 @@ class RawScrollbarState extends State with TickerProv @protected bool get showScrollbar => widget.isAlwaysShown ?? widget.thumbVisibility ?? false; + bool get _showTrack => showScrollbar && (widget.trackVisibility ?? false); + /// Overridable getter to indicate is gestures should be enabled on the /// scrollbar. /// @@ -1470,6 +1509,8 @@ class RawScrollbarState extends State with TickerProv void updateScrollbarPainter() { scrollbarPainter ..color = widget.thumbColor ?? const Color(0x66BCBCBC) + ..trackColor = _showTrack ? const Color(0x08000000) : const Color(0x00000000) + ..trackBorderColor = _showTrack ? const Color(0x1a000000) : const Color(0x00000000) ..textDirection = Directionality.of(context) ..thickness = widget.thickness ?? _kScrollbarThickness ..radius = widget.radius diff --git a/packages/flutter/test/widgets/scrollbar_test.dart b/packages/flutter/test/widgets/scrollbar_test.dart index c0e3ccce4d..d23a0521a7 100644 --- a/packages/flutter/test/widgets/scrollbar_test.dart +++ b/packages/flutter/test/widgets/scrollbar_test.dart @@ -2245,4 +2245,71 @@ void main() { expect(painter.shouldRepaint(createPainter(minOverscrollLength: 1.0)), true); expect(painter.shouldRepaint(createPainter(scrollbarOrientation: ScrollbarOrientation.bottom)), true); }); + + testWidgets('Scrollbar track can be drawn', (WidgetTester tester) async { + final ScrollController scrollController = ScrollController(); + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: MediaQuery( + data: const MediaQueryData(), + child: PrimaryScrollController( + controller: scrollController, + child: RawScrollbar( + thumbVisibility: true, + trackVisibility: true, + controller: scrollController, + child: const SingleChildScrollView( + child: SizedBox(width: 4000.0, height: 4000.0), + ), + ), + ), + ), + ), + ); + await tester.pumpAndSettle(); + expect(scrollController.offset, 0.0); + expect( + find.byType(RawScrollbar), + paints + ..rect( + rect: const Rect.fromLTRB(794.0, 0.0, 800.0, 600.0), + color: const Color(0x08000000), + ) + ..line( + p1: const Offset(794.0, 0.0), + p2: const Offset(794.0, 600.0), + strokeWidth: 1.0, + color: const Color(0x1a000000), + ) + ..rect( + rect: const Rect.fromLTRB(794.0, 0.0, 800.0, 90.0), + color: const Color(0x66BCBCBC), + ), + ); + }); + + testWidgets('Scrollbar asserts that a visible track has a visible thumb', (WidgetTester tester) async { + final ScrollController scrollController = ScrollController(); + Widget _buildApp() { + return Directionality( + textDirection: TextDirection.ltr, + child: MediaQuery( + data: const MediaQueryData(), + child: PrimaryScrollController( + controller: scrollController, + child: RawScrollbar( + thumbVisibility: false, + trackVisibility: true, + controller: scrollController, + child: const SingleChildScrollView( + child: SizedBox(width: 4000.0, height: 4000.0), + ), + ), + ), + ), + ); + } + expect(() => tester.pumpWidget(_buildApp()), throwsAssertionError); + }); }