dart Flutter AutoFillGroup/Autofill提示不工作

h5qlskok  于 2024-01-04  发布在  Flutter
关注(0)|答案(2)|浏览(336)

我的代码显示并提交登录表单:

  1. class LoginWidget extends StatelessWidget {
  2. const LoginWidget({
  3. super.key,
  4. });
  5. @override
  6. Widget build(BuildContext context) {
  7. final citiesLoaded = context.select((LoginBloc bloc) => bloc.state.citiesLoaded);
  8. final loginBloc = context.read<LoginBloc>();
  9. final selectedCity = (citiesLoaded && loginBloc.state.cities.length == 1)
  10. ? loginBloc.state.cities[0]
  11. : context.select((LoginBloc bloc) => bloc.state.selectedCity);
  12. final formValidator = context.select((LoginBloc bloc) => bloc.state.formValidator);
  13. final theme = Theme.of(context).extension<LoginPageThemeData>();
  14. return AutofillGroup(
  15. child: Column(
  16. children: [
  17. citiesLoaded
  18. ? Visibility(
  19. visible: loginBloc.state.cities.length > 1,
  20. child: CitySelectWidget(selectedCity: selectedCity),
  21. )
  22. : const BusyWidget(status: 'Loading cities'),
  23. Text('Username:'),
  24. TextFormField(
  25. autofillHints: const [AutofillHints.username, AutofillHints.email, AutofillHints.name],
  26. enabled: selectedCity != null,
  27. initialValue: loginBloc.state.username,
  28. keyboardType: TextInputType.emailAddress,
  29. decoration: theme?.inputDecoration
  30. ?.copyWith(errorText: formValidator.usernameField.isValid ? null : formValidator.usernameField.validationMessage),
  31. onChanged: (value) => loginBloc.add(SetUsernameEvent(username: value))),
  32. Text('Password:'),
  33. TextFormField(
  34. autofillHints: const [AutofillHints.username, AutofillHints.email, AutofillHints.name],
  35. enabled: selectedCity != null,
  36. initialValue: loginBloc.state.password,
  37. obscureText: true,
  38. decoration: theme?.inputDecoration
  39. ?.copyWith(errorText: formValidator.passwordField.isValid ? null : formValidator.passwordField.validationMessage),
  40. onChanged: (value) => loginBloc.add(SetPasswordEvent(password: value))),
  41. ElevatedButton(
  42. onPressed: selectedCity == null
  43. ? null
  44. : () {
  45. context.read<app_bloc.AppBloc>().add(app_bloc.SetSelectedCityEvent(selectedCity));
  46. context.read<LoginBloc>().add(const AuthEvent());
  47. },
  48. child: Text('Sign in'),
  49. ),
  50. ],
  51. ),
  52. );
  53. }
  54. }

字符串
在父部件(登录页面)显示主应用程序的页面时,自动化进程完成:

  1. @override
  2. Widget build(BuildContext context) {
  3. return BlocListener<LoginBloc, LoginState>(
  4. listenWhen: (previous, current) => previous.isLogged != current.isLogged || (current.loginFailed && !current.isBusy),
  5. listener: (context, state) {
  6. if (state.isLogged) {
  7. Navigator.pushNamedAndRemoveUntil(context, '/home', (route) => false);
  8. }
  9. if (state.loginFailed) {
  10. Fluttertoast.showToast(msg: state.statusMessage);
  11. }
  12. },
  13. child: Scaffold( ......


所以,在提交isLogged标志将设置在认证成功后,应用程序导航到主主页+销毁登录页面。但自动填充仍然不工作。没有提示保存密码。
你知道我还需要做什么吗?

f8rj6qna

f8rj6qna1#

自动填充功能似乎存在一些问题,有时是由于设备类型,操作系统和系统设置。
自动填充服务提供程序严重依赖自动填充提示。请确保当前使用的自动填充服务支持自动填充提示中的条目(通常可以在移动终端的系统设置中找到服务名称)。
在开发过程中,我们不会自动填充,以便为许多用户提供最适合的解决方案。
但一般来说,为了获得最好的经验:
1.必须将keyboardType定义为准确的预期文本输入。
1.最好只指定一个AutofillHint类型 (所有类型都可以在here中找到),该类型最容易与您的keyboardType或TextInputType对齐,但如果您必须列出多组AutofillHint类型,则应该从与指定的keyboardType最密切相关的类型开始;特别是为了满足iOS设置为seen here的要求
某些自动填充提示仅适用于特定的keyboardType。例如,AutofillHints.name需要TextInputType.name,而AutofillHints.email仅适用于TextInputType. emailAddresss。请确保输入字段具有兼容的keyboardType。根据经验,TextInputType.name适用于iOS上预定义的许多自动填充提示。
所以你可以有

  1. /
  2. Text('Username:'),
  3. TextFormField(
  4. autofillHints: const [AutofillHints.email, AutofillHints.name],
  5. enabled: selectedCity != null,
  6. initialValue: loginBloc.state.username,
  7. keyboardType: TextInputType.emailAddress,
  8. decoration: theme?.inputDecoration
  9. ?.copyWith(errorText: formValidator.usernameField.isValid ? null : formValidator.usernameField.validationMessage),
  10. onChanged: (value) => loginBloc.add(SetUsernameEvent(username: value))),
  11. Text('Password:'),
  12. TextFormField(
  13. keyboardType: TextInputType.visiblePassword,
  14. autofillHints: const [AutofillHints.password],
  15. enabled: selectedCity != null,
  16. initialValue: loginBloc.state.password,
  17. obscureText: true,
  18. decoration: theme?.inputDecoration
  19. ?.copyWith(errorText: formValidator.passwordField.isValid ? null : formValidator.passwordField.validationMessage),
  20. onChanged: (value) => loginBloc.add(SetPasswordEvent(password: value))),
  21. /

字符串

展开查看全部
wf82jlnq

wf82jlnq2#

我测试了官方flutter bloc示例-登录流。除了他们使用formz库来简化表单验证,它几乎和我的一样。所以我在那里添加了AutofillGroup,它工作了。我终于找到了它在我的情况下不工作的原因。这是因为我使用了:

  1. final formValidator = context.select((LoginBloc bloc) => bloc.state.formValidator);

字符串
formValidator在提交时改变(新状态是发出新的表单验证器值),所以它导致整个表单(包括AutofillGroup)重建。在这种情况下,自动填充似乎会丢失其上下文,并且不再有任何数据要保存。
解决方案是删除顶级选择并使用此选项,以确保仅在需要时刷新表单项:

  1. BlocBuilder<LoginBloc, LoginState>(
  2. buildWhen: (previous, current) => previous.password != current.password || previous.formValidator != current.formValidator,
  3. builder: (context, state) {
  4. return TextFormField(
  5. autofillHints: const [AutofillHints.password],
  6. enabled: selectedCity != null,
  7. initialValue: loginBloc.state.password,
  8. obscureText: true,
  9. decoration: theme.inputDecoration
  10. ?.copyWith(errorText: state.formValidator.passwordField.isValid ? null : state.formValidator.passwordField.validationMessage, label: Text('Hasło')),
  11. onChanged: (value) => loginBloc.add(SetPasswordEvent(password: value)));
  12. }


除此之外,Flutter的自动填充方法仍然不是很好。如果自动填充服务是Google,它工作得很好,但它几乎从来没有像第三方自动填充服务那样工作。在iOS上,一般来说它不工作。密码可以保存到密钥库,但自动填充对话框不会出现,我需要手动查找凭据。

展开查看全部

相关问题