【发布时间】:2018-04-01 10:39:55
【问题描述】:
我在TextField 的onSubmitted 中收到错误The method 'setState' isn't defined for the class MyApp error in Flutter
代码:
Widget build(BuildContext context) {
String phoneNo;
return new MaterialApp(
title: 'SchoolTrack',
theme: new ThemeData(
primaryColor: Colors.grey[50],
),
home: new Scaffold(
appBar: null,
backgroundColor: Colors.cyan[100],
body: new Container(
padding: const EdgeInsets.all(32.0),
child: new Center(
child: new TextField(
autofocus: true,
autocorrect: false,
decoration: new InputDecoration(
hintText: 'Type the phone no',
suffixIcon: new Icon(Icons.send),
suffixStyle: new TextStyle(
color: Colors.cyan[300],
)
),
onSubmitted: (String input) {
setState(() {
phoneNo = input;
});
},
),
),
),
)
);
}
【问题讨论】:
-
你需要创建自己的StatefulWidget,在那里你可以调用
setState -
我们可以在 Util 类中使用 setState 方法吗? Util 类不是有状态的小部件。
标签: flutter