Unverified Commit 82bd7cf8 authored by Michael Goderbauer's avatar Michael Goderbauer Committed by GitHub

Make CupertinoThemeData properties non-nullable (#66274)

parent 19062180
...@@ -143,7 +143,7 @@ class _InheritedCupertinoTheme extends InheritedWidget { ...@@ -143,7 +143,7 @@ class _InheritedCupertinoTheme extends InheritedWidget {
/// * [ThemeData], a Material equivalent that also configures Cupertino /// * [ThemeData], a Material equivalent that also configures Cupertino
/// styling via a [CupertinoThemeData] subclass [MaterialBasedCupertinoThemeData]. /// styling via a [CupertinoThemeData] subclass [MaterialBasedCupertinoThemeData].
@immutable @immutable
class CupertinoThemeData with Diagnosticable { class CupertinoThemeData extends NoDefaultCupertinoThemeData with Diagnosticable {
/// Creates a [CupertinoTheme] styling specification. /// Creates a [CupertinoTheme] styling specification.
/// ///
/// Unspecified parameters default to a reasonable iOS default style. /// Unspecified parameters default to a reasonable iOS default style.
...@@ -186,16 +186,125 @@ class CupertinoThemeData with Diagnosticable { ...@@ -186,16 +186,125 @@ class CupertinoThemeData with Diagnosticable {
); );
const CupertinoThemeData._rawWithDefaults( const CupertinoThemeData._rawWithDefaults(
this.brightness, Brightness? brightness,
this._primaryColor, Color? primaryColor,
this._primaryContrastingColor, Color? primaryContrastingColor,
this._textTheme, CupertinoTextThemeData? textTheme,
this._barBackgroundColor, Color? barBackgroundColor,
this._scaffoldBackgroundColor, Color? scaffoldBackgroundColor,
this._defaults, this._defaults,
) : super(
brightness: brightness,
primaryColor: primaryColor,
primaryContrastingColor: primaryContrastingColor,
textTheme: textTheme,
barBackgroundColor: barBackgroundColor,
scaffoldBackgroundColor: scaffoldBackgroundColor,
); );
final _CupertinoThemeDefaults? _defaults; final _CupertinoThemeDefaults _defaults;
@override
Color get primaryColor => super.primaryColor ?? _defaults.primaryColor;
@override
Color get primaryContrastingColor => super.primaryContrastingColor ?? _defaults.primaryContrastingColor;
@override
CupertinoTextThemeData get textTheme {
return super.textTheme ?? _defaults.textThemeDefaults.createDefaults(primaryColor: primaryColor);
}
@override
Color get barBackgroundColor => super.barBackgroundColor ?? _defaults.barBackgroundColor;
@override
Color get scaffoldBackgroundColor => super.scaffoldBackgroundColor ?? _defaults.scaffoldBackgroundColor;
@override
NoDefaultCupertinoThemeData noDefault() {
return NoDefaultCupertinoThemeData(
brightness: super.brightness,
primaryColor: super.primaryColor,
primaryContrastingColor: super.primaryContrastingColor,
textTheme: super.textTheme,
barBackgroundColor: super.barBackgroundColor,
scaffoldBackgroundColor: super.scaffoldBackgroundColor,
);
}
@override
CupertinoThemeData resolveFrom(BuildContext context, { bool nullOk = false }) {
Color? convertColor(Color? color) => CupertinoDynamicColor.resolve(color, context, nullOk: nullOk);
return CupertinoThemeData._rawWithDefaults(
brightness,
convertColor(super.primaryColor),
convertColor(super.primaryContrastingColor),
super.textTheme?.resolveFrom(context, nullOk: nullOk),
convertColor(super.barBackgroundColor),
convertColor(super.scaffoldBackgroundColor),
_defaults.resolveFrom(context, super.textTheme == null, nullOk: nullOk),
);
}
@override
CupertinoThemeData copyWith({
Brightness? brightness,
Color? primaryColor,
Color? primaryContrastingColor,
CupertinoTextThemeData? textTheme,
Color? barBackgroundColor,
Color? scaffoldBackgroundColor,
}) {
return CupertinoThemeData._rawWithDefaults(
brightness ?? super.brightness,
primaryColor ?? super.primaryColor,
primaryContrastingColor ?? super.primaryContrastingColor,
textTheme ?? super.textTheme,
barBackgroundColor ?? super.barBackgroundColor,
scaffoldBackgroundColor ?? super.scaffoldBackgroundColor,
_defaults,
);
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
const CupertinoThemeData defaultData = CupertinoThemeData();
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));
properties.add(createCupertinoColorProperty('scaffoldBackgroundColor', scaffoldBackgroundColor, defaultValue: defaultData.scaffoldBackgroundColor));
textTheme.debugFillProperties(properties);
}
}
/// Styling specifications for a cupertino theme without default values for
/// unspecified properties.
///
/// Unlike [CupertinoThemeData] instances of this class do not return default
/// values for properties that have been left unspecified in the constructor.
/// Instead, unspecified properties will return null. This is used by
/// Material's [ThemeData.cupertinoOverrideTheme].
///
/// See also:
///
/// * [CupertinoThemeData], which uses reasonable default values for
/// unspecified theme properties.
class NoDefaultCupertinoThemeData {
/// Creates a [NoDefaultCupertinoThemeData] styling specification.
///
/// Unspecified properties default to null.
const NoDefaultCupertinoThemeData({
this.brightness,
this.primaryColor,
this.primaryContrastingColor,
this.textTheme,
this.barBackgroundColor,
this.scaffoldBackgroundColor,
});
/// The brightness override for Cupertino descendants. /// The brightness override for Cupertino descendants.
/// ///
...@@ -230,8 +339,7 @@ class CupertinoThemeData with Diagnosticable { ...@@ -230,8 +339,7 @@ class CupertinoThemeData with Diagnosticable {
/// ///
/// * [MaterialBasedCupertinoThemeData], a [CupertinoThemeData] that defers /// * [MaterialBasedCupertinoThemeData], a [CupertinoThemeData] that defers
/// [primaryColor] to its Material [Theme] parent if it's unspecified. /// [primaryColor] to its Material [Theme] parent if it's unspecified.
Color? get primaryColor => _primaryColor ?? _defaults!.primaryColor; final Color? primaryColor;
final Color? _primaryColor;
/// A color that must be easy to see when rendered on a [primaryColor] background. /// A color that must be easy to see when rendered on a [primaryColor] background.
/// ///
...@@ -245,150 +353,57 @@ class CupertinoThemeData with Diagnosticable { ...@@ -245,150 +353,57 @@ class CupertinoThemeData with Diagnosticable {
/// ///
/// * [MaterialBasedCupertinoThemeData], a [CupertinoThemeData] that defers /// * [MaterialBasedCupertinoThemeData], a [CupertinoThemeData] that defers
/// [primaryContrastingColor] to its Material [Theme] parent if it's unspecified. /// [primaryContrastingColor] to its Material [Theme] parent if it's unspecified.
Color? get primaryContrastingColor => _primaryContrastingColor ?? _defaults!.primaryContrastingColor; final Color? primaryContrastingColor;
final Color? _primaryContrastingColor;
/// Text styles used by Cupertino widgets. /// Text styles used by Cupertino widgets.
/// ///
/// Derived from [primaryColor] if unspecified. /// Derived from [primaryColor] if unspecified.
CupertinoTextThemeData? get textTheme { final CupertinoTextThemeData? textTheme;
return _textTheme ?? _defaults!.textThemeDefaults.createDefaults(primaryColor: primaryColor!);
}
final CupertinoTextThemeData? _textTheme;
/// Background color of the top nav bar and bottom tab bar. /// Background color of the top nav bar and bottom tab bar.
/// ///
/// Defaults to a light gray in light mode, or a dark translucent gray color in /// Defaults to a light gray in light mode, or a dark translucent gray color in
/// dark mode. /// dark mode.
Color? get barBackgroundColor => _barBackgroundColor ?? _defaults!.barBackgroundColor; final Color? barBackgroundColor;
final Color? _barBackgroundColor;
/// Background color of the scaffold. /// Background color of the scaffold.
/// ///
/// Defaults to [CupertinoColors.systemBackground]. /// Defaults to [CupertinoColors.systemBackground].
Color? get scaffoldBackgroundColor => _scaffoldBackgroundColor ?? _defaults!.scaffoldBackgroundColor; final Color? scaffoldBackgroundColor;
final Color? _scaffoldBackgroundColor;
/// Returns an instance of the [CupertinoThemeData] whose property getters /// Returns an instance of the theme data whose property getters only return
/// only return the construction time specifications with no derived values. /// the construction time specifications with no derived values.
/// ///
/// Used in Material themes to let unspecified properties fallback to Material /// Used in Material themes to let unspecified properties fallback to Material
/// theme properties instead of iOS defaults. /// theme properties instead of iOS defaults.
CupertinoThemeData noDefault() { NoDefaultCupertinoThemeData noDefault() => this;
return _NoDefaultCupertinoThemeData(
brightness,
_primaryColor,
_primaryContrastingColor,
_textTheme,
_barBackgroundColor,
_scaffoldBackgroundColor,
);
}
/// Returns a new `CupertinoThemeData` with all its colors resolved against the /// Returns a new theme data with all its colors resolved against the
/// given [BuildContext]. /// given [BuildContext].
/// ///
/// Called by [CupertinoTheme.of] to resolve colors defined in the retrieved /// Called by [CupertinoTheme.of] to resolve colors defined in the retrieved
/// [CupertinoThemeData]. /// [CupertinoThemeData].
@protected @protected
CupertinoThemeData resolveFrom(BuildContext context, { bool nullOk = false }) { NoDefaultCupertinoThemeData resolveFrom(BuildContext context, { bool nullOk = false }) {
Color? convertColor(Color? color) => CupertinoDynamicColor.resolve(color, context, nullOk: nullOk); Color? convertColor(Color? color) => CupertinoDynamicColor.resolve(color, context, nullOk: nullOk);
return CupertinoThemeData._rawWithDefaults( return NoDefaultCupertinoThemeData(
brightness, brightness: brightness,
convertColor(_primaryColor), primaryColor: convertColor(primaryColor),
convertColor(_primaryContrastingColor), primaryContrastingColor: convertColor(primaryContrastingColor),
_textTheme?.resolveFrom(context, nullOk: nullOk), textTheme: textTheme?.resolveFrom(context, nullOk: nullOk),
convertColor(_barBackgroundColor), barBackgroundColor: convertColor(barBackgroundColor),
convertColor(_scaffoldBackgroundColor), scaffoldBackgroundColor: convertColor(scaffoldBackgroundColor),
_defaults!.resolveFrom(context, _textTheme == null, nullOk: nullOk),
); );
} }
/// Creates a copy of [CupertinoThemeData] with specified attributes overridden. /// Creates a copy of the theme data with specified attributes overridden.
/// ///
/// Only the current instance's specified attributes are copied instead of /// Only the current instance's specified attributes are copied instead of
/// derived values. For instance, if the current [CupertinoThemeData.textTheme] /// derived values. For instance, if the current [textTheme] is implied from
/// is implied from the current [primaryColor] because it was not specified, /// the current [primaryColor] because it was not specified, copying with a
/// copying with a different [primaryColor] will also change the copy's implied /// different [primaryColor] will also change the copy's implied [textTheme].
/// [textTheme]. NoDefaultCupertinoThemeData copyWith({
CupertinoThemeData copyWith({
Brightness? brightness,
Color? primaryColor,
Color? primaryContrastingColor,
CupertinoTextThemeData? textTheme,
Color? barBackgroundColor,
Color? scaffoldBackgroundColor,
}) {
return CupertinoThemeData._rawWithDefaults(
brightness ?? this.brightness,
primaryColor ?? _primaryColor,
primaryContrastingColor ?? _primaryContrastingColor,
textTheme ?? _textTheme,
barBackgroundColor ?? _barBackgroundColor,
scaffoldBackgroundColor ?? _scaffoldBackgroundColor,
_defaults,
);
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
const CupertinoThemeData defaultData = CupertinoThemeData();
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));
properties.add(createCupertinoColorProperty('scaffoldBackgroundColor', scaffoldBackgroundColor, defaultValue: defaultData.scaffoldBackgroundColor));
textTheme!.debugFillProperties(properties);
}
}
class _NoDefaultCupertinoThemeData extends CupertinoThemeData {
const _NoDefaultCupertinoThemeData(
Brightness? brightness,
this.primaryColor,
this.primaryContrastingColor,
this.textTheme,
this.barBackgroundColor,
this.scaffoldBackgroundColor,
) : super._rawWithDefaults(
brightness,
primaryColor,
primaryContrastingColor,
textTheme,
barBackgroundColor,
scaffoldBackgroundColor,
null,
);
@override
final Color? primaryColor;
@override
final Color? primaryContrastingColor;
@override
final CupertinoTextThemeData? textTheme;
@override
final Color? barBackgroundColor;
@override
final Color? scaffoldBackgroundColor;
@override
_NoDefaultCupertinoThemeData resolveFrom(BuildContext context, { bool nullOk = false }) {
Color? convertColor(Color? color) => CupertinoDynamicColor.resolve(color, context, nullOk: nullOk);
return _NoDefaultCupertinoThemeData(
brightness,
convertColor(primaryColor),
convertColor(primaryContrastingColor),
textTheme?.resolveFrom(context, nullOk: nullOk),
convertColor(barBackgroundColor),
convertColor(scaffoldBackgroundColor),
);
}
@override
CupertinoThemeData copyWith({
Brightness? brightness, Brightness? brightness,
Color? primaryColor, Color? primaryColor,
Color? primaryContrastingColor, Color? primaryContrastingColor,
...@@ -396,13 +411,13 @@ class _NoDefaultCupertinoThemeData extends CupertinoThemeData { ...@@ -396,13 +411,13 @@ class _NoDefaultCupertinoThemeData extends CupertinoThemeData {
Color? barBackgroundColor , Color? barBackgroundColor ,
Color? scaffoldBackgroundColor, Color? scaffoldBackgroundColor,
}) { }) {
return _NoDefaultCupertinoThemeData( return NoDefaultCupertinoThemeData(
brightness ?? this.brightness, brightness: brightness ?? this.brightness,
primaryColor ?? this.primaryColor, primaryColor: primaryColor ?? this.primaryColor,
primaryContrastingColor ?? this.primaryContrastingColor, primaryContrastingColor: primaryContrastingColor ?? this.primaryContrastingColor,
textTheme ?? this.textTheme, textTheme: textTheme ?? this.textTheme,
barBackgroundColor ?? this.barBackgroundColor, barBackgroundColor: barBackgroundColor ?? this.barBackgroundColor,
scaffoldBackgroundColor ?? this.scaffoldBackgroundColor, scaffoldBackgroundColor: scaffoldBackgroundColor ?? this.scaffoldBackgroundColor,
); );
} }
} }
......
...@@ -271,7 +271,7 @@ class ThemeData with Diagnosticable { ...@@ -271,7 +271,7 @@ class ThemeData with Diagnosticable {
FloatingActionButtonThemeData floatingActionButtonTheme, FloatingActionButtonThemeData floatingActionButtonTheme,
NavigationRailThemeData navigationRailTheme, NavigationRailThemeData navigationRailTheme,
Typography typography, Typography typography,
CupertinoThemeData cupertinoOverrideTheme, NoDefaultCupertinoThemeData cupertinoOverrideTheme,
SnackBarThemeData snackBarTheme, SnackBarThemeData snackBarTheme,
BottomSheetThemeData bottomSheetTheme, BottomSheetThemeData bottomSheetTheme,
PopupMenuThemeData popupMenuTheme, PopupMenuThemeData popupMenuTheme,
...@@ -1080,7 +1080,7 @@ class ThemeData with Diagnosticable { ...@@ -1080,7 +1080,7 @@ class ThemeData with Diagnosticable {
/// ///
/// This cascading effect for individual attributes of the [CupertinoThemeData] /// This cascading effect for individual attributes of the [CupertinoThemeData]
/// can be overridden using attributes of this [cupertinoOverrideTheme]. /// can be overridden using attributes of this [cupertinoOverrideTheme].
final CupertinoThemeData cupertinoOverrideTheme; final NoDefaultCupertinoThemeData cupertinoOverrideTheme;
/// A theme for customizing the color, elevation, and shape of a bottom sheet. /// A theme for customizing the color, elevation, and shape of a bottom sheet.
final BottomSheetThemeData bottomSheetTheme; final BottomSheetThemeData bottomSheetTheme;
...@@ -1211,7 +1211,7 @@ class ThemeData with Diagnosticable { ...@@ -1211,7 +1211,7 @@ class ThemeData with Diagnosticable {
FloatingActionButtonThemeData floatingActionButtonTheme, FloatingActionButtonThemeData floatingActionButtonTheme,
NavigationRailThemeData navigationRailTheme, NavigationRailThemeData navigationRailTheme,
Typography typography, Typography typography,
CupertinoThemeData cupertinoOverrideTheme, NoDefaultCupertinoThemeData cupertinoOverrideTheme,
SnackBarThemeData snackBarTheme, SnackBarThemeData snackBarTheme,
BottomSheetThemeData bottomSheetTheme, BottomSheetThemeData bottomSheetTheme,
PopupMenuThemeData popupMenuTheme, PopupMenuThemeData popupMenuTheme,
...@@ -1682,7 +1682,7 @@ class ThemeData with Diagnosticable { ...@@ -1682,7 +1682,7 @@ class ThemeData with Diagnosticable {
properties.add(DiagnosticsProperty<FloatingActionButtonThemeData>('floatingActionButtonThemeData', floatingActionButtonTheme, defaultValue: defaultData.floatingActionButtonTheme, level: DiagnosticLevel.debug)); properties.add(DiagnosticsProperty<FloatingActionButtonThemeData>('floatingActionButtonThemeData', floatingActionButtonTheme, defaultValue: defaultData.floatingActionButtonTheme, level: DiagnosticLevel.debug));
properties.add(DiagnosticsProperty<NavigationRailThemeData>('navigationRailThemeData', navigationRailTheme, defaultValue: defaultData.navigationRailTheme, level: DiagnosticLevel.debug)); properties.add(DiagnosticsProperty<NavigationRailThemeData>('navigationRailThemeData', navigationRailTheme, defaultValue: defaultData.navigationRailTheme, level: DiagnosticLevel.debug));
properties.add(DiagnosticsProperty<Typography>('typography', typography, defaultValue: defaultData.typography, level: DiagnosticLevel.debug)); properties.add(DiagnosticsProperty<Typography>('typography', typography, defaultValue: defaultData.typography, level: DiagnosticLevel.debug));
properties.add(DiagnosticsProperty<CupertinoThemeData>('cupertinoOverrideTheme', cupertinoOverrideTheme, defaultValue: defaultData.cupertinoOverrideTheme, level: DiagnosticLevel.debug)); properties.add(DiagnosticsProperty<NoDefaultCupertinoThemeData>('cupertinoOverrideTheme', cupertinoOverrideTheme, defaultValue: defaultData.cupertinoOverrideTheme, level: DiagnosticLevel.debug));
properties.add(DiagnosticsProperty<SnackBarThemeData>('snackBarTheme', snackBarTheme, defaultValue: defaultData.snackBarTheme, level: DiagnosticLevel.debug)); properties.add(DiagnosticsProperty<SnackBarThemeData>('snackBarTheme', snackBarTheme, defaultValue: defaultData.snackBarTheme, level: DiagnosticLevel.debug));
properties.add(DiagnosticsProperty<BottomSheetThemeData>('bottomSheetTheme', bottomSheetTheme, defaultValue: defaultData.bottomSheetTheme, level: DiagnosticLevel.debug)); properties.add(DiagnosticsProperty<BottomSheetThemeData>('bottomSheetTheme', bottomSheetTheme, defaultValue: defaultData.bottomSheetTheme, level: DiagnosticLevel.debug));
properties.add(DiagnosticsProperty<PopupMenuThemeData>('popupMenuTheme', popupMenuTheme, defaultValue: defaultData.popupMenuTheme, level: DiagnosticLevel.debug)); properties.add(DiagnosticsProperty<PopupMenuThemeData>('popupMenuTheme', popupMenuTheme, defaultValue: defaultData.popupMenuTheme, level: DiagnosticLevel.debug));
...@@ -1759,7 +1759,7 @@ class MaterialBasedCupertinoThemeData extends CupertinoThemeData { ...@@ -1759,7 +1759,7 @@ class MaterialBasedCupertinoThemeData extends CupertinoThemeData {
); );
final ThemeData _materialTheme; final ThemeData _materialTheme;
final CupertinoThemeData _cupertinoOverrideTheme; final NoDefaultCupertinoThemeData _cupertinoOverrideTheme;
@override @override
Brightness get brightness => _cupertinoOverrideTheme.brightness ?? _materialTheme.brightness; Brightness get brightness => _cupertinoOverrideTheme.brightness ?? _materialTheme.brightness;
......
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