概述

#

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

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

变更

#

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

按钮 styleFrom 属性

#

软件包:flutter Flutter Fix 支持:是

TextButtonElevatedButtonOutlinedButton 小部件都包含一个用于生成 ButtonStyle 的静态 styleFrom 方法。此方法中每个类的以下颜色属性在 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