Dart AlertDialog设置函数/OnTap中的状态
新的振翅。我知道如何设置警报对话框的状态,但由于需要点击以实现类似()=>\u createPlayer的功能,它不希望重建警报对话框。 我想知道当你需要点击它们时,如何设置“警报状态”对话框Dart AlertDialog设置函数/OnTap中的状态,dart,flutter,android-alertdialog,Dart,Flutter,Android Alertdialog,新的振翅。我知道如何设置警报对话框的状态,但由于需要点击以实现类似()=>\u createPlayer的功能,它不希望重建警报对话框。 我想知道当你需要点击它们时,如何设置“警报状态”对话框 File _image; GestureDetector( onTap: () => _createPlayer(), 轻触后,将显示如下警告对话框: _createPlayer() { return showDialog( co
File _image;
GestureDetector(
onTap: () => _createPlayer(),
轻触后,将显示如下警告对话框:
_createPlayer() {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(32.0))),
content: Container(
height: 400,
width: 300,
child: Column(
children: <Widget>[
Text('Create Player', style: Theme
.of(context)
.textTheme
.body1),
GestureDetector(
onTap: _getImageCamera,
child: CircleAvatar(
radius: 100,
backgroundColor: Colors.white,
backgroundImage: _image != null ? FileImage(_image) : AssetImage('assets/images/undercover.png'),
),
),
],
),
),
);
});
}
_getImageCamera() async{
var image = await ImagePicker.pickImage(source: ImageSource.camera);
setState(() {
_image = image;
});
}
\u createPlayer(){
返回显示对话框(
上下文:上下文,
生成器:(BuildContext上下文){
返回警报对话框(
形状:圆形矩形边框(
borderRadius:borderRadius.all(半径.圆形(32.0)),
内容:容器(
身高:400,
宽度:300,
子:列(
儿童:[
文本(“创建播放器”,样式:主题
.of(上下文)
.文本主题
.1),,
手势检测器(
onTap:_getImageCamera,
孩子:圆环星(
半径:100,
背景颜色:Colors.white,
背景图像:_image!=null?文件图像(_image):AssetImage('assets/images/undercover.png'),
),
),
],
),
),
);
});
}
_getImageCamera()异步{
var image=等待ImagePicker.pickImage(源:ImageSource.camera);
设置状态(){
_图像=图像;
});
}
选中时,我想在“警报”对话框中设置状态/更改图像。有什么想法吗?为AlertDialog创建一个单独的有状态小部件CustomDialog,并将_getImageCamera函数_image变量按如下方式移动到其中
_createPlayer() {
return CustomDialog();
}
class CustomDialog extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return CustomDialogState();
}
}
class CustomDialogState extends State<CustomDialog> {
ImageProvider _image;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(32.0))),
content: Container(
height: 400,
width: 300,
child: Column(
children: <Widget>[
Text('Create Player', style: Theme
.of(context)
.textTheme
.body1),
GestureDetector(
onTap: _getImageCamera,
child: CircleAvatar(
radius: 100,
backgroundColor: Colors.white,
backgroundImage: _image != null ? FileImage(_image) : AssetImage('assets/images/undercover.png'),
),
),
],
),
),
);
});
}
_getImageCamera() async{
var image = await ImagePicker.pickImage(source: ImageSource.camera);
setState(() {
_image = image;
});
}
}
\u createPlayer(){
返回CustomDialog();
}
类CustomDialog扩展StatefulWidget{
@凌驾
状态createState(){
返回CustomDialogState();
}
}
类CustomDialogState扩展状态{
ImageProvider\u图像;
@凌驾
void initState(){
super.initState();
}
@凌驾
小部件构建(构建上下文){
返回警报对话框(
形状:圆形矩形边框(
borderRadius:borderRadius.all(半径.圆形(32.0)),
内容:容器(
身高:400,
宽度:300,
子:列(
儿童:[
文本(“创建播放器”,样式:主题
.of(上下文)
.文本主题
.1),,
手势检测器(
onTap:_getImageCamera,
孩子:圆环星(
半径:100,
背景颜色:Colors.white,
背景图像:_image!=null?文件图像(_image):AssetImage('assets/images/undercover.png'),
),
),
],
),
),
);
});
}
_getImageCamera()异步{
var image=等待ImagePicker.pickImage(源:ImageSource.camera);
设置状态(){
_图像=图像;
});
}
}
要查看showDialog
上的UI更改,您必须创建一个新的StatefulWidget
,然后在该类中使用dialog 您可以使用StatefulBuilder在对话框内进行更改
showDialog(
context: context,
builder: (context) {
String contentText = "Content of Dialog";
// add StatefulBuilder to return value
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text("Title of Dialog"),
content: Text(contentText),
actions: <Widget>[
FlatButton(
onPressed: () => Navigator.pop(context),
child: Text("Cancel"),
),
FlatButton(
onPressed: () {
setState(() {
contentText = "Changed Content of Dialog";
});
},
child: Text("Change"),
),
],
);
},
);
},
);
showDialog(
上下文:上下文,
生成器:(上下文){
String contentText=“对话框的内容”;
//将StatefulBuilder添加到返回值
返回状态生成器(
生成器:(上下文,设置状态){
返回警报对话框(
标题:文本(“对话框标题”),
内容:文本(contentText),
行动:[
扁平按钮(
onPressed:()=>Navigator.pop(上下文),
子项:文本(“取消”),
),
扁平按钮(
已按下:(){
设置状态(){
contentText=“更改了对话框的内容”;
});
},
子项:文本(“更改”),
),
],
);
},
);
},
);
最愚蠢、最快速的修复方法是:
Navigator.of(context).pop();
然后再次调用showDialog()
。
将有一个微延迟,但工作