概述

#

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