Unverified Commit c3587c62 authored by Taha Tesser's avatar Taha Tesser Committed by GitHub

Add `InheritedTheme` support to `ScrollbarTheme` (#120970)

parent 51712b90
......@@ -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;
}
......@@ -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?>(Color(0xffff0000)),
thumbVisibility: MaterialStatePropertyAll<bool?>(true),
);
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(scrollbarTheme: scrollbarTheme),
home: Material(
child: MenuBar(
children: <Widget>[
SubmenuButton(
menuChildren: <Widget>[
MenuItemButton(
style: ButtonStyle(
minimumSize: MaterialStateProperty.all<Size>(
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?>(Color(0xff00ff00)),
),
child: MenuBar(
children: <Widget>[
SubmenuButton(
menuChildren: <Widget>[
MenuItemButton(
style: ButtonStyle(
minimumSize: MaterialStateProperty.all<Size>(
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(
......
......@@ -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?>(Color(0xffff0000)),
thumbVisibility: MaterialStatePropertyAll<bool?>(true),
);
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(
scrollbarTheme: scrollbarTheme,
useMaterial3: true,
),
home: Material(
child: Column(
children: <Widget>[
PopupMenuButton<void>(
key: popupButtonKey,
itemBuilder: (BuildContext context) {
return <PopupMenuEntry<void>>[
const PopupMenuItem<void>(
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: <Widget>[
ScrollbarTheme(
data: scrollbarTheme.copyWith(
thumbColor: const MaterialStatePropertyAll<Color?>(Color(0xff0000ff)),
),
child: PopupMenuButton<void>(
key: popupButtonKey,
itemBuilder: (BuildContext context) {
return <PopupMenuEntry<void>>[
const PopupMenuItem<void>(
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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment