diff --git a/packages/flutter/lib/src/cupertino/theme.dart b/packages/flutter/lib/src/cupertino/theme.dart index b7c7631e2a..300022a4f1 100644 --- a/packages/flutter/lib/src/cupertino/theme.dart +++ b/packages/flutter/lib/src/cupertino/theme.dart @@ -63,7 +63,7 @@ class CupertinoTheme extends StatelessWidget { /// Resolves all the colors defined in that [CupertinoThemeData] against the /// given [BuildContext] on a best-effort basis. static CupertinoThemeData of(BuildContext context) { - final _InheritedCupertinoTheme? inheritedTheme = context.dependOnInheritedWidgetOfExactType<_InheritedCupertinoTheme>(); + final InheritedCupertinoTheme? inheritedTheme = context.dependOnInheritedWidgetOfExactType(); return (inheritedTheme?.theme.data ?? const CupertinoThemeData()).resolveFrom(context); } @@ -83,7 +83,7 @@ class CupertinoTheme extends StatelessWidget { /// * [CupertinoThemeData.brightness], the property takes precedence over /// [MediaQueryData.platformBrightness] for descendant Cupertino widgets. static Brightness brightnessOf(BuildContext context) { - final _InheritedCupertinoTheme? inheritedTheme = context.dependOnInheritedWidgetOfExactType<_InheritedCupertinoTheme>(); + final InheritedCupertinoTheme? inheritedTheme = context.dependOnInheritedWidgetOfExactType(); return inheritedTheme?.theme.data.brightness ?? MediaQuery.platformBrightnessOf(context); } @@ -103,7 +103,7 @@ class CupertinoTheme extends StatelessWidget { /// * [brightnessOf], which throws if no valid [CupertinoTheme] or /// [MediaQuery] exists, instead of returning null. static Brightness? maybeBrightnessOf(BuildContext context) { - final _InheritedCupertinoTheme? inheritedTheme = context.dependOnInheritedWidgetOfExactType<_InheritedCupertinoTheme>(); + final InheritedCupertinoTheme? inheritedTheme = context.dependOnInheritedWidgetOfExactType(); return inheritedTheme?.theme.data.brightness ?? MediaQuery.maybePlatformBrightnessOf(context); } @@ -114,7 +114,7 @@ class CupertinoTheme extends StatelessWidget { @override Widget build(BuildContext context) { - return _InheritedCupertinoTheme( + return InheritedCupertinoTheme( theme: this, child: IconTheme( data: CupertinoIconThemeData(color: data.primaryColor), @@ -130,12 +130,17 @@ class CupertinoTheme extends StatelessWidget { } } -class _InheritedCupertinoTheme extends InheritedTheme { - const _InheritedCupertinoTheme({ +/// Provides a [CupertinoTheme] to all decendents. +class InheritedCupertinoTheme extends InheritedTheme { + /// Creates an [InheritedTheme] that provides a [CupertinoTheme] to all + /// decendents. + const InheritedCupertinoTheme({ + super.key, required this.theme, required super.child, }); + /// The [CupertinoTheme] that is provided to widgets lower in the tree. final CupertinoTheme theme; @override @@ -144,7 +149,7 @@ class _InheritedCupertinoTheme extends InheritedTheme { } @override - bool updateShouldNotify(_InheritedCupertinoTheme old) => theme.data != old.theme.data; + bool updateShouldNotify(InheritedCupertinoTheme oldWidget) => theme.data != oldWidget.theme.data; } /// Styling specifications for a [CupertinoTheme]. diff --git a/packages/flutter/lib/src/material/theme.dart b/packages/flutter/lib/src/material/theme.dart index c55efc2aeb..0e5cd64cd2 100644 --- a/packages/flutter/lib/src/material/theme.dart +++ b/packages/flutter/lib/src/material/theme.dart @@ -105,7 +105,10 @@ class Theme extends StatelessWidget { final _InheritedTheme? inheritedTheme = context.dependOnInheritedWidgetOfExactType<_InheritedTheme>(); final MaterialLocalizations? localizations = Localizations.of(context, MaterialLocalizations); final ScriptCategory category = localizations?.scriptCategory ?? ScriptCategory.englishLike; - final ThemeData theme = inheritedTheme?.theme.data ?? _kFallbackTheme; + final InheritedCupertinoTheme? inheritedCupertinoTheme = context.dependOnInheritedWidgetOfExactType(); + final ThemeData theme = inheritedTheme?.theme.data ?? ( + inheritedCupertinoTheme != null ? CupertinoBasedMaterialThemeData(themeData: inheritedCupertinoTheme.theme.data).materialTheme : _kFallbackTheme + ); return ThemeData.localize(theme, theme.typography.geometryThemeFor(category)); } @@ -124,17 +127,20 @@ class Theme extends StatelessWidget { ); } + CupertinoThemeData _inheritedCupertinoThemeData(BuildContext context) { + final InheritedCupertinoTheme? inheritedTheme = context.dependOnInheritedWidgetOfExactType(); + return (inheritedTheme?.theme.data ?? MaterialBasedCupertinoThemeData(materialTheme: data)).resolveFrom(context); + } + @override Widget build(BuildContext context) { return _InheritedTheme( theme: this, child: CupertinoTheme( - // We're using a MaterialBasedCupertinoThemeData here instead of a - // CupertinoThemeData because it defers some properties to the Material - // ThemeData. - data: MaterialBasedCupertinoThemeData( - materialTheme: data, - ), + // If a CupertinoThemeData doesn't exist, we're using a + // MaterialBasedCupertinoThemeData here instead of a CupertinoThemeData + // because it defers some properties to the Material ThemeData. + data: _inheritedCupertinoThemeData(context), child: _wrapsWidgetThemes(context, child), ), ); diff --git a/packages/flutter/lib/src/material/theme_data.dart b/packages/flutter/lib/src/material/theme_data.dart index 1c41c63b31..ec429153cd 100644 --- a/packages/flutter/lib/src/material/theme_data.dart +++ b/packages/flutter/lib/src/material/theme_data.dart @@ -2210,6 +2210,33 @@ class MaterialBasedCupertinoThemeData extends CupertinoThemeData { } } +/// A class for creating a Material theme with a color scheme based off of the +/// colors from a [CupertinoThemeData]. This is intended to be used only in the +/// case when a Material widget is unable to find a Material theme in the tree, +/// but is able to find a Cupertino theme. Most often this will occur when a +/// Material widget is used inside of a [CupertinoApp]. +/// +/// Besides the colors, this theme will use all the defaults from Material's +/// [ThemeData], so if further customization is needed, it is best to manually +/// add a Material [Theme] above the [CupertinoApp]. +class CupertinoBasedMaterialThemeData { + /// Creates a Material theme with a color scheme based off of the colors from + /// a [CupertinoThemeData]. + CupertinoBasedMaterialThemeData({ + required CupertinoThemeData themeData, + }) : materialTheme = ThemeData( + colorScheme: ColorScheme.fromSeed( + seedColor: themeData.primaryColor, + brightness: themeData.brightness ?? Brightness.light, + primary: themeData.primaryColor, + onPrimary: themeData.primaryContrastingColor, + ) + ); + + /// The Material theme data with colors based on an existing [CupertinoThemeData]. + final ThemeData materialTheme; +} + @immutable class _IdentityThemeDataCacheKey { const _IdentityThemeDataCacheKey(this.baseTheme, this.localTextGeometry); diff --git a/packages/flutter/test/cupertino/app_test.dart b/packages/flutter/test/cupertino/app_test.dart index df359b9d8d..787a05113a 100644 --- a/packages/flutter/test/cupertino/app_test.dart +++ b/packages/flutter/test/cupertino/app_test.dart @@ -5,6 +5,7 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; +import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -448,6 +449,23 @@ void main() { debugBrightnessOverride = null; }); + testWidgets('CupertinoApp creates a Material theme with colors based off of Cupertino theme', (WidgetTester tester) async { + late ThemeData appliedTheme; + await tester.pumpWidget( + CupertinoApp( + theme: const CupertinoThemeData(primaryColor: CupertinoColors.activeGreen), + home: Builder( + builder: (BuildContext context) { + appliedTheme = Theme.of(context); + return const SizedBox(); + }, + ), + ), + ); + + expect(appliedTheme.colorScheme.primary, CupertinoColors.activeGreen); + }); + testWidgets('Cursor color is resolved when CupertinoThemeData.brightness is null', (WidgetTester tester) async { debugBrightnessOverride = Brightness.dark;