Flutter SetState无法更新文本

时间:2019-08-08 17:56:58

标签: android flutter dart widget setstate

我在一个页面上有一个文本,该文本应显示我在TextEditingController中引入的文本。我只粘贴重要的代码,如果需要更多请告诉我:

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
            key: _scaffoldKey,
            appBar: _buildBar(context),
            body: Container(
                child: SingleChildScrollView(
                    child: Column(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.only(top: 10),
                ),
                buildTitle(),
              ],
            )))),
        onWillPop: () => _onWillPop(context));
...
  }

buildTitle()方法是:

Widget buildTitle() {
return Column(children: <Widget>[
  Row(
    mainAxisAlignment: MainAxisAlignment.center,
    children: <Widget>[
      Text(
        name == null ? "" : name,
        style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
      ),
      IconButton(
        icon: Icon(Icons.edit),
        onPressed: () {
          changeName();
        },
      ),
    ],
  ),
  ...
]);

}

changeName()是:

void changeName() {
    showDialog(
        context: context,
        builder: (context) {
          return StatefulBuilder(builder: (context, setState) {
            return AlertDialog(
              title: Text('Name'),
              content: TextField(
                autofocus: true,
                controller: _textController,
                decoration: InputDecoration(
                  hintText: "Name of product",
                  errorText: incorrectName
                      ? 'The name could not be empty'
                      : null,
                ),
              ),
              actions: <Widget>[
                FlatButton(
                  child: new Text('OK'),
                  onPressed: () {
                    if (_textController.text.length == 0) {
                      setState(() {
                        _textController.text.length == 0
                            ? incorrectName = true
                            : incorrectName = false;
                      });
                    } else {
                      setState(() {
                        incorrectName = false;
                        name = _textController.text;
                      });
                      Navigator.of(context).pop();
                    }
                  },
                ),
                FlatButton(
                  child: new Text('Cancel'),
                  onPressed: () {
                      Navigator.of(context).pop();
                  },
                )
              ],
            );
          });
        });
  }

起初,名称为空,因此仅显示编辑按钮,但是当我单击“确定”时,Text不变,但是我有另一个带有SetState的方法,当我单击该名称时,就会出现。

在这种情况下为什么不使用SetState更新名称?

1 个答案:

答案 0 :(得分:1)

更改changeName()返回类型:

Future<String> changeName() {
    showDialog<String>(
        context: context,
        builder: (context) {
            return AlertDialog(
              title: Text('Name'),
              content: TextField(
                autofocus: true,
                controller: _textController,
                decoration: InputDecoration(
                  hintText: "Name of product",
                  errorText: incorrectName
                      ? 'The name could not be empty'
                      : null,
                ),
              ),
              actions: <Widget>[
                FlatButton(
                  child: new Text('OK'),
                  onPressed: () {
                    if (_textController.text.length == 0) {
                      Navigator.of(context).pop(null);
                    } else {
                      Navigator.of(context).pop(_textController.text);
                    }
                  },
                ),
                FlatButton(
                  child: new Text('Cancel'),
                  onPressed: () {
                      Navigator.of(context).pop(null);
                  },
                )
              ],
            );
        });
  }

在您的buildTitle()中:

Widget buildTitle() {
return Column(children: <Widget>[
  Row(
    mainAxisAlignment: MainAxisAlignment.center,
    children: <Widget>[
      Text(
        name == null ? "" : name,
        style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
      ),
      IconButton(
        icon: Icon(Icons.edit),
        onPressed: () async {
          name = await changeName();
          setState((){});
        },
      ),
    ],
  ),
  ...
]);