Unverified Commit 8661dc40 authored by LongCatIsLooong's avatar LongCatIsLooong Committed by GitHub

Make CupertinoDynamicColor.resolve return null when given a null color (#39927)

parent 57d714eb
...@@ -325,13 +325,15 @@ class CupertinoDynamicColor extends Color { ...@@ -325,13 +325,15 @@ class CupertinoDynamicColor extends Color {
/// Resolves the given [Color] by calling [resolveFrom]. /// Resolves the given [Color] by calling [resolveFrom].
/// ///
/// If the given color is already a concrete [Color], it will be returned as is. /// If the given color is already a concrete [Color], it will be returned as is.
/// If the given color is null, returns null.
/// If the given color is a [CupertinoDynamicColor], but the given [BuildContext] /// If the given color is a [CupertinoDynamicColor], but the given [BuildContext]
/// lacks the dependencies required to the color resolution, the default trait /// lacks the dependencies required to the color resolution, the default trait
/// value will be used ([Brightness.light] platform brightness, normal contrast, /// value will be used ([Brightness.light] platform brightness, normal contrast,
/// [CupertinoUserInterfaceLevelData.base] elevation level), unless [nullOk] is /// [CupertinoUserInterfaceLevelData.base] elevation level), unless [nullOk] is
/// set to false, in which case an exception will be thrown. /// set to false, in which case an exception will be thrown.
static Color resolve(Color resolvable, BuildContext context, { bool nullOk = true }) { static Color resolve(Color resolvable, BuildContext context, { bool nullOk = true }) {
assert(resolvable != null); if (resolvable == null)
return null;
assert(context != null); assert(context != null);
return (resolvable is CupertinoDynamicColor) return (resolvable is CupertinoDynamicColor)
? resolvable.resolveFrom(context, nullOk: nullOk) ? resolvable.resolveFrom(context, nullOk: nullOk)
......
...@@ -216,7 +216,7 @@ class CupertinoTextThemeData extends Diagnosticable { ...@@ -216,7 +216,7 @@ class CupertinoTextThemeData extends Diagnosticable {
/// Returns a copy of the current [CupertinoTextThemeData] with all the colors /// Returns a copy of the current [CupertinoTextThemeData] with all the colors
/// resolved against the given [BuildContext]. /// resolved against the given [BuildContext].
CupertinoTextThemeData resolveFrom(BuildContext context, { bool nullOk = false }) { CupertinoTextThemeData resolveFrom(BuildContext context, { bool nullOk = false }) {
Color convertColor(Color color) => color == null ? null : CupertinoDynamicColor.resolve(color, context, nullOk: nullOk); Color convertColor(Color color) => CupertinoDynamicColor.resolve(color, context, nullOk: nullOk);
TextStyle resolveTextStyle(TextStyle textStyle) { TextStyle resolveTextStyle(TextStyle textStyle) {
return textStyle?.copyWith( return textStyle?.copyWith(
......
...@@ -248,7 +248,7 @@ class CupertinoThemeData extends Diagnosticable { ...@@ -248,7 +248,7 @@ class CupertinoThemeData extends Diagnosticable {
/// It will be called in [CupertinoTheme.of]. /// It will be called in [CupertinoTheme.of].
@protected @protected
CupertinoThemeData resolveFrom(BuildContext context, { bool nullOk = false }) { CupertinoThemeData resolveFrom(BuildContext context, { bool nullOk = false }) {
Color convertColor(Color color) => color == null ? null : CupertinoDynamicColor.resolve(color, context, nullOk: nullOk); Color convertColor(Color color) => CupertinoDynamicColor.resolve(color, context, nullOk: nullOk);
return copyWith( return copyWith(
primaryColor: convertColor(primaryColor), primaryColor: convertColor(primaryColor),
...@@ -329,9 +329,7 @@ class _NoDefaultCupertinoThemeData extends CupertinoThemeData { ...@@ -329,9 +329,7 @@ class _NoDefaultCupertinoThemeData extends CupertinoThemeData {
@override @override
_NoDefaultCupertinoThemeData resolveFrom(BuildContext context, { bool nullOk = false }) { _NoDefaultCupertinoThemeData resolveFrom(BuildContext context, { bool nullOk = false }) {
Color convertColor(Color color) => color == null Color convertColor(Color color) => CupertinoDynamicColor.resolve(color, context, nullOk: nullOk);
? null
: CupertinoDynamicColor.resolve(color, context, nullOk: nullOk);
return _NoDefaultCupertinoThemeData( return _NoDefaultCupertinoThemeData(
brightness, brightness,
......
...@@ -160,6 +160,10 @@ void main() { ...@@ -160,6 +160,10 @@ void main() {
); );
}); });
test('can resolve null color', () {
expect(CupertinoDynamicColor.resolve(null, null), isNull);
});
test('withVibrancy constructor creates colors that may depend on vibrancy', () { test('withVibrancy constructor creates colors that may depend on vibrancy', () {
expect(vibrancyDependentColor1, const CupertinoDynamicColor.withBrightness( expect(vibrancyDependentColor1, const CupertinoDynamicColor.withBrightness(
color: color1, color: color1,
......
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