跳至主要內容

v3.19 版後已移除的已棄用 API

摘要

#

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

所有受影響的 API 都已編譯到這個主要來源中,以協助遷移。為了進一步協助您遷移,請查看這個快速參考表

變更

#

本節列出按套件和受影響的類別分類的棄用項目。

TextTheme

#

套件:flutter Flutter Fix 支援:是

TextTheme 的幾個 TextStyle 屬性在 v3.1 中已棄用,以支援 Material Design 規範中的新樣式。它們在下表中與新 API 中的適當替換項目一起列出。

棄用新 API
headline1displayLarge
headline2displayMedium
headline3displaySmall
headline4headlineMedium
headline5headlineSmall
headline6titleLarge
subtitle1titleMedium
subtitle2titleSmall
bodyText1bodyLarge
bodyText2bodyMedium
captionbodySmall
buttonlabelLarge
overlinelabelSmall

遷移指南

遷移前的程式碼

dart
// TextTheme
// Base constructor
TextTheme(
  headline1: headline1Style,
  headline2: headline2Style,
  headline3: headline3Style,
  headline4: headline4Style,
  headline5: headline5Style,
  headline6: headline6Style,
  subtitle1: subtitle1Style,
  subtitle2: subtitle2Style,
  bodyText1: bodyText1Style,
  bodyText2: bodyText2Style,
  caption: captionStyle,
  button: buttonStyle,
  overline: overlineStyle,
);

// copyWith
TextTheme.copyWith(
  headline1: headline1Style,
  headline2: headline2Style,
  headline3: headline3Style,
  headline4: headline4Style,
  headline5: headline5Style,
  headline6: headline6Style,
  subtitle1: subtitle1Style,
  subtitle2: subtitle2Style,
  bodyText1: bodyText1Style,
  bodyText2: bodyText2Style,
  caption: captionStyle,
  button: buttonStyle,
  overline: overlineStyle,
);

// Getters
TextStyle style;
style = textTheme.headline1,
style = textTheme.headline2,
style = textTheme.headline3,
style = textTheme.headline4,
style = textTheme.headline5,
style = textTheme.headline6,
style = textTheme.subtitle1,
style = textTheme.subtitle2,
style = textTheme.bodyText1,
style = textTheme.bodyText2,
style = textTheme.caption,
style = textTheme.button,
style = textTheme.overline,

遷移後的程式碼

dart
// TextTheme
// Base constructor
TextTheme(
  displayLarge: headline1Style,
  displayMedium: headline2Style,
  displaySmall: headline3Style,
  headlineMedium: headline4Style,
  headlineSmall: headline5Style,
  titleLarge: headline6Style,
  titleMedium: subtitle1Style,
  titleSmall: subtitle2Style,
  bodyLarge: bodyText1Style,
  bodyMedium: bodyText2Style,
  bodySmall: captionStyle,
  labelLarge: buttonStyle,
  labelSmall: overlineStyle,
);

TextTheme.copyWith(
  displayLarge: headline1Style,
  displayMedium: headline2Style,
  displaySmall: headline3Style,
  headlineMedium: headline4Style,
  headlineSmall: headline5Style,
  titleLarge: headline6Style,
  titleMedium: subtitle1Style,
  titleSmall: subtitle2Style,
  bodyLarge: bodyText1Style,
  bodyMedium: bodyText2Style,
  bodySmall: captionStyle,
  labelLarge: buttonStyle,
  labelSmall: overlineStyle,
);

TextStyle style;
style = textTheme.displayLarge;
style = textTheme.displayMedium;
style = textTheme.displaySmall;
style = textTheme.headlineMedium;
style = textTheme.headlineSmall;
style = textTheme.titleLarge;
style = textTheme.titleMedium;
style = textTheme.titleSmall;
style = textTheme.bodyLarge;
style = textTheme.bodyMedium;
style = textTheme.bodySmall;
style = textTheme.labelLarge;
style = textTheme.labelSmall;

參考資料

API 文件

相關的 PR


ThemeData

#

套件:flutter Flutter Fix 支援:是

ThemeData 的幾個 Color 屬性在 v3.3 中已棄用,以支援 Material Design 規範中的新樣式。這些顏色是 errorColorbackgroundColorbottomAppBarColortoggleableActiveColor。前兩個由 ThemeData.colorScheme 的屬性取代,而 bottomAppBarColor 由元件主題 BottomAppBarTheme 的顏色取代。toggleableActiveColor 不再被框架使用,因此被移除。

遷移指南

遷移前的程式碼

