概述

#

TextInputClient 接口中添加了一个新的 getter TextInputClient.currentAutofillScope;所有 TextInputClient 的子类都必须提供 currentAutofillScope 的具体实现。

此 getter 允许 TextInputClient 触发涉及多个逻辑上相连的输入字段的自动填充。例如,“用户名”字段可以触发一个自动填充,该自动填充会同时填充它本身以及与其关联的“密码”字段。

背景

#

在许多平台上,自动填充服务能够在一次自动填充尝试中自动填充多个输入字段。例如,用户名和密码字段通常可以一次性自动填充。出于这个原因,即将触发自动填充的 Flutter 输入字段也应向平台提供有关与之逻辑上相连的其他可自动填充输入字段的信息。TextInputClient.currentAutofillScope 定义了与此 TextInputClient 逻辑上相连且可以一起自动填充的输入字段组。

变更说明

#

TextInputClient 现在有一个额外的 getter,用于返回此客户端所属的 AutofillScope。输入客户端使用此 getter 从同一范围内的其他可自动填充输入字段收集与自动填充相关的信息。

dart
abstract class TextInputClient {
  AutofillScope get currentAutofillScope;
}

如果在编译 Flutter 应用时看到错误消息“missing concrete implementation of 'getter TextInputClient.currentAutofillScope'”(缺少 'getter TextInputClient.currentAutofillScope' 的具体实现),请按照下面列出的迁移步骤进行操作。

迁移指南

#

如果您不打算为您的 TextInputClient 子类添加多字段自动填充支持,只需在 getter 中返回 null 即可。

dart
class CustomTextField implements TextInputClient {
  // Not having an AutofillScope does not prevent the input field
  // from being autofilled. However, only this input field is
  // autofilled when autofill is triggered on it.
  AutofillScope get currentAutofillScope => null;
}

如果需要多字段自动填充支持,一个常用的 AutofillScopeAutofillGroup widget。要获取离文本输入最近的 AutofillGroup widget,请使用 AutofillGroup.of(context)

dart
class CustomTextFieldState extends State<CustomTextField> implements TextInputClient {
  AutofillScope get currentAutofillScope => AutofillGroup.of(context);
}

有关更多信息,请参阅 AutofillGroup

时间线

#

发布版本:1.18.0
稳定版本中:1.20

参考资料

#

API 文档

相关议题

相关 PR