Flutter TexField 和 TextFormField 在使用初始值或控制器设置初始文本时复制文本

Sal*_*ish 11 android flutter

在具有 TextField 或 TextFormField 的 flutter 应用程序中,第一次输入数据时,文本可能会被复制。

首次输入按键时,在已预填充文本的字段中,文本会复制,然后附加您的按键。

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

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

这是一个准系统源示例:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

小智 5

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

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

keyboardType: samsungKeyboard ? TextInputType.visiblePassword : TextInputType.emailAddress,
autoFocus: false,
Run Code Online (Sandbox Code Playgroud)

两周前修复的问题部分(标点符号重复)。但问题的主要部分仍然被观察到。

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