摘要

#

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

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

變更內容

#

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

RenderUnconstrainedBox

#

支援 Flutter Fix:否

RenderUnconstrainedBox 在 v2.1 中已被棄用。 請改用 RenderConstraintsTransformBox

當在兩個軸向皆無限制時,請提供 ConstraintsTransformBox.unconstrainedconstraintsTransform

若先前有設定 RenderUnconstrainedBox.constrainedAxis, 請分別替換為:

  • constrainedAxis 先前為 Axis.horizontal,請將 constraintsTransform 設為 ConstraintsTransformBox.widthUnconstrained
  • constrainedAxis 先前為 Axis.vertical,請將 constraintsTransform 設為 ConstraintsTransformBox.heightUnconstrained

此變更允許透過 ConstraintsTransformBox 引入更多類型的限制條件 轉換。舊 API 的其他參數與新 API 相容。

遷移指南

遷移前的程式碼:

dart
// Unconstrained
final RenderUnconstrainedBox unconstrained = RenderUnconstrainedBox(
  textDirection: TextDirection.ltr,
  child: RenderConstrainedBox(
    additionalConstraints: const BoxConstraints.tightFor(height: 200.0),
  ),
  alignment: Alignment.center,
);

// Constrained in horizontal axis
final RenderUnconstrainedBox unconstrained = RenderUnconstrainedBox(
  constrainedAxis: Axis.horizontal,
  textDirection: TextDirection.ltr,
  child: RenderConstrainedBox(
    additionalConstraints: const BoxConstraints.tightFor(width: 200.0, height: 200.0),
  ),
  alignment: Alignment.center,
);

// Constrained in vertical axis
final RenderUnconstrainedBox unconstrained = RenderUnconstrainedBox(
  constrainedAxis: Axis.vertical,
  textDirection: TextDirection.ltr,
  child: RenderFlex(
    direction: Axis.vertical,
    textDirection: TextDirection.ltr,
    children: <RenderBox>[flexible],
  ),
  alignment: Alignment.center,
);

遷移後的程式碼:

dart
// Unconstrained
final RenderConstraintsTransformBox unconstrained = RenderConstraintsTransformBox(
  constraintsTransform: ConstraintsTransformBox.unconstrained,
  textDirection: TextDirection.ltr,
  child: RenderConstrainedBox(
    additionalConstraints: const BoxConstraints.tightFor(height: 200.0),
  ),
  alignment: Alignment.center,
);

// Constrained in horizontal axis
final RenderConstraintsTransformBox unconstrained = RenderConstraintsTransformBox(
  constraintsTransform: ConstraintsTransformBox.widthUnconstrained,
  textDirection: TextDirection.ltr,
  child: RenderConstrainedBox(
    additionalConstraints: const BoxConstraints.tightFor(width: 200.0, height: 200.0),
  ),
  alignment: Alignment.center,
);

// Constrained in vertical axis
final RenderConstraintsTransformBox unconstrained = RenderConstraintsTransformBox(
  constraintsTransform: ConstraintsTransformBox.widthUnconstrained,
  textDirection: TextDirection.ltr,
  child: RenderFlex(
    direction: Axis.vertical,
    textDirection: TextDirection.ltr,
    children: <RenderBox>[flexible],
  ),
  alignment: Alignment.center,
);

參考資料

API 文件:

相關 PR:


DragAnchorDraggable.dragAnchorLongPressDraggable.dragAnchor

#

支援 Flutter Fix:是

列舉型別 DragAnchor 以及其在 Draggable.dragAnchorLongPressDraggable.dragAnchor 中的用法,已於 v2.1 標記為已淘汰。 請改用 dragAnchorStrategy

此變更讓可拖曳元件(draggable widget)在與其他元件(如 StackInteractiveViewer)搭配使用時,能提供更精確的回饋。

遷移指南

遷移前的程式碼:

dart
Draggable draggable = Draggable();
draggable = Draggable(dragAnchor: DragAnchor.child);
draggable = Draggable(dragAnchor: DragAnchor.pointer);

LongPressDraggable longPressDraggable = LongPressDraggable();
longPressDraggable = LongPressDraggable(dragAnchor: DragAnchor.child);
longPressDraggable = LongPressDraggable(dragAnchor: DragAnchor.pointer);

遷移後的程式碼:

dart
Draggable draggable = Draggable();
draggable = Draggable(dragAnchorStrategy: childDragAnchorStrategy);
draggable = Draggable(dragAnchorStrategy: pointerDragAnchorStrategy);

LongPressDraggable longPressDraggable = LongPressDraggable();
longPressDraggable = LongPressDraggable(dragAnchorStrategy: childDragAnchorStrategy);
longPressDraggable = LongPressDraggable(dragAnchorStrategy: pointerDragAnchorStrategy);

參考資料

API 文件:

相關議題:

相關 PR:


ScrollBehavior.buildViewportChrome

#

支援 Flutter Fix:是

ScrollBehavior.buildViewportChrome 方法自 v2.1 起已被棄用。

此方法過去由 Scrollable 元件(Widget)用於在適當的平台上預設套用 overscroll 指示器,例如 GlowingOverscrollIndicator。隨著更多預設裝飾器(decorator)被加入,例如 Scrollbar,每一種裝飾器都被拆分為獨立的方法,以取代 buildViewportChrome

這樣一來,繼承的類別只需透過 buildScrollbarbuildOverscrollIndicator 覆寫特定的裝飾器方法,而無需為了維護其中之一而重寫整段程式碼。

遷移指南

提供詳細遷移指南

遷移前的程式碼:

dart
final ScrollBehavior scrollBehavior = ScrollBehavior();
scrollBehavior.buildViewportChrome(context, child, axisDirection);

遷移後的程式碼:

dart
final ScrollBehavior scrollBehavior = ScrollBehavior();
scrollBehavior.buildOverscrollIndicator(context, child, axisDirection);

參考資料

設計文件:

API 文件:

相關議題:

相關 PR:


時程

#

於穩定版發布:3.7