概述

#

根據 Flutter 的棄用策略,在 3.3 穩定版釋出後達到生命週期結束的已棄用 API 已經被移除。

所有受影響的 API 都已編譯到此主要原始檔中,以協助遷移。同時提供了一個快速參考表

變更

#

本節列出了按受影響類別的棄用內容。

RenderUnconstrainedBox

#

Flutter Fix 支援:否

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

在雙軸均無約束的情況下,為 constraintsTransform 提供 ConstraintsTransformBox.unconstrained

如果之前設定了 RenderUnconstrainedBox.constrainedAxis,則相應地替換

  • 如果之前 constrainedAxisAxis.horizontal,則將 constraintsTransform 設定為 ConstraintsTransformBox.widthUnconstrained
  • 如果之前 constrainedAxisAxis.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

此更改允許拖動控制元件在與 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 控制元件使用此方法預設在適當的平臺上應用滾動溢位指示器,如 GlowingOverscrollIndicator。隨著添加了更多預設裝飾器(如 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