Flutter - 跨度不能为零长度

6

我正在开发一个使用TextField的Flutter应用程序。

我是这样声明TextField的:

new TextField(
  controller : _controller,
  decoration : new InputDecoration(
      hintText: 'Message...'
  )
)

TextField 显示在我的小部件中,但是当我点击它时,键盘会自动关闭,并且在控制台中出现以下错误。

E/SpannableStringBuilder(17860): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length

我正在使用IntelliJ中的Flutter插件在Android上运行此应用程序(Samsung Galaxy Grand Prime, Android 5.1)。

我该如何解决这个问题?

编辑:根据这个答案 (Android - SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length),我已经尝试更换键盘(谷歌和三星),但仍然有相同的问题。


你是如何初始化_controller的? - Collin Jackson
最终的 TextEditingController _controller = new TextEditingController(); - Alexi Coard
请参考此处:https://flutter.cn/text-input/ - Alexi Coard
3个回答

3

对我来说解决方法是添加autocorrect: false,并将keyboardType: TextInputType.visiblePassword设置。该错误发生在我的s8plus上,但在我的iPhone模拟器上没有发生。

return TextFormField(
        controller: emailController,
        autofocus: true,
        autocorrect: false,
        keyboardType: TextInputType.visiblePassword,
        validator: validateEmailOrPhoneNumber,
        onChanged: (text) {
          this._validateEmailORPhoneNumber(text);
          this._onTouched('emailOrPhoneNumber');
        },
        onTap: () => _onTouched('emailOrPhoneNumber'),
        decoration: InputDecoration(
            prefixIcon: isPhoneNumber
                ? Container(
                    width: 70,
                    child: leftSection,
            )
                : null,
            labelText: 'Email or Phone number',
            hintText: 'Enter Email or Phone number'),
      );

2

我认为完整的错误可能出现在其他地方。如果问题仅限于一个安卓设备,我建议您打开一个新的Flutter问题。


会尝试使用另一个Android设备,我会保持联系。 - Alexi Coard
1
刚刚尝试了一下Galaxy Tab A,没有出现这个bug。我会提交一个问题报告。谢谢Collin。 - Alexi Coard

2

我曾经遇到过同样的错误。在我的情况下,问题不在TextFormField上,而是由于Form小部件内部的键(key)引起的。如果你使用了键(key),那么总体小部件必须是一个StatefulWidget,而不是StatelessWidget

错误代码

class MyWidget extends StatelessWidget{
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>(); 
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Form(
        key: _formKey,
        child: TextFormField(),
      ),
    );
  }
}

正确的代码

class MyWidget extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MyWidgetState();
  }
}

class _MyWidgetState extends State<MyWidget>{
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>(); 
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Form(
        key: _formKey,
        child: TextFormField(),
      ),
    );
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接