diff --git a/packages/flutter/lib/src/cupertino/colors.dart b/packages/flutter/lib/src/cupertino/colors.dart index 7f7fc418514..41a17ad1a4b 100644 --- a/packages/flutter/lib/src/cupertino/colors.dart +++ b/packages/flutter/lib/src/cupertino/colors.dart @@ -325,13 +325,15 @@ class CupertinoDynamicColor extends Color { /// 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 null, returns null. /// If the given color is a [CupertinoDynamicColor], but the given [BuildContext] /// lacks the dependencies required to the color resolution, the default trait /// value will be used ([Brightness.light] platform brightness, normal contrast, /// [CupertinoUserInterfaceLevelData.base] elevation level), unless [nullOk] is /// set to false, in which case an exception will be thrown. static Color resolve(Color resolvable, BuildContext context, { bool nullOk = true }) { - assert(resolvable != null); + if (resolvable == null) + return null; assert(context != null); return (resolvable is CupertinoDynamicColor) ? resolvable.resolveFrom(context, nullOk: nullOk) diff --git a/packages/flutter/lib/src/cupertino/text_theme.dart b/packages/flutter/lib/src/cupertino/text_theme.dart index 981e28bb90e..8e87277bcf8 100644 --- a/packages/flutter/lib/src/cupertino/text_theme.dart +++ b/packages/flutter/lib/src/cupertino/text_theme.dart @@ -216,7 +216,7 @@ class CupertinoTextThemeData extends Diagnosticable { /// Returns a copy of the current [CupertinoTextThemeData] with all the colors /// resolved against the given [BuildContext]. 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) { return textStyle?.copyWith( diff --git a/packages/flutter/lib/src/cupertino/theme.dart b/packages/flutter/lib/src/cupertino/theme.dart index 50f55ecfea8..173afb835eb 100644 --- a/packages/flutter/lib/src/cupertino/theme.dart +++ b/packages/flutter/lib/src/cupertino/theme.dart @@ -248,7 +248,7 @@ class CupertinoThemeData extends Diagnosticable { /// It will be called in [CupertinoTheme.of]. @protected 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( primaryColor: convertColor(primaryColor), @@ -329,9 +329,7 @@ class _NoDefaultCupertinoThemeData extends CupertinoThemeData { @override _NoDefaultCupertinoThemeData 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 _NoDefaultCupertinoThemeData( brightness, diff --git a/packages/flutter/test/cupertino/colors_test.dart b/packages/flutter/test/cupertino/colors_test.dart index 91b44c0cdfc..d5a26f523bf 100644 --- a/packages/flutter/test/cupertino/colors_test.dart +++ b/packages/flutter/test/cupertino/colors_test.dart @@ -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', () { expect(vibrancyDependentColor1, const CupertinoDynamicColor.withBrightness( color: color1,