dart
var myTheme = ThemeData(
  //...
  errorColor: Colors.red,
  backgroundColor: Colors.blue,
  bottomAppBarColor: Colors.purple,
  toggleableActiveColor: Colors.orange,
  //...
);
var errorColor = myTheme.errorColor;
var backgroundColor = myTheme.backgroundColor;
var bottomAppBarColor = myTheme.bottomAppBarColor;
var toggleableActiveColor = myTheme.toggleableActiveColor;

遷移後的程式碼

dart
var myTheme = ThemeData(
  //...
  colorScheme: ColorScheme(
    /// ...
    error: Colors.red,
    background: Colors.blue,
  ),
  bottomAppBarTheme: BottomAppBarTheme(
    color: Colors.purple,
  ),
  //...
);
var errorColor = myTheme.colorScheme.error;
var backgroundColor = myTheme.colorScheme.background;
var bottomAppBarColor = myTheme.bottomAppBarTheme.color;
var toggleableActiveColor = Colors.orange;

參考資料

API 文件

相關的 PR


CupertinoContextMenu.previewBuilder

#

套件:flutter Flutter Fix 支援:是

在 v3.4 後,previewBuilderCupertinoContextMenubuilder 取代。透過新增 builder,涵蓋了上下文選單執行的所有動畫,其中後半部分由 previewBuilder 執行,並由 CupertinoContextMenu.animationOpensAt 劃分。

遷移指南

遷移前的程式碼

dart
CupertinoContextMenu(
  previewBuilder: (BuildContext context, Animation<double> animation, Widget child) {
    return FittedBox(
      fit: BoxFit.cover,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(64.0 * animation.value),
        child: Image.asset('assets/photo.jpg'),
      ),
    );
  },
  actions: <Widget>[
    CupertinoContextMenuAction(
      child: const Text('Action one'),
      onPressed: () {},
    ),
  ],
  child: FittedBox(
    fit: BoxFit.cover,
    child: Image.asset('assets/photo.jpg'),
  ),
);

遷移後的程式碼

dart
CupertinoContextMenu(
  actions: <Widget>[
    CupertinoContextMenuAction(
      child: const Text('Action one'),
      onPressed: () {},
    ),
  ],
  builder: (BuildContext context, Animation<double> animation) {
    final Animation<BorderRadius?> borderRadiusAnimation = BorderRadiusTween(
      begin: BorderRadius.circular(0.0),
      end: BorderRadius.circular(CupertinoContextMenu.kOpenBorderRadius),
    ).animate(
      CurvedAnimation(
        parent: animation,
        curve: Interval(
          CupertinoContextMenu.animationOpensAt,
          1.0,
        ),
      ),
    );

    final Animation<Decoration> boxDecorationAnimation = DecorationTween(
      begin: const BoxDecoration(
        color: Color(0xFFFFFFFF),
        boxShadow: <BoxShadow>[],
      ),
      end: BoxDecoration(
        color: Color(0xFFFFFFFF),
        boxShadow: CupertinoContextMenu.kEndBoxShadow,
      ),
    ).animate(
      CurvedAnimation(
        parent: animation,
        curve: Interval(
          0.0,
          CupertinoContextMenu.animationOpensAt,
        )
      )
    );

    return Container(
      decoration: animation.value < CupertinoContextMenu.animationOpensAt
        ? boxDecorationAnimation.value
        : null,
      child: FittedBox(
        fit: BoxFit.cover,
        child: ClipRRect(
          borderRadius: borderRadiusAnimation.value ?? BorderRadius.circular(0.0),
          child: SizedBox(
            height: 150,
            width: 150,
            child: Image.asset('assets/photo.jpg'),
          ),
        ),
      )
    );
   }
 )

參考資料

API 文件

相關的 PR


Scrollbar.showTrackOnHover

#

套件:flutter Flutter Fix 支援:是

在 v3.4 後,ScrollbarshowTrackOnHover 屬性及其相關的元件主題 ScrollbarThemeData.showTrackOnHover 被有狀態的屬性 ScrollbarThemeData.trackVisibility 取代。透過使用 trackVisibility,所有狀態的排列組合都可以影響顯示捲軸軌道,而不僅僅是懸停。

遷移指南

遷移前的程式碼

dart
Scrollbar(
  showTrackOnHover: true,
  child: //...
);
ScrollbarThemeData(
  showTrackOnHover: true,
);

遷移後的程式碼

dart
Scrollbar(
  child: //...
);
ScrollbarThemeData(
  // This will always show the track for any state.
  trackVisibility: MaterialStateProperty<bool>.all(true),
);
// Or
ScrollbarThemeData(
  // Only show on hover.
  trackVisibility: (Set<MaterialState> states) => states.contains(MaterialState.hovered),
);

