概述

#

為了支援 寬色域色彩空間(已在 Flutter 3.27 中新增),CupertinoDynamicColor 中的某些屬性和方法已被棄用,以使其與 Color 類保持一致。

背景

#

Color 類已更新以支援寬色域色彩空間,但由於 CupertinoDynamicColor 的實現方式,而不是 Color 的擴充套件,一些相應的棄用並未立即應用於 CupertinoDynamicColor

變更說明

#
  1. CupertinoDynamicColor.red 欄位已被棄用,推薦使用 CupertinoDynamicColor.r
  2. CupertinoDynamicColor.green 已被棄用,推薦使用 CupertinoDynamicColor.g
  3. CupertinoDynamicColor.blue 已被棄用,推薦使用 CupertinoDynamicColor.b
  4. CupertinoDynamicColor.opacity 已被棄用,推薦使用 CupertinoDynamicColor.a
  5. CupertinoDynamicColor.withOpacity() 已被棄用,推薦使用 CupertinoDynamicColor.withValues()

遷移指南

#

訪問顏色分量

#

如果您的應用訪問單個顏色分量,請考慮利用浮點分量。短期內,您可以自己縮放分量。

dart
int _floatToInt8(double x) {
  return (x * 255.0).round().clamp(0, 255);
}

const CupertinoDynamicColor color = CupertinoColors.systemBlue;
final intRed = _floatToInt8(color.r);
final intGreen = _floatToInt8(color.g);
final intBlue = _floatToInt8(color.b);

不透明度

#

在 Flutter 3.27 之前,Color 具有“不透明度”的概念,體現在 opacitywithOpacity() 方法中。自 Flutter 3.27 起,alpha 值儲存為浮點數。使用 .a.withValues() 將提供浮點值的完整表示,並且不會被量化(限制在有限的範圍內)。這意味著“alpha”更準確地表達了“不透明度”的意圖。

遷移 opacity

#
dart
// Before: Access the alpha channel as a (converted) floating-point value.
final x = color.opacity;

// After: Access the alpha channel directly.
final x = color.a;

遷移 withOpacity

#
dart
// Before: Create a new color with the specified opacity.
final x = color.withOpacity(0.5);

// After: Create a new color with the specified alpha channel value,
// accounting for the current or specified color space.
final x = color.withValues(alpha: 0.5);

時間線

#

生效版本:尚未釋出
穩定版釋出:暫無

參考資料

#

相關指南

相關問題

相關 PR