Skip to main content

AppBar 主題色彩參數棄用

為了提升 API 一致性,AppBarTheme 和 AppBarThemeData 中的 color 參數已被棄用,請改用 backgroundColor。

摘要

#

AppBarThemeAppBarThemeData 的建構函式,以及它們的 copyWith 方法中的 color 參數已被棄用。請改用 backgroundColor。此變更會影響 AppBar 主題的設定方式,並可能在既有程式碼中產生棄用警告。

背景說明

#

AppBar 主題化(theming)系統中,原本有兩個參數可控制相同屬性:colorbackgroundColor。這樣的重複設計造成 API 使用上的混淆與不一致。為了提升清晰度與一致性,color 參數已被棄用,請改用 backgroundColor

此次棄用影響以下類別與方法:

  • AppBarTheme 建構函式
  • AppBarTheme.copyWith 方法
  • AppBarThemeData 建構函式
  • AppBarThemeData.copyWith 方法

當你在使用已棄用的 color 參數時,將會看到如下警告:

txt
'color' is deprecated and shouldn't be used. Use backgroundColor instead.
This feature was deprecated after v3.33.0-0.2.pre.

這些類別同時包含了斷言檢查,以防止同時使用這兩個參數:

txt
The color and backgroundColor parameters mean the same thing. Only specify one.

遷移指南

#

請將所有在 AppBarThemeAppBarThemeData 建構函式以及 copyWith 方法中使用的 color 參數,替換為 backgroundColor

遷移前的程式碼:

dart
// AppBarTheme constructor
AppBarTheme(
  color: Colors.blue,
  elevation: 4.0,
)

// AppBarTheme copyWith
theme.copyWith(
  color: Colors.red,
  elevation: 2.0,
)

// AppBarThemeData constructor
AppBarThemeData(
  color: Colors.green,
  elevation: 4.0,
)

// AppBarThemeData copyWith
themeData.copyWith(
  color: Colors.purple,
  elevation: 2.0,
)

遷移後的程式碼:

dart
// AppBarTheme constructor
AppBarTheme(
  backgroundColor: Colors.blue,
  elevation: 4.0,
)

// AppBarTheme copyWith
theme.copyWith(
  backgroundColor: Colors.red,
  elevation: 2.0,
)

// AppBarThemeData constructor
AppBarThemeData(
  backgroundColor: Colors.green,
  elevation: 4.0,
)

// AppBarThemeData copyWith
themeData.copyWith(
  backgroundColor: Colors.purple,
  elevation: 2.0,
)

時程

#

合併於版本:3.33.0-0.2.pre
穩定版發佈於:3.35.4

參考資料

#

API 文件:

相關 PR: