【问题标题】:Flutter auto fill not working on text field颤振自动填充不适用于文本字段
【发布时间】:2021-01-06 17:14:54
【问题描述】:

对于任何类型的提示,我都无法让 Flutter 中的自动填充正常工作。我复制了 Flutter 的示例代码,稍作修改。

  bool isSameAddress = true;
  final TextEditingController email = TextEditingController();
  final TextEditingController billingAddress1 = TextEditingController();
  final TextEditingController billingAddress2 = TextEditingController();

  final TextEditingController creditCardNumber = TextEditingController();
  final TextEditingController creditCardSecurityCode = TextEditingController();

  final TextEditingController phoneNumber = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: <Widget>[
        const Text('Email'),
        AutofillGroup(
          child: Column(
            children: <Widget>[
              TextField(
                controller: email,
                autofillHints: <String>[AutofillHints.email],
              ),
            ],
          ),
        ),
        const Text('Billing address'),
        Checkbox(
          value: isSameAddress,
          onChanged: (bool newValue) {
            setState(() { isSameAddress = newValue; });
          },
        ),
        // Again the address fields are grouped together for the same reason.
        if (!isSameAddress) AutofillGroup(
          child: Column(
            children: <Widget>[
              TextField(
                controller: billingAddress1,
                autofillHints: <String>[AutofillHints.streetAddressLine1],
              ),
              TextField(
                controller: billingAddress2,
                autofillHints: <String>[AutofillHints.streetAddressLine2],
              ),
            ],
          ),
        ),
        const Text('Credit Card Information'),
        // The credit card number and the security code are grouped together as
        // some platforms are capable of autofilling both fields.
        AutofillGroup(
          child: Column(
            children: <Widget>[
              TextField(
                controller: creditCardNumber,
                autofillHints: <String>[AutofillHints.creditCardNumber],
              ),
              TextField(
                controller: creditCardSecurityCode,
                autofillHints: <String>[AutofillHints.creditCardSecurityCode],
              ),
            ],
          ),
        ),
        const Text('Contact Phone Number'),
        // The phone number field can still be autofilled despite lacking an
        // `AutofillScope`.
        TextField(
          controller: phoneNumber,
          autofillHints: <String>[AutofillHints.telephoneNumber],
        ),
      ],
    );
  }

但是对于任何类型的提示都没有显示自动填充下拉菜单。我已经在设备上开启了自动填充服务(使用 Google 的自动填充服务)并且自动填充本身可以与其他应用一起使用。

代码有什么问题?是否缺少任何导致自动填充功能被禁用的内容?

注意:我使用 Flutter 1.20.4 和 Android 10

【问题讨论】:

  • 我已经尝试过自动填充并解决了与您相同的问题。同样使用 Flutter 1.20.4
  • 我选择了 flutter_typeahead: ^3.0.0-nullsafety.0 包。

标签: flutter


【解决方案1】:

我缺少的是这次迁移https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects。值得注意的是,您的 MainActivity 需要扩展 io.flutter.embedding.android.FlutterActivity 而不是 io.flutter.app.FlutterActivity。

另外,正如您已经提到的,您需要在系统设置中配置自动填充服务。

工作示例代码:

  @override
  Widget build(BuildContext context) => Scaffold(
    appBar: AppBar(
      title: Text("Login"),
    ),
    body: AutofillGroup(
        child: Column(
      children: [
        TextField(
          autofillHints: [AutofillHints.email],
          keyboardType: TextInputType.emailAddress,
        ),
        TextField(
          autofillHints: [AutofillHints.password],
          keyboardType: TextInputType.text,
        ),
      ],
    )));

用户名而不是电子邮件应该类似。

【讨论】:

  • 这对我有帮助。谢谢。
  • 对我来说,它只适用于电子邮件和姓名,不适用于电话号码。可能是什么原因?
猜你喜欢
  • 2020-11-24
  • 1970-01-01
  • 2015-02-07
  • 2020-12-04
  • 2014-08-20
  • 1970-01-01
  • 2018-07-16
  • 2019-06-28
  • 1970-01-01
相关资源
最近更新 更多