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
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);
......
......@@ -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', brightness, defaultValue: defaultData.brightness));
properties.add(EnumProperty<Brightness>('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
......
......@@ -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);
});
......
......@@ -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(
......
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