Commit a499e05f authored by LongCatIsLooong's avatar LongCatIsLooong Committed by Flutter GitHub Bot

Let material ThemeData dictate brightness if cupertinoOverrideTheme.brightness is null (#47249)

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