Skip to main content

3.3 版本后移除的已弃用 API

摘要

#

根据 Flutter 的弃用策略,3.3 稳定版本发布后已达到生命周期终点的弃用 API 已被移除。

所有受影响的 API 都已编译到此主要资源中,以帮助迁移。还提供了一个快速参考表

变更

#

本节列出了按受影响的类列出的弃用项。

RenderUnconstrainedBox

#

Flutter Fix 支持:否

RenderUnconstrainedBox 在 v2.1 中已弃用。 请改用 RenderConstraintsTransformBox

在两个轴上都不受约束的情况下,请将 ConstraintsTransformBox.unconstrained 提供给 constraintsTransform

如果之前设置了 RenderUnconstrainedBox.constrainedAxis,请分别替换:

  • 如果之前 constrainedAxisAxis.horizontal,则将 constraintsTransform 设置为 ConstraintsTransformBox.widthUnconstrained
  • 如果之前 constrainedAxisAxis.vertical,则将 constraintsTransform 设置为 ConstraintsTransformBox.heightUnconstrained

此更改允许通过 ConstraintsTransformBox 引入更多类型的约束转换。旧 API 的其他参数与新 API 兼容。

迁移指南

迁移前的代码:

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

// 水平轴约束
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,
);

// 垂直轴约束
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
// 无约束
final RenderConstraintsTransformBox unconstrained = RenderConstraintsTransformBox(
  constraintsTransform: ConstraintsTransformBox.unconstrained,
  textDirection: TextDirection.ltr,
  child: RenderConstrainedBox(
    additionalConstraints: const BoxConstraints.tightFor(height: 200.0),
  ),
  alignment: Alignment.center,
);

// 水平轴约束
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,
);

// 垂直轴约束
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 等其他 widget 结合使用时,更准确地反馈可拖动 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 文档:

相关的 issue:

相关的 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 文档:

相关的 issue:

相关的 PR:


时间线

#

稳定版本:3.7