參考資料

API 文件

相關的 PR


KeepAliveHandle.release 方法

#

套件:flutter Flutter Fix 支援:否

在 v3.3 後,KeepAliveHandlerelease 方法被移除,並被呼叫 dispose 取代。做出此變更是因為發現 release 經常在沒有呼叫 dispose 的情況下被呼叫,導致記憶體洩漏。dispose 方法現在執行與 release 相同的功能。

遷移指南

遷移前的程式碼

dart
KeepAliveHandle handle = KeepAliveHandle();
handle.release();
handle.dispose();

遷移後的程式碼

dart
KeepAliveHandle handle = KeepAliveHandle();
handle.dispose();

參考資料

API 文件

相關的 PR


InteractiveViewer.alignPanAxis

#

套件:flutter Flutter Fix 支援:是

在 v3.3 後,InteractiveVieweralignPanAxis 屬性被移除,並被 panAxis 取代。做出此變更是為了在 InteractiveViewer 中啟用更多平移模式。

遷移指南

遷移前的程式碼

dart
InteractiveViewer(
  alignPanAxis: true,
);

遷移後的程式碼

dart
InteractiveViewer(
  panAxis: PanAxis.aligned,
);

參考資料

API 文件

相關的 PR


MediaQuery.boldTextOverride

#

套件:flutter Flutter Fix 支援:是

在 v3.5 後,MediaQueryboldTextOverride 方法被移除,並被 boldTextOf 取代。做出此變更是作為 MediaQuery 更大重構的一部分,最值得注意的是減少了依賴於它的 Widget 所觸發的重建次數。

遷移指南

遷移前的程式碼

dart
MediaQuery.boldTextOverride(context);

遷移後的程式碼

dart
MediaQuery.boldTextOf(context)

參考資料

API 文件

相關的 PR


AnimatedList 重新命名 builder typedefs

#

套件:flutter Flutter Fix 支援:否

隨著 AnimatedGrid 的加入,AnimatedList 被重構為共享一個共同的基類。之前名為 AnimatedListItemBuilderAnimatedListRemovedItemBuilder 的名稱被重新命名,以便在 v3.5 後更好地反映它們可以使用的類別。重新命名任何對 AnimatedItemBuilderAnimatedRemovedItemBuilder 的參考。

參考資料

API 文件

相關的 PR


FlutterDriver.enableAccessibility

#

套件:flutter_driver Flutter Fix 支援:是

flutterDriverenableAccessibility 方法在 v2.3 中已棄用。它被移除並由 setSemantics 取代。此變更使得可以啟用或停用無障礙功能,而不僅僅是啟用它。

遷移指南

遷移前的程式碼

dart
FlutterDriver driver = FlutterDriver.connectedTo(
  // ...
);
driver.enableAccessibility();

遷移後的程式碼

dart
FlutterDriver driver = FlutterDriver.connectedTo(
  // ...
);
driver.setSemantics(true);

參考資料

API 文件

相關的 PR


TimelineSummary.writeSummaryToFile

#

套件:flutter_driver Flutter Fix 支援:是

TimelineSummarywriteSummaryToFile 方法在 v2.1 中已棄用。它被移除並由 writeTimelineToFile 取代。

遷移指南

遷移前的程式碼

dart
TimelineSummary summary = TimelineSummary.summarize(
  myTimeline,
);
summary.writeSummaryToFile(
  traceName,
  pretty: true,
);

遷移後的程式碼

dart
TimelineSummary summary = TimelineSummary.summarize(
  myTimeline,
);
summary.writeTimelineToFile(
  traceName,
  pretty: true,
);

參考資料

API 文件

相關的 PR

API 22 及更低版本上的 Android 平台檢視

#

Flutter Fix 支援:否

從 Flutter 3.0 開始,平台檢視需要 API 23 或更高版本。在 Flutter 3.19 中,當在執行 API 等級 22 和更低版本的 Android 裝置上使用平台檢視時,我們現在會拋出 UnsupportedOperationException。

遷移指南

將最低 API 等級設定為 23(或更高版本),或在顯示平台檢視之前檢查 Android API 等級。


先前宣布的上下文選單棄用項目,與 ToolbarOptions 以及 TextSelectionControllerSelectableRegionState 的部分相關,在本週期內未被移除,以便有更多時間進行遷移。預計這些棄用項目將在下個週期移除,屆時將再次宣布。


時間軸

#

在穩定版本中:3.22.0