Skip to main content

v2.10 后移除的已弃用 API

摘要

#

根据 Flutter 的弃用策略,在 2.10 稳定版发布后达到生命周期末端的已弃用 API 已被移除。

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

变更

#

本节按受影响的类列出弃用的内容。


TextField 及相关类的 maxLengthEnforced

#

Flutter Fix 支持:是

maxLengthEnforced 在 v1.25 中已弃用。

请改用 maxLengthEnforcement。 如果 maxLengthEnforced 为 true,则替换为 MaxLengthEnforcement.enforce。 如果 maxLengthEnforced 为 false,则替换为 MaxLengthEnforcement.none。 此更改允许指定超出原始二元选择的更多行为,并添加 MaxLengthEnforcement.truncateAfterCompositionEnds 作为附加选项。

以下所有类都具有相同的 API 更改:

  • TextField
  • TextFormField
  • CupertinoTextField

迁移指南

提供深入迁移指南

迁移前的代码:

dart
const TextField textField = TextField(maxLengthEnforced: true);
const TextField textField = TextField(maxLengthEnforced: false);
final lengthEnforced = textField.maxLengthEnforced;

const TextFormField textFormField = TextFormField(maxLengthEnforced: true);
const TextFormField textFormField = TextFormField(maxLengthEnforced: false);
final lengthEnforced = textFormField.maxLengthEnforced;

const CupertinoTextField cupertinoTextField = CupertinoTextField(maxLengthEnforced: true);
const CupertinoTextField cupertinoTextField = CupertinoTextField(maxLengthEnforced: false);
final lengthEnforced = cupertinoTextField.maxLengthEnforced;

迁移后的代码:

dart
const TextField textField = TextField(maxLengthEnforcement: MaxLengthEnforcement.enforce);
const TextField textField = TextField(maxLengthEnforcement: MaxLengthEnforcement.none);
final lengthEnforced = textField.maxLengthEnforcement;

const TextFormField textFormField = TextFormField(maxLengthEnforcement: MaxLengthEnforcement.enforce);
const TextFormField textFormField = TextFormField(maxLengthEnforcement: MaxLengthEnforcement.none);
final lengthEnforced = textFormField.maxLengthEnforcement;

const CupertinoTextField cupertinoTextField = CupertinoTextField(maxLengthEnforcement: MaxLengthEnforcement.enforce);
const CupertinoTextField cupertinoTextField = CupertinoTextField(maxLengthEnforcement: MaxLengthEnforcement.none);
final lengthEnforced = cupertinoTextField.maxLengthEnforcement;

参考

API 文档:

相关问题:

相关 PR:


VelocityTracker 构造函数

#

Flutter Fix 支持:是

VelocityTracker 的默认构造函数在 v1.22 中已弃用。

应改用 VelocityTracker.withKind()。这允许为跟踪器指定 PointerDeviceKindVelocityTracker.kind 的先前默认值为 PointerDeviceKind.touch

迁移指南

迁移前的代码:

dart
final VelocityTracker tracker = VelocityTracker();

迁移后的代码:

dart
final VelocityTracker tracker = VelocityTracker.withKind(PointerDeviceKind.touch);

参考

API 文档:

相关 PR:


DayPickerMonthPicker

#

Flutter Fix 支持:否

DayPickerMonthPicker 小部件在 v1.15 中首次弃用,然后在 v1.26 中扩展。

它们已被一个全面的小部件 CalendarDatePicker 替换。

这些小部件使用 showDatePicker 方法显示。此方法在此版本之前已迁移为显示新的 CalendarDatePicker,因此它们的最终移除不应需要进一步的操作。

参考

设计文档:

API 文档:

相关问题:

相关 PR:


FlatButtonRaisedButtonOutlineButton

#

Flutter Fix 支持:否

FlatButtonRaisedButtonOutlineButton 小部件在 v1.20 中首次弃用,然后在 v1.26 中扩展。

它们被新的按钮 TextButtonElevatedButtonOutlinedButton 替换。这些新的小部件还使用新的关联主题,而不是通用的 ButtonTheme

旧小部件旧主题新小部件新主题
FlatButtonButtonThemeTextButtonTextButtonTheme
RaisedButtonButtonThemeElevatedButtonElevatedButtonTheme
OutlineButtonButtonThemeOutlinedButtonOutlinedButtonTheme

迁移指南

提供详细样式的深入迁移指南

迁移前的代码:

dart
FlatButton(
  onPressed: onPressed,
  child: Text('Button'),
  // ...
);

RaisedButton(
  onPressed: onPressed,
  child: Text('Button'),
  // ...
);

OutlineButton(
  onPressed: onPressed,
  child: Text('Button'),
  // ...
);

迁移后的代码:

dart
TextButton(
  onPressed: onPressed,
  child: Text('Button'),
  // ...
);

ElevatedButton(
  onPressed: onPressed,
  child: Text('Button'),
  // ...
);

OutlinedButton(
  onPressed: onPressed,
  child: Text('Button'),
  // ...
);

参考

设计文档:

API 文档:

相关 PR:


ScaffoldSnackBar 方法

#

Flutter Fix 支持:否

