跳至主要內容

v2.10 之後移除的已棄用 API

摘要

#

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

所有受影響的 API 都已編譯到此主要來源中,以協助遷移。此外,還提供快速參考表

變更

#

本節列出按受影響類別分類的棄用。


#

Flutter Fix 支援:是

maxLengthEnforced 已在 v1.25 中棄用。

請改用 maxLengthEnforcement。將 maxLengthEnforced 為 true 的地方替換為 MaxLengthEnforcement.enforce。將 maxLengthEnforced 為 false 的地方替換為 MaxLengthEnforcement.none。此變更允許指定比原始二元選擇更多的行為,新增 MaxLengthEnforcement.truncateAfterCompositionEnds 作為額外選項。

以下類別都具有相同的 API 變更

  • TextField
  • TextFormField
  • CupertinoTextField

遷移指南

提供深入的遷移指南

遷移前的程式碼

dart
const TextField textField = TextField(maxLengthEnforced: true);
const TextField textField = TextField(maxLengthEnforced: false);
final lengthEnforced = textField.maxLengthEnforced;

const TextFormField textFormField = TextFormField(maxLengthEnforced: true);
const TextFormField textFormField = TextFormField(maxLengthEnforced: false);
final lengthEnforced = textFormField.maxLengthEnforced;

const CupertinoTextField cupertinoTextField = CupertinoTextField(maxLengthEnforced: true);
const CupertinoTextField cupertinoTextField = CupertinoTextField(maxLengthEnforced: false);
final lengthEnforced = cupertinoTextField.maxLengthEnforced;

遷移後的程式碼

dart
const TextField textField = TextField(maxLengthEnforcement: MaxLengthEnforcement.enforce);
const TextField textField = TextField(maxLengthEnforcement: MaxLengthEnforcement.none);
final lengthEnforced = textField.maxLengthEnforcement;

const TextFormField textFormField = TextFormField(maxLengthEnforcement: MaxLengthEnforcement.enforce);
const TextFormField textFormField = TextFormField(maxLengthEnforcement: MaxLengthEnforcement.none);
final lengthEnforced = textFormField.maxLengthEnforcement;

const CupertinoTextField cupertinoTextField = CupertinoTextField(maxLengthEnforcement: MaxLengthEnforcement.enforce);
const CupertinoTextField cupertinoTextField = CupertinoTextField(maxLengthEnforcement: MaxLengthEnforcement.none);
final lengthEnforced = cupertinoTextField.maxLengthEnforcement;

參考

API 文件

相關問題

相關 PR


VelocityTracker 建構函式

#

Flutter Fix 支援:是

VelocityTracker 的預設建構函式已在 v1.22 中棄用。

應改用 VelocityTracker.withKind()。這允許為追蹤器指定 PointerDeviceKindVelocityTracker.kind 先前的預設值為 PointerDeviceKind.touch

遷移指南

遷移前的程式碼

dart
final VelocityTracker tracker = VelocityTracker();

遷移後的程式碼

dart
final VelocityTracker tracker = VelocityTracker.withKind(PointerDeviceKind.touch);

參考

API 文件

相關 PR


DayPickerMonthPicker

#

Flutter Fix 支援:否

DayPickerMonthPicker 小工具已在 v1.15 中首次棄用,然後在 v1.26 中擴展。

它們已被一個全面的小工具 CalendarDatePicker 取代。

這些小工具是使用 showDatePicker 方法顯示的。此方法已在此版本之前遷移以呈現新的 CalendarDatePicker,因此最終移除它們不應需要進一步的操作。

參考

設計文件

API 文件

相關問題

相關 PR


FlatButtonRaisedButtonOutlineButton

#

Flutter Fix 支援:否

FlatButtonRaisedButtonOutlineButton 小工具已在 v1.20 中首次棄用,然後在 v1.26 中擴展。

它們被新的按鈕 TextButtonElevatedButtonOutlinedButton 取代。這些新的小工具也使用新的相關主題,而不是通用的 ButtonTheme

舊的小工具舊的主題新的小工具新的主題
FlatButtonButtonThemeTextButtonTextButtonTheme
RaisedButtonButtonThemeElevatedButtonElevatedButtonTheme
OutlineButtonButtonThemeOutlinedButtonOutlinedButtonTheme

遷移指南

提供深入的遷移指南以了解詳細的樣式設定

遷移前的程式碼

dart
FlatButton(
  onPressed: onPressed,
  child: Text('Button'),
  // ...
);

RaisedButton(
  onPressed: onPressed,
  child: Text('Button'),
  // ...
);

OutlineButton(
  onPressed: onPressed,
  child: Text('Button'),
  // ...
);

遷移後的程式碼

dart
TextButton(
  onPressed: onPressed,
  child: Text('Button'),
  // ...
);

ElevatedButton(
  onPressed: onPressed,
  child: Text('Button'),
  // ...
);

OutlinedButton(
  onPressed: onPressed,
  child: Text('Button'),
  // ...
);

參考

設計文件

API 文件

相關 PR


Scaffold SnackBar 方法

#

Flutter Fix 支援:否

以下 Scaffold SnackBar 方法已在 v1.23 中棄用。

  • showSnackBar
  • removeCurrentSnackBar
  • hideCurrentSnackBar

