onChange在Flutter应用程序栏中不起作用

时间:2019-08-22 13:00:14

标签: flutter dart

我想在应用栏内放置一个文本字段,并使用onChanged属性从中获取数据。我做到了,但是onChanged不起作用。

new Container(
      child: new Stack(
        children: <Widget>[
          new Container(
            child: BuildSvg('assets/svg/backgroundSmall.svg'),
            color: Colors.lightGreen,
          ),
          new Scaffold(
            appBar: new AppBar(
              title: TextField(
                onChanged: (text) {
                  print('text');
                  setState(() {});
                },
              ),
              backgroundColor: Colors.transparent,
              elevation: 0.0,
            ),

          )
        ],
      ),
    );

1 个答案:

答案 0 :(得分:0)

如果我对您的理解正确,那么您需要的是:

创建变量

String _text = "";

并更新您的代码。

new Container(
  child: new Stack(
    children: <Widget>[
      new Container(
        child: BuildSvg('assets/svg/backgroundSmall.svg'),
        color: Colors.lightGreen,
      ),
      new Scaffold(
        appBar: new AppBar(
          title: TextField(
            onChanged: (text) {
              setState(() => _text = text); // you need this
            },
          ),
          backgroundColor: Colors.transparent,
          elevation: 0.0,
        ),
        backgroundColor: Colors.transparent,
        body: new Container(
          color: Colors.grey,
          child: new Center(child: new Text(_text)), // and this
        ),
      )
    ],
  ),
)