【问题标题】:Flutter: obscureText, how to stop showing typed charactersFlutter:obscureText,如何停止显示键入的字符
【发布时间】:2020-06-23 02:34:43
【问题描述】:

在使用 obscureText: true 的字段中键入文本时,每个键入的字符都会在转换为项目符号之前短暂显示。如何制止这种行为?

【问题讨论】:

  • 您想像许多其他应用一样使用eye 图标(或类似图标)或其他方式来打开/关闭晦涩的文本吗?
  • 别的。虽然切换会很有帮助,因为我想让包括打字员在内的任何人都无法看到正在输入的内容。因此,通过切换检查 WAS 输入的内容会很有帮助。 obscureText 的默认行为是短暂显示键入的字符,然后将其转换为项目符号。这会每次显示一个字符的密码,这可能会导致在屏幕共享/录制会话期间肩冲浪或密码泄露。默认行为感觉像是一种移动功能,但 Flutter 也可以在桌面上运行。
  • 另见github.com/flutter/flutter/issues/65352。从 github.com/flutter/flutter/pull/98150 开始,Flutter 现在应该遵循 Android 上的系统设置。

标签: flutter


【解决方案1】:

这是在网络/桌面上的fixed,但在移动设备上没有选择。您可以按照comment 中的建议创建自定义 TextEditingController:

class ObscuringTextEditingController extends TextEditingController {
  @override
  TextSpan buildTextSpan({TextStyle style, bool withComposing}) {
    var displayValue = '•' * value.text.length;
    if (!value.composing.isValid || !withComposing) {
      return TextSpan(style: style, text: displayValue);
    }
    final TextStyle composingStyle = style.merge(
      const TextStyle(decoration: TextDecoration.underline),
    );
    return TextSpan(
      style: style,
      children: <TextSpan>[
        TextSpan(text: value.composing.textBefore(displayValue)),
        TextSpan(
          style: composingStyle,
          text: value.composing.textInside(displayValue),
        ),
        TextSpan(text: value.composing.textAfter(displayValue)),
      ],
    );
  }
}

删除obscureText并使用控制器:

var passwordController = ObscuringTextEditingController();

TextField(
  controller: passwordController,
  decoration: const InputDecoration(hintText: 'Password'),
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 2012-11-11
    • 1970-01-01
    相关资源
    最近更新 更多