概述

#

根据 Flutter 的 弃用策略,已于 3.16 稳定版发布后生命周期结束的已弃用 API 已被移除。

所有受影响的 API 都已汇总到此主要来源,以帮助迁移。为进一步协助您的迁移,请查看此 快速参考表

变更

#

本节按软件包和受影响的类列出了废弃项。

Button styleFrom 属性

#

软件包:flutter Flutter Fix 支持:是

TextButtonElevatedButtonOutlinedButton 小部件均有一个静态 styleFrom 方法,用于生成 ButtonStyle。该方法针对每个类别的以下颜色属性已在 v3.1 中弃用。

  • TextButton.styleFrom
    • primary
    • onSurface
  • ElevatedButton.styleFrom
    • primary
    • onPrimary
    • onSurface
  • OutlinedButton.styleFrom
    • primary
    • onSurface

这些更改更好地使 API 与更新的 Material Design 规范保持一致。通过将这些属性替换为 backgroundColorforegroundColordisabledForegroundColor,这些更改也提高了颜色应用于按钮的清晰度。

迁移指南

迁移前的代码

dart
TextButton.styleFrom(
  primary: Colors.red,
  onSurface: Colors.black,
);
ElevatedButton.styleFrom(
  primary: Colors.red,
  onPrimary: Colors.blue,
  onSurface: Colors.black,
);
OutlinedButton.styleFrom(
  primary: Colors.red,
  onSurface: Colors.black,
);

迁移后的代码

dart
TextButton.styleFrom(
  foregroundColor: Colors.red,
  disabledForegroundColor: Colors.black,
);
ElevatedButton.styleFrom(
  backgroundColor: Colors.red,
  foregroundColor: Colors.blue,
  disabledForegroundColor: Colors.black,
);
OutlinedButton.styleFrom(
  foregroundColor: Colors.red,
  disabledForegroundColor: Colors.black,
);

参考资料

API 文档

相关 PR


ThemeData.selectedRowColor

#

软件包:flutter Flutter Fix 支持:是

ThemeDataselectedRowColor 属性已在 v3.1 中弃用。

该属性不再被框架使用,因为使用它的部件已迁移到其他组件主题,或在 Material Design 的更新规范中不再需要它。

迁移指南

迁移前的代码

dart
ThemeData(
  // ...
  selectedRowColor: Colors.pink, // Would have no effect.  
);

迁移后的代码

dart
ThemeData(
  // ...
  // Remove uses.  
);

参考资料

API 文档

相关 PR


#

软件包:flutter Flutter Fix 支持:是

NavigatorStatefocusScopeNode 属性已在 v3.1 中弃用。

此更改是为了解决围绕 Navigator 引入的 FocusScopeNode 的几个问题。相反,FocusScope 已移至 WidgetsApp 中的最顶层 NavigatorNavigatorState 已更改为包含其自身的 FocusNode,从中可以引用其 FocusNode.enclosingScope 来访问正确的 FocusScopeNode

迁移指南

迁移前的代码

dart
Navigator.of(context).focusScopeNode;

迁移后的代码

dart
Navigator.of(context).focusNode.enclosingScope!;

参考资料

API 文档

相关 PR


PlatformMenuBar.body

#

软件包:flutter Flutter Fix 支持:是

PlatformMenuBarbody 属性已在 v3.1 中弃用。

此更改是为了使 PlatformMenuBar 与框架中的其他小部件保持一致,并将其重命名为 child

迁移指南

迁移前的代码

dart
PlatformMenuBar(
  body: myWidget,
);

迁移后的代码

dart
PlatformMenuBar(
  child: myWidget,
);

参考资料

API 文档

相关 PR


先前宣布的 TextThemeWidgetInspectorServiceWidgetInspectorServiceExtensions 的弃用并未在此周期中移除。WidgetInspectorServiceWidgetInspectorServiceExtensions 中关于 setPubRootDirectories 的弃用已延长一年,以允许 IDE 和其他客户迁移。预计 TextTheme 的弃用将在下一个周期中移除,届时将再次宣布。


时间线

#

稳定版本:3.19.0