android - Flutter SetState 不更新文本

标签 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();
                  },
                )
              ],
            );
          });
        });
  }

起初,name 是空的,所以只出现编辑按钮,但是当我点击 Ok 时,Text 没有改变,但我有另一个 SetState 方法,当我点击时,名字就会出现。

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

最佳答案

更改 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((){});
        },
      ),
    ],
  ),
  ...
]);

关于android - Flutter SetState 不更新文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57418296/

相关文章:

javascript - 如何在 Phaser JS 中实现一个好的定时器

Flutter检查器在Visual Studio Code中错误/卡住

dart - 如何在Dart中从父类到子类进行回调

dart - 我如何阻止我的应用回到启动画面

android - 从 Receiver 调用时服务不处理 Intent

java - Pie 上的 Camera2 API 越界异常

flutter - 如何在 flutter 中的for循环中动态创建scrollController

flutter - 如何使类似堆栈的定位小部件可滚动?

android-studio - 如何在不使用Android Studio的情况下更改Flutter中的应用程序包名称?

java - 删除文件后更新 View 适配器