_listenerAttached':不正确”,在StatefulWidget中更改布局后

时间:2019-02-12 14:58:43

标签: dart flutter flutter-animation

我正在尝试使用Flutter制作表单,让用户在提交后可以更改答案。表单在TextField中包含两个AnimatedSwitcher。当用户单击“发送”按钮时,切换器被告知将TextField替换为Text消息,表示'Successfully submitted! (you can still change your answer)'我再次单击该按钮,我可以正常看到表格,但是当我专注于表单时,我会短暂看到以下错误。

The following assertion was thrown building _ScrollableScope:
'package:flutter/src/rendering/editable.dart': Failed assertion: line 599 pos 14: 
'_listenerAttached': is not true.

下面的代码完全重新创建了错误。

  1. 在两个文本字段中都写一些东西
  2. 点击提交按钮
  3. 点击取消按钮
  4. 专注于任何文本字段,然后专注于另一个文本字段。该错误将显示。

代码:

import 'dart:core';
import 'package:flutter/material.dart';

class EditableFocus extends StatefulWidget {
  @override
  _State createState() {
    return _State();
  }
}

class _State extends State<EditableFocus> with TickerProviderStateMixin {
  FocusNode focusNode1 = FocusNode();
  FocusNode focusNode2 = FocusNode();
  bool success = false;
  TextEditingController controller1 = TextEditingController();
  TextEditingController controller2 = TextEditingController();
  final GlobalKey<FormState> _formKeyOne = GlobalKey<FormState>();
  final GlobalKey<FormState> _formKeyTwo = GlobalKey<FormState>();

  @override
  void initState() {
    super.initState();
    focusNode1.addListener(_focusListner);
    focusNode2.addListener(_focusListner);
    controller1.addListener(_changeListner);
    controller2.addListener(_changeListner);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0.5,
        title: Text(
          "Testing views",
          style: Theme.of(context).textTheme.title,
        ),
      ),
      body: _builtLayoutBuilder(),
    );
  }

  Widget _builtLayoutBuilder() {
    return Column(
      children: <Widget>[
        AnimatedSwitcher(
          duration: Duration(milliseconds: 200),
          child: _buildChild(),
        ),
        RaisedButton(
            child: Text('Submit/unsubmitted'),
            onPressed: () {
              setState(() {
                success = !success;
              });
            })
      ],
    );
  }

  void _focusListner() {
    if (focusNode1.hasFocus) {
      print('Focus on 1');
    } else if (focusNode2.hasFocus) {
      print('Focus on 2');
    }
  }

  void _changeListner() {
    if (focusNode1.hasFocus) {
      print(controller1.text);
    } else if (focusNode2.hasFocus) {
      print(controller2.text);
    }
  }

  Widget _buildChild() {
    if (success) {
      return Text('Successfully submitted! (you can still change your answer)');
    }
    return Column(
      children: <Widget>[
        TextFormField(
          key: _formKeyOne,
          focusNode: focusNode1,
          controller: controller1,
          style: TextStyle(fontSize: 16.0, color: Colors.black),
          decoration: InputDecoration(
              hintText: 'Write some text', border: InputBorder.none),
//                          onChanged: onSearchTextChanged,
        ),
        TextFormField(
          key: _formKeyTwo,
          focusNode: focusNode2,
          controller: controller2,
          style: TextStyle(fontSize: 16.0, color: Colors.black), 
          decoration: InputDecoration(
              hintText: 'Write some text', border: InputBorder.none),
//                          onChanged: onSearchTextChanged,
        ),
      ],
    );
  }
}

0 个答案:

没有答案