Skip to main content

v3.16 之後移除的已棄用 API

以下在 Flutter 達到生命週期終止後, 已從 Flutter 移除的已棄用 API。

摘要

#

根據 Flutter 的 棄用政策, 在 3.16 穩定版發佈後達到生命週期終止的 已棄用 API 已被移除。

所有受影響的 API 已彙整於此主要來源, 以協助您進行遷移。 如需進一步協助遷移,請參考這份 快速參考表

變更內容

#

本節依套件及受影響的類別列出棄用項目。

Button styleFrom 屬性

#

套件:flutter 支援 Flutter Fix:是

TextButtonElevatedButton 以及 OutlinedButton 這三個元件 (Widget) 皆有一個 靜態的 styleFrom 方法用於產生 ButtonStyle。在 v3.1 版本中, 以下各類別該方法的顏色屬性已被棄用:

  • TextButton.styleFrom
    • primary
    • onSurface
  • ElevatedButton.styleFrom
    • primary
    • onPrimary
    • onSurface
  • OutlinedButton.styleFrom
    • primary
    • onSurface

這些變更讓 API 更符合最新的 Material Design 規範。這些調整也讓按鈕顏色的應用方式更加明確, 透過以 backgroundColorforegroundColordisabledForegroundColor 取代上述屬性來達成。

遷移指南

遷移前的程式碼:

dart
TextButton.styleFrom(
  primary: Colors.red,
  onSurface: Colors.black,
);
ElevatedButton.styleFrom(
  primary: Colors.red,
  onPrimary: Colors.blue,
  onSurface: Colors.black,
);
OutlinedButton.styleFrom(
  primary: Colors.red,
  onSurface: Colors.black,
);

遷移後的程式碼:

dart
TextButton.styleFrom(
  foregroundColor: Colors.red,
  disabledForegroundColor: Colors.black,
);
ElevatedButton.styleFrom(
  backgroundColor: Colors.red,
  foregroundColor: Colors.blue,
  disabledForegroundColor: Colors.black,
);
OutlinedButton.styleFrom(
  foregroundColor: Colors.red,
  disabledForegroundColor: Colors.black,
);

參考資料

API 文件:

相關 PR:


ThemeData.selectedRowColor

#

套件:flutter 支援 Flutter Fix:是

ThemeDataselectedRowColor 屬性於 v3.1 被標記為已棄用。

此屬性已不再被框架使用,因為原本使用它的元件已遷移至其他元件主題, 或是在新版 Material Design 規範下已不再需要此屬性。

遷移指南

遷移前的程式碼:

dart
ThemeData(
  // ...
  selectedRowColor: Colors.pink, // Would have no effect.
);

遷移後的程式碼:

dart
ThemeData(
  // ...
  // Remove uses.
);

參考資料

API 文件:

相關 PR:


#

套件:flutter 支援 Flutter Fix:是

NavigatorStatefocusScopeNode 屬性於 v3.1 被標記為已棄用。

此變更是為了解決由 Navigator 引入的 FocusScopeNode 所產生的多項問題。 取而代之的是,FocusScope 現在會包覆 WidgetsApp 中最上層的 NavigatorNavigatorState 也被調整為包含自己的 FocusNode, 並可透過其 FocusNode.enclosingScope 來存取正確的 FocusScopeNode

遷移指南

遷移前的程式碼:

dart
Navigator.of(context).focusScopeNode;

遷移後的程式碼:

dart
Navigator.of(context).focusNode.enclosingScope!;

參考資料

API 文件:

相關 PR:


PlatformMenuBar.body

#

套件:flutter 支援 Flutter Fix:是

PlatformMenuBarbody 屬性於 v3.1 被標記為已棄用。

此變更是為了讓 PlatformMenuBar 與框架中其他元件保持一致, 將其重新命名為 child

遷移指南

遷移前的程式碼:

dart
PlatformMenuBar(
  body: myWidget,
);

遷移後的程式碼:

dart
PlatformMenuBar(
  child: myWidget,
);

參考資料

API 文件:

相關 PR:


先前公告TextThemeWidgetInspectorServiceWidgetInspectorServiceExtensions 棄用項目,在本次週期中尚未移除。 WidgetInspectorServiceWidgetInspectorServiceExtensionssetPubRootDirectories 上的棄用時程已延長一年,以便 IDE 及其他客戶進行遷移。 預計 TextTheme 的棄用項目將於下個週期移除,屆時會再次公告。


時程

#

穩定版發佈於:3.19.0