簡體   English   中英

Flutter SetState無法更新文本

[英]Flutter SetState not update Text

我有一個帶有Text的頁面,該頁面應顯示我在TextEditingController引入的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更新名稱?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM