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 {
/// 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)
......
......@@ -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(
......
......@@ -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,
......
......@@ -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,
......
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