跳至主要内容

v3.3 后移除的已弃用 API

摘要

#

根据 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

此更改允许在与其他 widget(如 StackInteractiveViewer)结合使用时,更准确地反馈可拖动 widget。

迁移指南

迁移前代码

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 使用,用于在适当的平台上默认应用过度滚动指示器(如 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