From c3587c62e409b0b051a67bd533515955b817aece Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Fri, 17 Feb 2023 14:51:05 -0500 Subject: [PATCH] Add `InheritedTheme` support to `ScrollbarTheme` (#120970) --- .../lib/src/material/scrollbar_theme.dart | 7 +- .../test/material/menu_anchor_test.dart | 97 +++++++++++++++++++ .../test/material/popup_menu_test.dart | 89 +++++++++++++++++ 3 files changed, 192 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/scrollbar_theme.dart b/packages/flutter/lib/src/material/scrollbar_theme.dart index a3218285cd..b1d07888ce 100644 --- a/packages/flutter/lib/src/material/scrollbar_theme.dart +++ b/packages/flutter/lib/src/material/scrollbar_theme.dart @@ -299,7 +299,7 @@ bool? _lerpBool(bool? a, bool? b, double t) => t < 0.5 ? a : b; /// /// * [ScrollbarThemeData], which describes the configuration of a /// scrollbar theme. -class ScrollbarTheme extends InheritedWidget { +class ScrollbarTheme extends InheritedTheme { /// Constructs a scrollbar theme that configures all descendant [Scrollbar] /// widgets. const ScrollbarTheme({ @@ -324,6 +324,11 @@ class ScrollbarTheme extends InheritedWidget { return scrollbarTheme?.data ?? Theme.of(context).scrollbarTheme; } + @override + Widget wrap(BuildContext context, Widget child) { + return ScrollbarTheme(data: data, child: child); + } + @override bool updateShouldNotify(ScrollbarTheme oldWidget) => data != oldWidget.data; } diff --git a/packages/flutter/test/material/menu_anchor_test.dart b/packages/flutter/test/material/menu_anchor_test.dart index d3b1aa92a4..4fa005b5db 100644 --- a/packages/flutter/test/material/menu_anchor_test.dart +++ b/packages/flutter/test/material/menu_anchor_test.dart @@ -9,6 +9,8 @@ import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; +import '../rendering/mock_canvas.dart'; + void main() { late MenuController controller; String? focusedMenu; @@ -298,6 +300,101 @@ void main() { expect(iconRichText.text.style?.color, themeData.colorScheme.onSurface.withOpacity(0.38)); }); + testWidgets('Menu scrollbar inherits ScrollbarTheme', (WidgetTester tester) async { + const ScrollbarThemeData scrollbarTheme = ScrollbarThemeData( + thumbColor: MaterialStatePropertyAll(Color(0xffff0000)), + thumbVisibility: MaterialStatePropertyAll(true), + ); + await tester.pumpWidget( + MaterialApp( + theme: ThemeData(scrollbarTheme: scrollbarTheme), + home: Material( + child: MenuBar( + children: [ + SubmenuButton( + menuChildren: [ + MenuItemButton( + style: ButtonStyle( + minimumSize: MaterialStateProperty.all( + const Size.fromHeight(1000), + ), + ), + onPressed: () {}, + child: const Text( + 'Category', + ), + ), + ], + child: const Text( + 'Main Menu', + ), + ), + ], + ), + ), + ), + ); + + await tester.tap(find.text('Main Menu')); + await tester.pumpAndSettle(); + + expect(find.byType(Scrollbar), findsOneWidget); + // Test Scrollbar thumb color. + expect( + find.byType(Scrollbar), + paints..rrect(color: const Color(0xffff0000)), + ); + + // Close the menu. + await tester.tapAt(const Offset(10.0, 10.0)); + await tester.pumpAndSettle(); + + await tester.pumpWidget( + MaterialApp( + theme: ThemeData(scrollbarTheme: scrollbarTheme), + home: Material( + child: ScrollbarTheme( + data: scrollbarTheme.copyWith( + thumbColor: const MaterialStatePropertyAll(Color(0xff00ff00)), + ), + child: MenuBar( + children: [ + SubmenuButton( + menuChildren: [ + MenuItemButton( + style: ButtonStyle( + minimumSize: MaterialStateProperty.all( + const Size.fromHeight(1000), + ), + ), + onPressed: () {}, + child: const Text( + 'Category', + ), + ), + ], + child: const Text( + 'Main Menu', + ), + ), + ], + ), + ), + ), + ), + ); + + await tester.tap(find.text('Main Menu')); + await tester.pumpAndSettle(); + + expect(find.byType(Scrollbar), findsOneWidget); + // Scrollbar thumb color should be updated. + expect( + find.byType(Scrollbar), + paints..rrect(color: const Color(0xff00ff00)), + ); + }, variant: TargetPlatformVariant.desktop()); + group('Menu functions', () { testWidgets('basic menu structure', (WidgetTester tester) async { await tester.pumpWidget( diff --git a/packages/flutter/test/material/popup_menu_test.dart b/packages/flutter/test/material/popup_menu_test.dart index 3fcfd9abe6..9976201698 100644 --- a/packages/flutter/test/material/popup_menu_test.dart +++ b/packages/flutter/test/material/popup_menu_test.dart @@ -10,6 +10,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; +import '../rendering/mock_canvas.dart'; import '../widgets/semantics_tester.dart'; import 'feedback_tester.dart'; @@ -3098,6 +3099,94 @@ void main() { expect(nodeA().hasFocus, true); expect(nodeB().hasFocus, false); }); + + testWidgets('Popup menu scrollbar inherits ScrollbarTheme', (WidgetTester tester) async { + final Key popupButtonKey = UniqueKey(); + const ScrollbarThemeData scrollbarTheme = ScrollbarThemeData( + thumbColor: MaterialStatePropertyAll(Color(0xffff0000)), + thumbVisibility: MaterialStatePropertyAll(true), + ); + await tester.pumpWidget( + MaterialApp( + theme: ThemeData( + scrollbarTheme: scrollbarTheme, + useMaterial3: true, + ), + home: Material( + child: Column( + children: [ + PopupMenuButton( + key: popupButtonKey, + itemBuilder: (BuildContext context) { + return >[ + const PopupMenuItem( + height: 1000, + child: Text('Example'), + ), + ]; + }, + ), + ], + ), + ), + ) + ); + + await tester.tap(find.byKey(popupButtonKey)); + await tester.pumpAndSettle(); + + expect(find.byType(Scrollbar), findsOneWidget); + // Test Scrollbar thumb color. + expect( + find.byType(Scrollbar), + paints..rrect(color: const Color(0xffff0000)), + ); + + // Close the menu. + await tester.tapAt(const Offset(20.0, 20.0)); + await tester.pumpAndSettle(); + + // Test local ScrollbarTheme overrides global ScrollbarTheme. + await tester.pumpWidget( + MaterialApp( + theme: ThemeData( + scrollbarTheme: scrollbarTheme, + useMaterial3: true, + ), + home: Material( + child: Column( + children: [ + ScrollbarTheme( + data: scrollbarTheme.copyWith( + thumbColor: const MaterialStatePropertyAll(Color(0xff0000ff)), + ), + child: PopupMenuButton( + key: popupButtonKey, + itemBuilder: (BuildContext context) { + return >[ + const PopupMenuItem( + height: 1000, + child: Text('Example'), + ), + ]; + }, + ), + ), + ], + ), + ), + ) + ); + await tester.tap(find.byKey(popupButtonKey)); + await tester.pumpAndSettle(); + + expect(find.byType(Scrollbar), findsOneWidget); + // Scrollbar thumb color should be updated. + expect( + find.byType(Scrollbar), + paints..rrect(color: const Color(0xff0000ff)), + ); + }, variant: TargetPlatformVariant.desktop()); } class TestApp extends StatelessWidget {