diff --git a/packages/flutter/lib/src/cupertino/text_field.dart b/packages/flutter/lib/src/cupertino/text_field.dart index ffb6ade658..48b705ed12 100644 --- a/packages/flutter/lib/src/cupertino/text_field.dart +++ b/packages/flutter/lib/src/cupertino/text_field.dart @@ -847,7 +847,7 @@ class _CupertinoTextFieldState extends State with AutomaticK final TextStyle placeholderStyle = textStyle.merge(resolvedPlaceholderStyle); - final Brightness keyboardAppearance = widget.keyboardAppearance ?? themeData.brightness; + final Brightness keyboardAppearance = widget.keyboardAppearance ?? CupertinoTheme.brightnessOf(context); final Color cursorColor = CupertinoDynamicColor.resolve(widget.cursorColor, context) ?? themeData.primaryColor; final Color disabledColor = CupertinoDynamicColor.resolve(_kDisabledBackground, context); diff --git a/packages/flutter/lib/src/cupertino/theme.dart b/packages/flutter/lib/src/cupertino/theme.dart index 190006c1b6..e0d4b7ca4a 100644 --- a/packages/flutter/lib/src/cupertino/theme.dart +++ b/packages/flutter/lib/src/cupertino/theme.dart @@ -71,17 +71,22 @@ class CupertinoTheme extends StatelessWidget { return (inheritedTheme?.theme?.data ?? const CupertinoThemeData()).resolveFrom(context, nullOk: true); } - /// Retrieves the [Brightness] value from the closest ancestor [CupertinoTheme] - /// widget. + /// Retrieves the [Brightness] to use for descendant Cupertino widgets, based + /// on the value of [CupertinoThemeData.brightness] in the given [context]. /// - /// If no [CupertinoTheme] ancestor with an explicit brightness value could be - /// found, this method will resort to the closest ancestor [MediaQuery] widget. + /// If no [CupertinoTheme] can be found in the given [context], or its `brightness` + /// is null, it will fall back to [MediaQueryData.brightness]. /// - /// Throws an exception if no such [CupertinoTheme] or [MediaQuery] widgets exist - /// in the ancestry tree, unless [nullOk] is set to true. + /// Throws an exception if no valid [CupertinoTheme] or [MediaQuery] widgets + /// exist in the ancestry tree, unless [nullOk] is set to true. + /// + /// See also: + /// + /// * [CupertinoThemeData.brightness], the property takes precedence over + /// [MediaQueryData.platformBrightness] for descendant Cupertino widgets. static Brightness brightnessOf(BuildContext context, { bool nullOk = false }) { final _InheritedCupertinoTheme inheritedTheme = context.dependOnInheritedWidgetOfExactType<_InheritedCupertinoTheme>(); - return inheritedTheme?.theme?.data?._brightness ?? MediaQuery.of(context, nullOk: nullOk)?.platformBrightness; + return inheritedTheme?.theme?.data?.brightness ?? MediaQuery.of(context, nullOk: nullOk)?.platformBrightness; } /// The widget below this widget in the tree. @@ -181,7 +186,7 @@ class CupertinoThemeData extends Diagnosticable { ); const CupertinoThemeData._rawWithDefaults( - this._brightness, + this.brightness, this._primaryColor, this._primaryContrastingColor, this._textTheme, @@ -192,10 +197,11 @@ class CupertinoThemeData extends Diagnosticable { final _CupertinoThemeDefaults _defaults; - /// The general brightness theme of the [CupertinoThemeData]. + /// The brightness override for Cupertino descendants. /// - /// Overrides the ambient [MediaQueryData.platformBrightness] when specified. - /// Defaults to [Brightness.light]. + /// Defaults to null. If a non-null [Brightness] is specified, the value will + /// take precedence over the ambient [MediaQueryData.platformBrightness], when + /// determining the brightness of descendant Cupertino widgets. /// /// If coming from a Material [Theme] and unspecified, [brightness] will be /// derived from the Material [ThemeData]'s `brightness`. @@ -204,8 +210,10 @@ class CupertinoThemeData extends Diagnosticable { /// /// * [MaterialBasedCupertinoThemeData], a [CupertinoThemeData] that defers /// [brightness] to its Material [Theme] parent if it's unspecified. - Brightness get brightness => _brightness ?? Brightness.light; - final Brightness _brightness; + /// + /// * [CupertinoTheme.brightnessOf], a method used to retrieve the overall + /// [Brightness] from a [BuildContext], for Cupertino widgets. + final Brightness brightness; /// A color used on interactive elements of the theme. /// @@ -268,7 +276,7 @@ class CupertinoThemeData extends Diagnosticable { /// theme properties instead of iOS defaults. CupertinoThemeData noDefault() { return _NoDefaultCupertinoThemeData( - _brightness, + brightness, _primaryColor, _primaryContrastingColor, _textTheme, @@ -287,7 +295,7 @@ class CupertinoThemeData extends Diagnosticable { Color convertColor(Color color) => CupertinoDynamicColor.resolve(color, context, nullOk: nullOk); return CupertinoThemeData._rawWithDefaults( - _brightness, + brightness, convertColor(_primaryColor), convertColor(_primaryContrastingColor), _textTheme?.resolveFrom(context, nullOk: nullOk), @@ -313,7 +321,7 @@ class CupertinoThemeData extends Diagnosticable { Color scaffoldBackgroundColor, }) { return CupertinoThemeData._rawWithDefaults( - brightness ?? _brightness, + brightness ?? this.brightness, primaryColor ?? _primaryColor, primaryContrastingColor ?? _primaryContrastingColor, textTheme ?? _textTheme, @@ -327,7 +335,7 @@ class CupertinoThemeData extends Diagnosticable { void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); const CupertinoThemeData defaultData = CupertinoThemeData(); - properties.add(EnumProperty('brightness', brightness, defaultValue: defaultData.brightness)); + properties.add(EnumProperty('brightness', brightness, defaultValue: null)); properties.add(createCupertinoColorProperty('primaryColor', primaryColor, defaultValue: defaultData.primaryColor)); properties.add(createCupertinoColorProperty('primaryContrastingColor', primaryContrastingColor, defaultValue: defaultData.primaryContrastingColor)); properties.add(createCupertinoColorProperty('barBackgroundColor', barBackgroundColor, defaultValue: defaultData.barBackgroundColor)); @@ -338,7 +346,7 @@ class CupertinoThemeData extends Diagnosticable { class _NoDefaultCupertinoThemeData extends CupertinoThemeData { const _NoDefaultCupertinoThemeData( - this.brightness, + Brightness brightness, this.primaryColor, this.primaryContrastingColor, this.textTheme, @@ -354,8 +362,6 @@ class _NoDefaultCupertinoThemeData extends CupertinoThemeData { null, ); - @override - final Brightness brightness; @override final Color primaryColor; @override diff --git a/packages/flutter/test/cupertino/theme_test.dart b/packages/flutter/test/cupertino/theme_test.dart index b874f0548c..7fc0a27729 100644 --- a/packages/flutter/test/cupertino/theme_test.dart +++ b/packages/flutter/test/cupertino/theme_test.dart @@ -52,7 +52,7 @@ void main() { testWidgets('Default theme has defaults', (WidgetTester tester) async { final CupertinoThemeData theme = await testTheme(tester, const CupertinoThemeData()); - expect(theme.brightness, Brightness.light); + expect(theme.brightness, isNull); expect(theme.primaryColor, CupertinoColors.activeBlue); expect(theme.textTheme.textStyle.fontSize, 17.0); }); diff --git a/packages/flutter/test/material/theme_test.dart b/packages/flutter/test/material/theme_test.dart index 837ec1aaa8..c0f342b71d 100644 --- a/packages/flutter/test/material/theme_test.dart +++ b/packages/flutter/test/material/theme_test.dart @@ -418,12 +418,14 @@ void main() { int buildCount; CupertinoThemeData actualTheme; IconThemeData actualIconTheme; + BuildContext context; final Widget singletonThemeSubtree = Builder( - builder: (BuildContext context) { + builder: (BuildContext localContext) { buildCount++; - actualTheme = CupertinoTheme.of(context); - actualIconTheme = IconTheme.of(context); + actualTheme = CupertinoTheme.of(localContext); + actualIconTheme = IconTheme.of(localContext); + context = localContext; return const Placeholder(); }, ); @@ -437,6 +439,7 @@ void main() { buildCount = 0; actualTheme = null; actualIconTheme = null; + context = null; }); testWidgets('Default theme has defaults', (WidgetTester tester) async { @@ -461,6 +464,27 @@ void main() { expect(theme.textTheme.textStyle.fontSize, 17.0); }); + testWidgets('MaterialTheme overrides the brightness', (WidgetTester tester) async { + await testTheme(tester, ThemeData.dark()); + expect(CupertinoTheme.brightnessOf(context), Brightness.dark); + + await testTheme(tester, ThemeData.light()); + expect(CupertinoTheme.brightnessOf(context), Brightness.light); + + // Overridable by cupertinoOverrideTheme. + await testTheme(tester, ThemeData( + brightness: Brightness.light, + cupertinoOverrideTheme: const CupertinoThemeData(brightness: Brightness.dark), + )); + expect(CupertinoTheme.brightnessOf(context), Brightness.dark); + + await testTheme(tester, ThemeData( + brightness: Brightness.dark, + cupertinoOverrideTheme: const CupertinoThemeData(brightness: Brightness.light), + )); + expect(CupertinoTheme.brightnessOf(context), Brightness.light); + }); + testWidgets('Can override material theme', (WidgetTester tester) async { final CupertinoThemeData theme = await testTheme(tester, ThemeData( cupertinoOverrideTheme: const CupertinoThemeData(