通过在 AlertDialog 的子窗口小部件中使用 setstate 设置 AlertDialog 屏幕的状态
Future<void> ChangeProfile(BuildContext context) {
final pro = Provider.of<Pro>(context, listen: false);
FirebaseFirestore fireStore = FirebaseFirestore.instance;
US(context);
int count = 0;
return showDialog<void>(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return AlertDialog(
// RoundedRectangleBorder - Dialog 화면 모서리 둥글게 조절
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0)),
//Dialog Main Title
title: Column(
children: <Widget>[
SizedBox(
width: 50.w,
height: 50.h,
child: Image.asset(
pro.currentProfile
),
)
],
),
//
content: SizedBox(
width: 400.w,
height: 400.h,
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
PrintProfile(1,"imagename");
这是我的 AlertDialog 代码,我使用 StatefulBuilder
来更改 AlertDialog 内的屏幕以及
class PrintProfile extends StatefulWidget {
PrintProfile(this.num,this.text);
final int num;
final String text;
@override
State<PrintProfile> createState() => _PrintProfileState();
}
class _PrintProfileState extends State<PrintProfile> {
@override
Widget build(BuildContext context) {
ParentState parent = context.findAncestorStateOfType<ParentState>();
final int num = widget.num;
final String text = widget.text;
final pro = Provider.of<Pro>(context);
final double tophe = MediaQuery.of(context).padding.top;
US(context);
return SizedBox(
width: 70.w,
height: 70.h,
child: OutlinedButton(
onPressed: (){
setState(() {
pro.currentProfile= text;
});
},
child: Image.asset(text),
),
);
}
}
PrintProfile 小部件。当从 AlertDialog 调用此小部件时,必须更改 AlertDialog 的输出部分 pro.currentProfile,并且必须设置父 AlertDialog setstate。
ParentState parent = context.findAncestorStateOfType<ParentState>();
我找到了这样的解决方案,但由于父窗口小部件是一个AlertDialog,我必须使用另一种方法。但我不知道。
问题摘要:当使用 StatefulBuilder 在 AlertDialog 中的子窗口小部件中按下按钮时,我想让 setstate 在父 AlertDialog 中运行。
Future<void> ChangeProfile(BuildContext context) {
final pro = Provider.of<Pro>(context, listen: false);
FirebaseFirestore fireStore = FirebaseFirestore.instance;
US(context);
int count = 0;
return showDialog<void>(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return AlertDialog(
// RoundedRectangleBorder - Dialog 화면 모서리 둥글게 조절
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0)),
//Dialog Main Title
title: Column(
children: <Widget>[
SizedBox(
width: 50.w,
height: 50.h,
child: Image.asset(
pro.currentProfile
),
)
],
),
//
content: SizedBox(
width: 400.w,
height: 400.h,
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
PrintProfile(1,"imagename");
This is my AlertDialog code I used Statefu
lBuilder to change the screen inside the AlertDialog too
class PrintProfile extends StatefulWidget {
PrintProfile(this.num,this.text);
final int num;
final String text;
@override
State<PrintProfile> createState() => _PrintProfileState();
}
class _PrintProfileState extends State<PrintProfile> {
@override
Widget build(BuildContext context) {
ParentState parent = context.findAncestorStateOfType<ParentState>();
final int num = widget.num;
final String text = widget.text;
final pro = Provider.of<Pro>(context);
final double tophe = MediaQuery.of(context).padding.top;
US(context);
return SizedBox(
width: 70.w,
height: 70.h,
child: OutlinedButton(
onPressed: (){
setState(() {
pro.currentProfile= text;
});
},
child: Image.asset(text),
),
);
}
}
PrintProfile widget. When this widget is called from AlertDialog, the output part of AlertDialog, pro.currentProfile, must be changed and the parent AlertDialog setstate must be set.
ParentState parent = context.findAncestorStateOfType<ParentState>();
I found such a solution, but since the parent widget is an AlertDialog, I have to use another method. But I don't know.
Summary of Question: I want to make the setstate run in the parent AlertDialog when a button is pressed in the child widget in the AlertDialog using StatefulBuilder.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个类将为您提供使用当前类的父类 setstate 的明确答案,与您可以为 AlertDialog 类执行的方法相同
This class will give you clear answer for use parent class setstate from current class, the same method you can do for your AlertDialog class