以下 ScaffoldSnackBar 方法在 v1.23 中已弃用。

  • showSnackBar
  • removeCurrentSnackBar
  • hideCurrentSnackBar

应改用 ScaffoldMessenger 的同名方法。默认的 ScaffoldMessenger 已在每个 MaterialApp 中创建。

迁移指南

提供深入迁移指南

迁移前的代码:

dart
Scaffold.of(context).showSnackBar(mySnackBar);
Scaffold.of(context).removeCurrentSnackBar(mySnackBar);
Scaffold.of(context).hideCurrentSnackBar(mySnackBar);

迁移后的代码:

dart
ScaffoldMessenger.of(context).showSnackBar(mySnackBar);
ScaffoldMessenger.of(context).removeCurrentSnackBar(mySnackBar);
ScaffoldMessenger.of(context).hideCurrentSnackBar(mySnackBar);

参考

设计文档:

视频内容:

API 文档:

相关问题:

相关 PR:


RectangularSliderTrackShape.disabledThumbGapWidth

#

Flutter Fix 支持:是

RectangularSliderTrackShape.disabledThumbGapWidth 在 v1.5 中首次弃用,然后在 v1.26 中扩展。

框架不再使用它,因为禁用时滑块拇指的动画不再发生。

迁移指南 迁移前的代码:

dart
RectangularSliderTrackShape(disabledThumbGapWidth: 2.0);

迁移后的代码:

dart
RectangularSliderTrackShape();

参考

API 文档:

相关 PR:


ThemeData 的文本选择到 TextSelectionThemeData

#

Flutter Fix 支持:是

以下 ThemeData 成员在 v1.23 中首次弃用,并在 v1.26 中扩展。

  • useTextSelectionTheme
  • textSelectionColor
  • cursorColor
  • textSelectionHandleColor

这些应替换为更全面的 TextSelectionThemeData,现在在 ThemeData 本身中指定。

useTextSelectionTheme 标志用作临时迁移标志,以区分这两个 API,现在可以将其删除。

迁移指南

提供深入迁移指南

迁移前的代码:

dart
ThemeData(
  useTextSelectionTheme: false,
  textSelectionColor: Colors.blue,
  cursorColor: Colors.green,
  textSelectionHandleColor: Colors.red,
);

迁移后的代码:

dart
ThemeData(
  textSelectionTheme: TextSelectionThemeData(
    selectionColor: Colors.blue,
    cursorColor: Colors.green,
    selectionHandleColor: Colors.red,
  ),
);

参考

设计文档:

API 文档:

相关问题:

相关 PR:


RenderEditable.onSelectionChangedTextSelectionDelegate.textEditingValue

#

Flutter Fix 支持:否

RenderEditable.onSelectionChangedTextSelectionDelegate.textEditingValue 在 v1.26 中已弃用。

不要调用这些方法中的一个或两个,请调用 TextSelectionDelegate.userUpdateTextEditingValue。这修复了一个 TextInputFormatter 将接收错误选择值的问题。

迁移指南

迁移前的代码:

dart
renderEditable.onSelectionChanged(selection, renderObject, cause);
textSelectionDelegate.textEditingValue = value;

迁移后的代码:

dart
textSelectionDelegate.userUpdateTextEditingValue(value, cause);

参考

API 文档:

相关问题:

相关 PR:


Stack.overflow

#

Flutter Fix 支持:是

Stack.overflow 以及 Overflow 枚举在 v1.22 中已弃用。

替换项为 Stack.clipBehavior,此更改是框架中统一剪辑行为和语义的一部分。如果使用了 Overflow.visible,则使用 Clip.none。如果使用了 Overflow.clip,则使用 Clip.hardEdge

迁移指南

提供深入迁移指南

迁移前的代码:

dart
const Stack stack = Stack(overflow: Overflow.visible);
const Stack stack = Stack(overflow: Overflow.clip);

迁移后的代码:

dart
const Stack stack = Stack(clipBehavior: Clip.none);
const Stack stack = Stack(clipBehavior: Clip.hardEdge);

参考

API 文档:

相关问题:

相关 PR:


UpdateLiveRegionEvent

#

Flutter Fix 支持:否

SemanticsEvent UpdateLiveRegionEvent 在 v1.12 中首次弃用,然后在 v1.26 中扩展。

框架从未实现过它,任何引用都应删除。

参考

API 文档:

相关 PR:


RenderObjectElement 方法

#

Flutter Fix 支持:是

以下 RenderObjectElement 方法在 v1.21 中已弃用。

  • insertChildRenderObject
  • moveChildRenderObject
  • removeChildRenderObject

这些方法分别被替换为:

  • insertRenderObjectChild
  • moveRenderObjectChild
  • removeRenderObjectChild

为了更改函数签名,这些更改被作为软性中断弃用进行。

迁移指南

迁移前的代码:

dart
element.insertChildRenderObject(child, slot);
element.moveChildRenderObject(child, slot);
element.removeChildRenderObject(child);

迁移后的代码:

dart
element.insertRenderObjectChild(child, slot);
element.moveRenderObjectChild(child, oldSlot, newSlot);
element.removeRenderObjectChild(child, slot);

参考

API 文档:

相关问题:

相关 PR:


时间线

#

稳定版:3.0.0