对于任何类型的提示,我根本无法使 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
最佳答案
我缺少的是这次迁移 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,
),
],
)));
用户名而不是电子邮件应该类似地工作。
关于Flutter 自动填充不适用于文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63978213/