TextFormField 失去焦点 - 颤动

Ste*_*pan 6 dart android-textinputlayout flutter

我正在尝试解决我的应用程序中的表单验证问题。每次单击 TextFromField 时,焦点都会丢失并且键盘隐藏。我发现问题出在“_formKey”上。但我需要它来验证。如何解决?

代码片段:

  class _TodoCreateDetailPageState extends State<TodoPage> {
  @override
  Widget build(BuildContext context) {
    final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();

    String _title = widget.todo == null ? "New TODO" : widget.todo.title;
    String _message;

    return new Scaffold(
      appBar: new AppBar(
        title: new Text(_title),
      ),
      floatingActionButton: new FloatingActionButton(
          child: new Icon(Icons.save), onPressed: null),
      body: new Padding(
          padding: new EdgeInsets.all(20.0),
          child: new Form(
              key: _formKey,
              child: new Column(
                children: <Widget>[
                  new TextField(
                    decoration: new InputDecoration(labelText: 'Title'),
                    onChanged: (String value) {
                      _title = value;
                    },
                  ),
                  new TextField(
                    decoration: new InputDecoration(labelText: 'Message'),
                    onChanged: (String value) {
                      _message = value;
                    },
                  )
                ],
              ))),
    );
  }
Run Code Online (Sandbox Code Playgroud)

Muh*_*bal 10

此问题是由于 build() 中的 GlobalKey 初始化造成的

@override
Widget build(BuildContext context){
    //here is the reason of losing focus.
   final GlobalKey<FormState> _formKey = new GlobalKey<FormState>()
}
Run Code Online (Sandbox Code Playgroud)

从构建中删除它,一切都好。

    final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
    //this will work fine when you move key outside the build();
    @override
    Widget build(BuildContext context){

    }
Run Code Online (Sandbox Code Playgroud)


小智 5

正如德里克·莱金 (Derek Lakin) 在评论中所说,更改StatelessWidgetStatefulWidget对我有用。


Smi*_*ily 5

您似乎知道问题出在密钥本身,如#6783。解决方案是避免每次都构建验证密钥。因此,您可以这样做(或者甚至将其设为小部件的属性):

class _TodoCreateDetailPageState extends State<TodoPage> {
  final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    String _title = widget.todo == null ? "New TODO" : widget.todo.title;
    String _message;

    return new Scaffold(
      appBar: new AppBar(
        title: new Text(_title),
      ),
      floatingActionButton: new FloatingActionButton(
          child: new Icon(Icons.save), onPressed: null),
      body: new Padding(
          padding: new EdgeInsets.all(20.0),
          child: new Form(
              key: _formKey,
              child: new Column(
                children: <Widget>[
                  new TextField(
                    decoration: new InputDecoration(labelText: 'Title'),
                    onChanged: (String value) {
                      _title = value;
                    },
                  ),
                  new TextField(
                    decoration: new InputDecoration(labelText: 'Message'),
                    onChanged: (String value) {
                      _message = value;
                    },
                  )
                ],
              ))),
    );
  }
Run Code Online (Sandbox Code Playgroud)