Skip to main content

wide gamut CupertinoDynamicColor 遷移指南

解決先前遺漏的 CupertinoDynamicColor 棄用項目,以配合 wide gamut Color API。

摘要

#

為了配合 Color 類別,部分 CupertinoDynamicColor 的屬性與方法已被棄用,這是因為 Flutter 3.27 新增了 wide gamut 色域空間 的支援。

背景說明

#

Color 類別已更新以支援 wide gamut 色域空間,但由於其實作方式,而非繼承自 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);

透明度(Opacity)

#

在 Flutter 3.27 之前,Color 採用了「透明度(opacity)」的概念,這在 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);

時程

#

合併於版本:3.36.0-0.1.pre
穩定版本釋出:3.38

參考資料

#

相關指南:

相關議題:

相關 PR: