【问题标题】:Flutter TexField and TextFormField duplicates text when using initial value or controller to set initial textFlutter TextField 和 TextFormField 使用初始值或控制器设置初始文本时重复文本
【发布时间】:2018-10-19 14:31:44
【问题描述】:

在具有 TextField 或 TextFormField 的 Flutter 应用程序中,文本可以在第一次输入数据时重复。

当第一次输入按键时,在一个已经预先填充了文本的字段中,文本会重复,然后您的按键被附加。

我在我的三星 Galaxy S7、S8 和 S9(仅限我必须测试的设备)上遇到了这种情况。这不会发生在模拟器(Galaxy Nexus9 和 Pixel 2)中。

如果我在字段末尾放置一个空格,则不会发生此问题,但是,如果我在预填充字段的中间点按(使用控制器或 initialValue)并按下某个键,则会发生此问题。

这是一个准系统源代码示例:

class SampleTextFormPage extends StatefulWidget {
    @override
    State<StatefulWidget> createState() => new _SampleTextFormPage();
}

class _SampleTextFormPage extends State<SampleTextFormPage> {
    final _scaffoldKey = new GlobalKey<ScaffoldState>();

    TextEditingController _txtController;

    @override
    void initState() {
        super.initState();

        _txtController = TextEditingController(text:'Using Controller');
    }

    @override
    Widget build(BuildContext context) { Scaffold scaffold = new Scaffold(
        key: _scaffoldKey,
        appBar: new AppBar(
            title: new Text('Text Entry',
                style: const TextStyle(
                    color: Colors.white)
            ),
            backgroundColor: Colors.indigo
        ),
        body:  Column(children: [
            //field 1
            TextField(
                 autocorrect: false,
                 autofocus: true,
                 controller: _txtController,
            ),

           //field 2
           TextFormField(
               autocorrect: false,
               autofocus: true,
               initialValue: 'Using initialValue',
           )
        ])
    );

    return scaffold;
    }
}

注意:我使用的是最新版本的 Flutter,并且已经恢复到多个版本的 Flutter(一直到支持 Dart 2 的第一个版本),但这个问题仍然存在。

【问题讨论】:

  • 你找到解决办法了吗?我也遇到了同样的问题
  • MrQwerty,抱歉回复晚了。使用 Flutter 1.5 帮助改善了一些问题,但还没有完全解决。但是,对于典型的词来说,这似乎很好,但是,在到达顶级域(例如 .com)时,输入网站地址会导致同样的问题。我在 S9 和 S9 Note 上对此进行了测试。

标签: android flutter


【解决方案1】:

正如@DarkNeuron 提到的,这是三星设备上已知的 Flutter 问题。目前还没有完全治愈的方法。

截至 2020 年 2 月,已确定该问题与三星键盘缓存过程和自动更正有关。在 Flutter github 中提出了可能的临时解决方法:对所有文本输入使用 keyboardType: TextInputType.visiblePassword。但有报道称它至少对韩语无效。另一项建议是检查三星设备并相应地构建:

keyboardType: samsungKeyboard ? TextInputType.visiblePassword : TextInputType.emailAddress,
autoFocus: false,

两周前修复的部分问题(标点符号重复)。但问题的主要部分仍然存在。

您可以在此处找到更多详细信息:Flutter github

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,并用这个简单的临时解决方案修复了它:只需在 initialValue 的末尾添加一个空格。示例:

    initialValue: controller.text + ' ',
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-05
      • 2011-08-29
      • 2018-11-24
      • 1970-01-01
      • 2020-06-14
      • 2021-06-19
      • 2017-11-21
      相关资源
      最近更新 更多