From 12b72919f977264a36c4a839ecb5a6eeb91c6757 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Sun, 7 Nov 2021 10:17:34 +0200 Subject: [PATCH] [Cupertino] fix dark mode for `ContextMenuAction` (#92480) --- .../src/cupertino/context_menu_action.dart | 19 ++++-- .../cupertino/context_menu_action_test.dart | 62 ++++++++++++------- 2 files changed, 54 insertions(+), 27 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/context_menu_action.dart b/packages/flutter/lib/src/cupertino/context_menu_action.dart index 478fe2e7de..c0dae6b8dc 100644 --- a/packages/flutter/lib/src/cupertino/context_menu_action.dart +++ b/packages/flutter/lib/src/cupertino/context_menu_action.dart @@ -49,8 +49,14 @@ class CupertinoContextMenuAction extends StatefulWidget { } class _CupertinoContextMenuActionState extends State { - static const Color _kBackgroundColor = Color(0xFFEEEEEE); - static const Color _kBackgroundColorPressed = Color(0xFFDDDDDD); + static const Color _kBackgroundColor = CupertinoDynamicColor.withBrightness( + color: Color(0xFFEEEEEE), + darkColor: Color(0xFF212122), + ); + static const Color _kBackgroundColorPressed = CupertinoDynamicColor.withBrightness( + color: Color(0xFFDDDDDD), + darkColor: Color(0xFF3F3F40), + ); static const double _kButtonHeight = 56.0; static const TextStyle _kActionSheetActionStyle = TextStyle( fontFamily: '.SF UI Text', @@ -93,10 +99,11 @@ class _CupertinoContextMenuActionState extends State color: CupertinoColors.destructiveRed, ); } - return _kActionSheetActionStyle; + return _kActionSheetActionStyle.copyWith( + color: CupertinoDynamicColor.resolve(CupertinoColors.label, context) + ); } - @override Widget build(BuildContext context) { return GestureDetector( @@ -114,7 +121,9 @@ class _CupertinoContextMenuActionState extends State button: true, child: Container( decoration: BoxDecoration( - color: _isPressed ? _kBackgroundColorPressed : _kBackgroundColor, + color: _isPressed + ? CupertinoDynamicColor.resolve(_kBackgroundColorPressed, context) + : CupertinoDynamicColor.resolve(_kBackgroundColor, context), ), padding: const EdgeInsets.symmetric( vertical: 16.0, diff --git a/packages/flutter/test/cupertino/context_menu_action_test.dart b/packages/flutter/test/cupertino/context_menu_action_test.dart index f3d7981e5f..00d75f32fc 100644 --- a/packages/flutter/test/cupertino/context_menu_action_test.dart +++ b/packages/flutter/test/cupertino/context_menu_action_test.dart @@ -5,15 +5,28 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter_test/flutter_test.dart'; +import '../rendering/mock_canvas.dart'; + void main() { // Constants taken from _ContextMenuActionState. - const Color _kBackgroundColor = Color(0xFFEEEEEE); - const Color _kBackgroundColorPressed = Color(0xFFDDDDDD); - const Color _kRegularActionColor = CupertinoColors.black; + const CupertinoDynamicColor _kBackgroundColor = CupertinoDynamicColor.withBrightness( + color: Color(0xFFEEEEEE), + darkColor: Color(0xFF212122), + ); + // static const Color _kBackgroundColorPressed = Color(0xFFDDDDDD); + const CupertinoDynamicColor _kBackgroundColorPressed = CupertinoDynamicColor.withBrightness( + color: Color(0xFFDDDDDD), + darkColor: Color(0xFF3F3F40), + ); const Color _kDestructiveActionColor = CupertinoColors.destructiveRed; const FontWeight _kDefaultActionWeight = FontWeight.w600; - Widget _getApp({VoidCallback? onPressed, bool isDestructiveAction = false, bool isDefaultAction = false}) { + Widget _getApp({ + VoidCallback? onPressed, + bool isDestructiveAction = false, + bool isDefaultAction = false, + Brightness? brightness, + }) { final UniqueKey actionKey = UniqueKey(); final CupertinoContextMenuAction action = CupertinoContextMenuAction( key: actionKey, @@ -25,6 +38,9 @@ void main() { ); return CupertinoApp( + theme: CupertinoThemeData( + brightness: brightness ?? Brightness.light, + ), home: CupertinoPageScaffold( child: Center( child: action, @@ -33,16 +49,6 @@ void main() { ); } - BoxDecoration _getDecoration(WidgetTester tester) { - final Finder finder = find.descendant( - of: find.byType(CupertinoContextMenuAction), - matching: find.byType(Container), - ); - expect(finder, findsOneWidget); - final Container container = tester.widget(finder); - return container.decoration! as BoxDecoration; - } - TextStyle _getTextStyle(WidgetTester tester) { final Finder finder = find.descendant( of: find.byType(CupertinoContextMenuAction), @@ -76,22 +82,34 @@ void main() { testWidgets('turns grey when pressed and held', (WidgetTester tester) async { await tester.pumpWidget(_getApp()); - expect(_getDecoration(tester).color, _kBackgroundColor); + expect(find.byType(CupertinoContextMenuAction), paints..rect(color: _kBackgroundColor.color)); - final Offset actionCenter = tester.getCenter(find.byType(CupertinoContextMenuAction)); - final TestGesture gesture = await tester.startGesture(actionCenter); + final Offset actionCenterLight = tester.getCenter(find.byType(CupertinoContextMenuAction)); + final TestGesture gestureLight = await tester.startGesture(actionCenterLight); await tester.pump(); - expect(_getDecoration(tester).color, _kBackgroundColorPressed); + expect(find.byType(CupertinoContextMenuAction), paints..rect(color: _kBackgroundColorPressed.color)); - await gesture.up(); + await gestureLight.up(); await tester.pump(); - expect(_getDecoration(tester).color, _kBackgroundColor); + expect(find.byType(CupertinoContextMenuAction), paints..rect(color: _kBackgroundColor.color)); + + await tester.pumpWidget(_getApp(brightness: Brightness.dark)); + expect(find.byType(CupertinoContextMenuAction), paints..rect(color: _kBackgroundColor.darkColor)); + + final Offset actionCenterDark = tester.getCenter(find.byType(CupertinoContextMenuAction)); + final TestGesture gestureDark = await tester.startGesture(actionCenterDark); + await tester.pump(); + expect(find.byType(CupertinoContextMenuAction), paints..rect(color: _kBackgroundColorPressed.darkColor)); + + await gestureDark.up(); + await tester.pump(); + expect(find.byType(CupertinoContextMenuAction), paints..rect(color: _kBackgroundColor.darkColor)); }); testWidgets('icon and textStyle colors are correct out of the box', (WidgetTester tester) async { await tester.pumpWidget(_getApp()); - expect(_getTextStyle(tester).color, _kRegularActionColor); - expect(_getIcon(tester).color, _kRegularActionColor); + expect(_getTextStyle(tester).color, CupertinoColors.label); + expect(_getIcon(tester).color, CupertinoColors.label); }); testWidgets('icon and textStyle colors are correct for destructive actions', (WidgetTester tester) async {