我想在应用栏内放置一个文本字段,并使用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,
),
)
],
),
);
答案 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
),
)
],
),
)