From 41b1aea281e23efe27ff880716a85a615de6d5d2 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Mon, 26 Feb 2024 12:34:27 +0200 Subject: [PATCH] Fix `Scrollbar.thickness` property is ignored when the `Scrollbar` is hovered (#144012) fixes [`Scrollbar.thickness` property is ignored when the `Scrollbar` is hovered](https://github.com/flutter/flutter/issues/143881) ### Code sample
expand to view the code sample ```dart import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { final ScrollController scrollController = ScrollController(); return MaterialApp( debugShowCheckedModeBanner: false, home: Material( child: ScrollConfiguration( behavior: const NoScrollbarBehavior(), child: ScrollbarTheme( data: ScrollbarThemeData( thickness: MaterialStateProperty.all(25.0), showTrackOnHover: true, ), child: Scrollbar( thickness: 50.0, thumbVisibility: true, radius: const Radius.circular(3.0), controller: scrollController, child: SingleChildScrollView( controller: scrollController, child: const SizedBox(width: 4000.0, height: 4000.0), ), ), ), ), ), ); } } class NoScrollbarBehavior extends ScrollBehavior { const NoScrollbarBehavior(); @override Widget buildScrollbar( BuildContext context, Widget child, ScrollableDetails details) => child; } ```
### Preview | Before | After | | --------------- | --------------- | | | | --- .../flutter/lib/src/material/scrollbar.dart | 3 ++- .../test/material/scrollbar_theme_test.dart | 17 +++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/flutter/lib/src/material/scrollbar.dart b/packages/flutter/lib/src/material/scrollbar.dart index c274695eed3..561c3917dd4 100644 --- a/packages/flutter/lib/src/material/scrollbar.dart +++ b/packages/flutter/lib/src/material/scrollbar.dart @@ -326,7 +326,8 @@ class _MaterialScrollbarState extends RawScrollbarState<_MaterialScrollbar> { MaterialStateProperty get _thickness { return MaterialStateProperty.resolveWith((Set states) { if (states.contains(MaterialState.hovered) && _trackVisibility.resolve(states)) { - return _scrollbarTheme.thickness?.resolve(states) + return widget.thickness + ?? _scrollbarTheme.thickness?.resolve(states) ?? _kScrollbarThicknessWithTrack; } // The default scrollbar thickness is smaller on mobile. diff --git a/packages/flutter/test/material/scrollbar_theme_test.dart b/packages/flutter/test/material/scrollbar_theme_test.dart index 635fa6fb2f5..642cab7aa68 100644 --- a/packages/flutter/test/material/scrollbar_theme_test.dart +++ b/packages/flutter/test/material/scrollbar_theme_test.dart @@ -362,6 +362,7 @@ void main() { testWidgets('Scrollbar widget properties take priority over theme', (WidgetTester tester) async { const double thickness = 4.0; + const double edgeMargin = 2.0; const bool showTrackOnHover = true; const Radius radius = Radius.circular(3.0); final ScrollController scrollController = ScrollController(); @@ -394,14 +395,14 @@ void main() { find.byType(Scrollbar), paints..rrect( rrect: RRect.fromRectAndRadius( - const Rect.fromLTRB(794.0, 0.0, 798.0, 90.0), + const Rect.fromLTRB(800 - thickness - edgeMargin, 0.0, 798.0, 90.0), const Radius.circular(3.0), ), color: _kDefaultIdleThumbColor, ), ); - // Drag scrollbar behavior + // Drag scrollbar behavior. const double scrollAmount = 10.0; final TestGesture dragScrollbarGesture = await tester.startGesture(const Offset(797.0, 45.0)); await tester.pumpAndSettle(); @@ -410,7 +411,7 @@ void main() { find.byType(Scrollbar), paints..rrect( rrect: RRect.fromRectAndRadius( - const Rect.fromLTRB(794.0, 0.0, 798.0, 90.0), + const Rect.fromLTRB(800 - thickness - edgeMargin, 0.0, 798.0, 90.0), const Radius.circular(3.0), ), // Drag color @@ -423,7 +424,7 @@ void main() { await dragScrollbarGesture.up(); await tester.pumpAndSettle(); - // Hover scrollbar behavior + // Hover scrollbar behavior. final TestGesture gesture = await tester.createGesture(kind: ui.PointerDeviceKind.mouse); await gesture.addPointer(); await gesture.moveTo(const Offset(794.0, 5.0)); @@ -433,18 +434,18 @@ void main() { find.byType(Scrollbar), paints ..rect( - rect: const Rect.fromLTRB(784.0, 0.0, 800.0, 600.0), + rect: const Rect.fromLTRB(792.0, 0.0, 800.0, 600.0), color: const Color(0x08000000), ) ..line( - p1: const Offset(784.0, 0.0), - p2: const Offset(784.0, 600.0), + p1: const Offset(792.0, 0.0), + p2: const Offset(792.0, 600.0), strokeWidth: 1.0, color: const Color(0x1a000000), ) ..rrect( rrect: RRect.fromRectAndRadius( - const Rect.fromLTRB(786.0, 10.0, 798.0, 100.0), + const Rect.fromLTRB(800 - thickness - edgeMargin, 10.0, 798.0, 100.0), const Radius.circular(3.0), ), // Hover color