我正在向用户显示一个对话框,我想在单击对话框的肯定按钮时更改对话框的标题,但是我该怎么做。
由于对话框已经在屏幕上可见,因此setState()
将无法执行任何操作。
代码:
String title = "Old Title" // member variable
RaisedButton(onPressed: (){
showDialog(context: context, builder: (context) {
return AlertDialog(title: Text(title), actions: <Widget>[FlatButton(onPressed: () {
setState(() => title = "New Title");
}, child: Text("Change"))],);
});
}, child: Text("Change"),)
答案 0 :(得分:2)
您可以创建一个返回Collection<Object>
的{{1}}。点击更改按钮时,Collection<StringBuilder>
将更新对话框的文本值。
StatefulWidget
答案 1 :(得分:0)
您可以执行此操作-无需创建其他小部件
RaisedButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
StreamController<String> controller = StreamController<String>.broadcast();
return AlertDialog(
title: StreamBuilder(
stream: controller.stream,
builder: (BuildContext context, AsyncSnapshot<String> snapshot){
return Text(snapshot.hasData ? snapshot.data : 'Title');
}),
actions: [
FlatButton(
onPressed: () {
controller.add('New Title');
},
child: Text('Change'))
],
);
});
},
child: Text('Change'),
);
答案 2 :(得分:0)
showDialog(
barrierDismissible: false,
context: context,
builder: (context) {
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text(title),
actions: <Widget>[
FlatButton(
onPressed: () {
setState(() => title = "New Title");
},
child: Text("Change"))
],
);
},
);
},
);