Android 颤振-在一次调用setState()后更新同级小部件
我有一个对话框小部件,有一个按钮和文本字段。按下按钮时,会出现一个函数,其中包含Android 颤振-在一次调用setState()后更新同级小部件,android,flutter,dart,Android,Flutter,Dart,我有一个对话框小部件,有一个按钮和文本字段。按下按钮时,会出现一个函数,其中包含setState()和变量更改。我没有在文本字段的屏幕上立即看到这些更改,我需要再次关闭并打开对话框。为什么以及如何实现这一点(这样整个类/父类都将被重建?(关于“获取位置”按钮和下一个字段) 类MyDialog扩展StatefulWidget{ MyDialogState createState()=>MyDialogState(); } 类MyDialogState扩展了状态{ 字符串用户位置; 双倍睡眠时间;
setState()
和变量更改。我没有在文本字段的屏幕上立即看到这些更改,我需要再次关闭并打开对话框。为什么以及如何实现这一点(这样整个类/父类都将被重建?(关于“获取位置”按钮和下一个字段)
类MyDialog扩展StatefulWidget{
MyDialogState createState()=>MyDialogState();
}
类MyDialogState扩展了状态{
字符串用户位置;
双倍睡眠时间;
@凌驾
小部件构建(构建上下文){
返回浮动操作按钮(
已按下:(){
显示对话框(
上下文:上下文,
生成器:(上下文){
返回对话框(
形状:圆形矩形边框(
边界半径:边界半径。圆形(30)
),
海拔:20,
子:ListView(
儿童:[
扁平按钮(
子项:文本(“获取位置$userLocation”),
已按下:(){
最终地理定位器Geolocator=Geolocator();
地理定位器
.getCurrentPosition()
.然后((位置)异步{
列表位置=等待地理定位器
.placemarkFromCoordinates(位置、纬度、位置、经度);
地点标记p=地点[0];
设置状态(){
//userLocation=“${p.locality},${p.country}”;
userLocation=Random().nextInt(10.toString();
印刷品(“A”);
});
}).catchError((e){
印刷品(“-------------”);
印刷品(e);
印刷品(“-------------”);
});
},
),
填充物(
填充:边缘设置。全部(10),
子:中心(子:文本(“$userLocation”))
),
分隔符(),
扁平按钮(
子项:文本(“设置睡眠长度”),
已按下:(){
//待办事项
},
),
填充物(
填充:边缘设置。全部(10),
子:居中(子:文本($sleepLength))
),
分隔符(),
],
)
);
}
);
},
子:图标(
图标。设置
),
背景颜色:Colors.black12,
);
}
}
您必须使用
返回状态生成器:…
仔细了解
}
和)代码>因为在本例中它是一个长字符串
代码片段
builder: (BuildContext context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Dialog(
...
);
},
);
使用StatefulBuilder
class MyDialog extends StatefulWidget {
MyDialogState createState() => MyDialogState();
}
class MyDialogState extends State<MyDialog> {
String userLocation;
double sleepLength;
@override
Widget build(BuildContext context) {
return FloatingActionButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30)),
elevation: 20,
child: ListView(
children: <Widget>[
FlatButton(
child: Text("Get location $userLocation"),
onPressed: () {
final Geolocator geolocator = Geolocator();
geolocator
.getCurrentPosition()
.then((Position position) async {
List<Placemark> place =
await geolocator.placemarkFromCoordinates(
position.latitude, position.longitude);
Placemark p = place[0];
setState(() {
//userLocation = "${p.locality}, ${p.country}";
userLocation = Random().nextInt(10).toString();
print("A");
});
}).catchError((e) {
print("------------");
print(e);
print("------------");
});
},
),
Padding(
padding: EdgeInsets.all(10),
child: Center(child: Text("$userLocation"))),
Divider(),
FlatButton(
child: Text("Set sleep length"),
onPressed: () {
//TODO
},
),
Padding(
padding: EdgeInsets.all(10),
child: Center(child: Text("$sleepLength"))),
Divider(),
],
));
});
});
},
child: Icon(Icons.settings),
backgroundColor: Colors.black12,
);
}
}
类MyDialog扩展StatefulWidget{
MyDialogState createState()=>MyDialogState();
}
类MyDialogState扩展了状态{
字符串用户位置;
双倍睡眠时间;
@凌驾
小部件构建(构建上下文){
返回浮动操作按钮(
已按下:(){
显示对话框(
上下文:上下文,
生成器:(上下文){
返回状态生成器(
生成器:(BuildContext上下文,StateSetter setState){
返回对话框(
形状:圆形矩形边框(
边界半径:边界半径。圆形(30)),
海拔:20,
子:ListView(
儿童:[
扁平按钮(
子项:文本(“获取位置$userLocation”),
已按下:(){
最终地理定位器Geolocator=Geolocator();
地理定位器
.getCurrentPosition()
.然后((位置)异步{
排名=
等待geolocator.placemarkFromCoordinates(
位置、纬度、位置、经度);
地点标记p=地点[0];
设置状态(){
//userLocation=“${p.locality},${p.country}”;
userLocation=Random().nextInt(10.toString();
印刷品(“A”);
});
}).catchError((e){
印刷品(“-------------”);
印刷品(e);
印刷品(“-------------”);
});
},
),
填充物(
填充:边缘设置。全部(10),
子:中心(子:文本($userLocation)),
分隔符(),
扁平按钮(
子项:文本(“设置睡眠长度”),
已按下:(){
//待办事项
},
),
填充物(
填充:边缘设置。全部(10),
子:居中(子:文本($sleepLength)),
分隔符(),
],
));
});
});
},
子:图标(Icons.settin)
class MyDialog extends StatefulWidget {
MyDialogState createState() => MyDialogState();
}
class MyDialogState extends State<MyDialog> {
String userLocation;
double sleepLength;
@override
Widget build(BuildContext context) {
return FloatingActionButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30)),
elevation: 20,
child: ListView(
children: <Widget>[
FlatButton(
child: Text("Get location $userLocation"),
onPressed: () {
final Geolocator geolocator = Geolocator();
geolocator
.getCurrentPosition()
.then((Position position) async {
List<Placemark> place =
await geolocator.placemarkFromCoordinates(
position.latitude, position.longitude);
Placemark p = place[0];
setState(() {
//userLocation = "${p.locality}, ${p.country}";
userLocation = Random().nextInt(10).toString();
print("A");
});
}).catchError((e) {
print("------------");
print(e);
print("------------");
});
},
),
Padding(
padding: EdgeInsets.all(10),
child: Center(child: Text("$userLocation"))),
Divider(),
FlatButton(
child: Text("Set sleep length"),
onPressed: () {
//TODO
},
),
Padding(
padding: EdgeInsets.all(10),
child: Center(child: Text("$sleepLength"))),
Divider(),
],
));
});
});
},
child: Icon(Icons.settings),
backgroundColor: Colors.black12,
);
}
}
class MyDialogState extends State<MyDialog> {
String userLocation;
double sleepLength;
@override
Widget build(BuildContext context) {
return FloatingActionButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30)
),
elevation: 20,
child: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return ListView(
children: <Widget>[
FlatButton(
child: Text("Get location $userLocation"),
onPressed: () {
final Geolocator geolocator = Geolocator();
geolocator
.getCurrentPosition()
.then((Position position) async {
List<Placemark> place = await geolocator
.placemarkFromCoordinates(position.latitude, position.longitude);
Placemark p = place[0];
setState(() {
//userLocation = "${p.locality}, ${p.country}";
userLocation = Random().nextInt(10).toString();
print("A");
});
}).catchError((e) {
print("------------");
print(e);
print("------------");
});
},
),
Padding(
padding: EdgeInsets.all(10),
child: Center(child: Text("$userLocation"))
),
Divider(),
FlatButton(
child: Text("Set sleep length"),
onPressed: () {
//TODO
},
),
Padding(
padding: EdgeInsets.all(10),
child: Center(child: Text("$sleepLength"))
),
Divider(),
],
);
},
)
);
}
);
},
child: Icon(
Icons.settings
),
backgroundColor: Colors.black12,
);
}
}