Flutter自动填充不适用于文本字段 [英] Flutter auto fill not working on text field

查看:82
本文介绍了Flutter自动填充不适用于文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于任何类型的提示,我都无法使Flutter自动填充工作.我复制了Flutter的示例代码,做了一些修改.

I cannot make auto fill in Flutter working at all, for any type of hint. I copied the Flutter's example code with a little modification.

  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的自动填充服务),并且自动填充本身可与其他应用一起使用.

But no autofill dropdown was shown for any type of hint. I've turned on Autofill service on the device (using Google's autofill service) and the autofill itself works with other apps.

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

What's wrong with the code? Is there anything missing that causes the autofill disabled?

注意:我使用Flutter 1.20.4和Android 10

Note: I use Flutter 1.20.4 and Android 10

推荐答案

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

What was missing for me is this migration https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects. Notably, your MainActivity needs to extend io.flutter.embedding.android.FlutterActivity and not io.flutter.app.FlutterActivity.

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

Also, as you already mentioned, you need to configure an AutoFill service in the system settings.

有效的示例代码:

  @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,
        ),
      ],
    )));

用户名而不是电子邮件应该类似地工作.

Username instead of email should work similarly.

这篇关于Flutter自动填充不适用于文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