宽色域 CupertinoDynamicColor 迁移指南
概述
#为了支持 宽色域色彩空间(已在 Flutter 3.27 中添加),CupertinoDynamicColor 中的某些属性和方法已被弃用,以使其与 Color 类保持一致。
背景
#Color 类已更新以支持宽色域色彩空间,但由于 CupertinoDynamicColor 的实现方式,而不是 Color 的扩展,一些相应的弃用并未立即应用于 CupertinoDynamicColor。
变更说明
#CupertinoDynamicColor.red字段已被弃用,推荐使用CupertinoDynamicColor.r。CupertinoDynamicColor.green已被弃用,推荐使用CupertinoDynamicColor.g。CupertinoDynamicColor.blue已被弃用,推荐使用CupertinoDynamicColor.b。CupertinoDynamicColor.opacity已被弃用,推荐使用CupertinoDynamicColor.a。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 具有“不透明度”的概念,体现在 opacity 和 withOpacity() 方法中。自 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