應改用 ScaffoldMessenger 的相同名稱方法。每個 MaterialApp 中都已建立預設的 ScaffoldMessenger

遷移指南

提供深入的遷移指南

遷移前的程式碼

dart
Scaffold.of(context).showSnackBar(mySnackBar);
Scaffold.of(context).removeCurrentSnackBar(mySnackBar);
Scaffold.of(context).hideCurrentSnackBar(mySnackBar);

遷移後的程式碼

dart
ScaffoldMessenger.of(context).showSnackBar(mySnackBar);
ScaffoldMessenger.of(context).removeCurrentSnackBar(mySnackBar);
ScaffoldMessenger.of(context).hideCurrentSnackBar(mySnackBar);

參考

設計文件

影片內容

API 文件

相關問題

相關 PR


RectangularSliderTrackShape.disabledThumbGapWidth

#

Flutter Fix 支援:是

RectangularSliderTrackShape.disabledThumbGapWidth 已在 v1.5 中首次棄用,然後在 v1.26 中擴展。

框架已不再使用此項目,因為停用時不再發生滑桿拇指的動畫。

遷移指南

遷移前的程式碼

dart
RectangularSliderTrackShape(disabledThumbGapWidth: 2.0);

遷移後的程式碼

dart
RectangularSliderTrackShape();

參考

API 文件

相關 PR


ThemeData 的文字選取移至 TextSelectionThemeData

#

Flutter Fix 支援:是

以下 ThemeData 成員已在 v1.23 中首次棄用,然後在 v1.26 中擴展。

  • useTextSelectionTheme
  • textSelectionColor
  • cursorColor
  • textSelectionHandleColor

應將這些項目替換為更全面的 TextSelectionThemeData,現在在 ThemeData 本身中指定。

useTextSelectionTheme 旗標作為臨時遷移旗標,用於區分兩個 API,現在可以移除。

遷移指南

提供深入的遷移指南

遷移前的程式碼

dart
ThemeData(
  useTextSelectionTheme: false,
  textSelectionColor: Colors.blue,
  cursorColor: Colors.green,
  textSelectionHandleColor: Colors.red,
);

遷移後的程式碼

dart
ThemeData(
  textSelectionTheme: TextSelectionThemeData(
    selectionColor: Colors.blue,
    cursorColor: Colors.green,
    selectionHandleColor: Colors.red,
  ),
);

參考

設計文件

API 文件

相關問題

相關 PR


RenderEditable.onSelectionChanged 移至 TextSelectionDelegate.textEditingValue

#

Flutter Fix 支援:否

RenderEditable.onSelectionChangedTextSelectionDelegate.textEditingValue 已在 v1.26 中棄用。

請改為呼叫 TextSelectionDelegate.userUpdateTextEditingValue,而不是呼叫其中一個或兩個方法。這修正了一個錯誤,其中 TextInputFormatter 會收到錯誤的選取值。

遷移指南

遷移前的程式碼

dart
renderEditable.onSelectionChanged(selection, renderObject, cause);
textSelectionDelegate.textEditingValue = value;

遷移後的程式碼

dart
textSelectionDelegate.userUpdateTextEditingValue(value, cause);

參考

API 文件

相關問題

相關 PR


Stack.overflow

#

Flutter Fix 支援:是

Stack.overflow 以及 Overflow 列舉已在 v1.22 中棄用。

替代項目是 Stack.clipBehavior,這是為了統一整個框架中的裁剪行為和語義而進行的變更。使用 Overflow.visible 的地方請使用 Clip.none。使用 Overflow.clip 的地方請使用 Clip.hardEdge

遷移指南

提供深入的遷移指南

遷移前的程式碼

dart
const Stack stack = Stack(overflow: Overflow.visible);
const Stack stack = Stack(overflow: Overflow.clip);

遷移後的程式碼

dart
const Stack stack = Stack(clipBehavior: Clip.none);
const Stack stack = Stack(clipBehavior: Clip.hardEdge);

參考

API 文件

相關問題

相關 PR


UpdateLiveRegionEvent

#

Flutter Fix 支援:否

SemanticsEvent UpdateLiveRegionEvent 已在 v1.12 中首次棄用,然後在 v1.26 中擴展。

框架從未實作此項目,應移除任何參考。

參考

API 文件

相關 PR


RenderObjectElement 方法

#

Flutter Fix 支援:是

以下 RenderObjectElement 方法已在 v1.21 中棄用。

  • insertChildRenderObject
  • moveChildRenderObject
  • removeChildRenderObject

這些方法分別由以下方法取代

  • insertRenderObjectChild
  • moveRenderObjectChild
  • removeRenderObjectChild

這些變更作為軟性中斷棄用而進行,以便變更函式簽名。

遷移指南

遷移前的程式碼

dart
element.insertChildRenderObject(child, slot);
element.moveChildRenderObject(child, slot);
element.removeChildRenderObject(child);

遷移後的程式碼

dart
element.insertRenderObjectChild(child, slot);
element.moveRenderObjectChild(child, oldSlot, newSlot);
element.removeRenderObjectChild(child, slot);

參考

API 文件

相關問題

相關 PR


時間軸

#

在穩定版發行:3.0.0