摘要

#

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

所有受影響的 API 已彙整於此 主要來源,以協助遷移。 同時也提供了快速參考表

變更內容

#

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

GestureRecognizer.kind 及其子類別

#

Flutter Fix 支援:是

GestureRecognizer.kind 已於 v2.3 棄用。 請改用 GestureRecognizer.supportedDevices

此變更同樣影響 GestureRecognizer 的所有子類別:

  • EagerGestureRecognizer
  • ForcePressGestureRecognizer
  • LongPressGestureRecognizer
  • DragGestureRecognizer
  • VerticalDragGestureRecognizer
  • HorizontalDragGestureRecognizer
  • MultiDragGestureRecognizer
  • ImmediateMultiDragGestureRecognizer
  • HorizontalMultiDragGestureRecognizer
  • VerticalMultiDragGestureRecognizer
  • DelayedMultiDragGestureRecognizer
  • DoubleTapGestureRecognizer
  • MultiTapGestureRecognizer
  • OneSequenceGestureRecognizer
  • PrimaryPointerGestureRecognizer
  • ScaleGestureRecognizer

此變更允許手勢可辨識多個裝置, 而不再僅限於 kind 所提供的單一選項。

遷移指南

遷移前的程式碼:

dart
var myRecognizer = GestureRecognizer(
  kind: PointerDeviceKind.mouse,  
);

遷移後的程式碼:

dart
var myRecognizer = GestureRecognizer(
  supportedDevices: <PointerDeviceKind>[ PointerDeviceKind.mouse ],
);

參考資料

API 文件:

相關 PR:


ThemeData accentColoraccentColorBrightnessaccentColorTextThemeaccentColorIconThemebuttonColor

#

Flutter Fix 支援:是

accentColoraccentColorBrightnessaccentColorTextThemeaccentColorIconThemebuttonColor 這些 ThemeData 的屬性已於 v2.3 被標記為已淘汰。

此變更讓 ThemeData 更加符合 Material Design 指南。這也讓主題化(theming)更為清晰,因為現在可依賴核心色彩方案或個別元件主題來達到所需的樣式。

accentColorBrightnessaccentColorTextThemeaccentColorIconThemebuttonColor 已不再被框架使用。 應移除相關參考。

對於 ThemeData.accentColor 的使用,請改為使用 ThemeData.colorScheme.secondary

遷移指南

#

遷移前的程式碼:

dart
var myTheme = ThemeData(
  //...
  accentColor: Colors.blue,
  //...
);
var color = myTheme.accentColor;

遷移後的程式碼:

dart
var myTheme = ThemeData(
  //...
  colorScheme: ColorScheme(
    //...
    secondary:Colors.blue,
    //...
  ),
  //...
);
var color = myTheme.colorScheme.secondary;

參考資料

API 文件:

相關議題:

相關 PR:

已棄用於:

已移除於:


AppBarSliverAppBarAppBarTheme 更新

#

Flutter Fix 支援:是

在 v2.4 版本中,為了更符合 Material Design,對 app bar 類別及其主題進行了多項調整。當時有數個屬性被標記為棄用,現已被移除。

針對 AppBarSliverAppBarAppBarTheme

  • brightness 已被移除,請改用 systemOverlayStyle
  • textTheme 已被移除,請改用 toolbarTextStyletitleTextStyle 其中之一。
  • backwardsCompatibility 可直接移除,因為它僅為這些屬性的暫時遷移旗標。

此外,AppBarTheme.color 也已被移除,請改用 AppBarTheme.backgroundColor 作為替代。

遷移指南

遷移前的程式碼:

dart
var toolbarTextStyle = TextStyle(...);
var titleTextStyle = TextStyle(...);
AppBar(
  brightness: Brightness.light,
  textTheme: TextTheme(
    bodyMedium: toolbarTextStyle,
    titleLarge: titleTextStyle,
  )
  backwardsCompatibility: true,
);
AppBarTheme(color: Colors.blue);

遷移後的程式碼:

dart
var toolbarTextStyle = TextStyle(...);
var titleTextStyle = TextStyle(...);
AppBar(
  systemOverlayStyle: SystemOverlayStyle(statusBarBrightness: Brightness.light),
  toolbarTextStyle: toolbarTextStyle,
  titleTextStyle: titleTextStyle,
);
AppBarTheme(backgroundColor: Colors.blue);

參考資料

API 文件:

相關議題:

已棄用於:

已移除於:


SystemChrome.setEnabledSystemUIOverlays

#

Flutter Fix 支援:是

在 v2.3 版本中,SystemChrome.setEnabledSystemUIOVerlays 這個用於設定裝置系統層級覆蓋(如狀態列與導覽列)的靜態方法已被棄用,建議改用 SystemChrome.setEnabledSystemUIMode

這項變更讓開發者可以設定符合原生 Android 應用程式設計(如 edge to edge)的常見全螢幕模式。

若仍需手動設定 overlays,而非選擇特定模式,仍可透過 SystemUiMode.manual 來實現,開發者可以如以往一樣傳入相同的 overlays 清單。

遷移指南

遷移前的程式碼:

dart
SystemChrome.setEnabledSystemUIOverlays(<SystemUiOverlay>[
  SystemUiOverlay.top,
  SystemUiOverlay.bottom,
]);

遷移後的程式碼:

dart
SystemChrome.setEnabledSystemUIMode(
  SystemUiMode.manual,
  overlays: <SystemUiOverlay>[
    SystemUiOverlay.top,
    SystemUiOverlay.bottom,
  ],
);

參考資料

API 文件:

相關議題:

已棄用於:

已移除於:


SystemNavigator.routeUpdated

#

Flutter Fix 支援:是

在 v2.3 版本中,SystemNavigator.routeUpdated 已被棄用,建議改用 SystemNavigator.routeInformationUpdated

為了避免有兩種方式向引擎回報當前路由,這項變更將所有相關操作統一到一個 API,當建立一個能回報路由的 Navigator 時,會分別選擇單一項目的歷史模式。

遷移指南

遷移前的程式碼:

dart
SystemNavigator.routeUpdated(routeName: 'foo', previousRouteName: 'bar');

遷移後的程式碼:

dart
SystemNavigator.routeInformationUpdated(location: 'foo');

參考資料

API 文件:

相關議題:

已棄用於:

已移除於:


AnimatedSize.vsync

#

Flutter Fix 支援:是

在 v2.2 版本中,AnimatedSize.vsyc 已被棄用。自從 AnimatedSize 被轉換為 StatefulWidget,且其 State 混入了 SingleTickerProviderStateMixin 之後,此屬性已不再需要。這項變更是為了解決記憶體洩漏的問題。

應移除對 vsync 的使用,因為現在由 AnimatedSize 處理該屬性。

遷移指南

遷移前的程式碼:

dart
AnimatedSize(
  vsync: this,
  // ...
);

遷移後的程式碼:

dart
AnimatedSize(
  // ...
);

參考資料

API 文件:

已棄用於:

已移除於:


時程

#

在穩定版本:3.10