Container(
margin: EdgeInsets.fromLTRB(220.w,155.h,0,0),
child: IconButton(
onPressed: (){
setState(() {
ChangeProfile(context);
});
},
icon: Icon(Icons.wifi_protected_setup),
),
),
This is the code that opens an AlertDialog to change the profile.
void ChangeProfile(BuildContext context) {
final pro = Provider.of<Pro>(context, listen: false);
US(context);
int count = 0;
showDialog(
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>[
Container(
width: 50.w,
height: 50.h,
child: Image.asset(
pro.currentProfile
),
)
],
),
//
content: SizedBox(
width: 230.w,
height: 230.h,
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox
(
width: 70.w,
height: 70.h,
child: OutlinedButton(
onPressed: (){
setState(() { // setState() 추가.
pro.currentProfile= 'image_profile/profile_1.png';
});
},
child: Image.asset('image_profile/profile_1.png'),
),
),
SizedBox(
width: 70.w,
height: 70.h,
child: OutlinedButton(
onPressed: (){
setState(() { // setState() 추가.
pro.currentProfile= 'image_profile/profile_2.png';
});
},
child: Image.asset('image_profile/profile_2.png'),
),
),
In this way, when the button in the AlertDialog is pressed, the currentProfile variable in the Provider is changed.
actions: <Widget>[
TextButton(
child: Text(
"닫기",
style: TextStyle(
color: Colors.black,
),
),
onPressed: () {
setState(() {
Navigator.pop(context);
});
},
),
],
Click the close button and it will pop up
PrintImage(
imagetext: pro.currentProfile,
xsize: 100.0,
ysize: 100.0,
hhh: 90.0,
),
When the currentProfile variable changes, the parent widget will output something like this
The values inside the AlertDialog are also successfully changed and when I check using the StatefulBuilder it changes well in real time. When I close the AlertDialog, the parent widget does nothing.
When another TextButton is pressed, PrintImage responds to the changed currentProfile value.
Is there a way to close the AlertDialog and refresh the build?
CodePudding user response:
Yes there is a way.
return showDialog
(which is Future) from your ChangeProfile
method
Since ChangeProfile
method returns a Future we can now use .then method on the ChangeProfile
and can refresh the build using setState
.
As soon as the alert dialog is closed future gets completed and .then method executes.
Container(
margin: EdgeInsets.fromLTRB(220.w,155.h,0,0),
child: IconButton(
onPressed: (){
ChangeProfile(context).then(
setState(() {
// update your widget
})
);
},
icon: Icon(Icons.wifi_protected_setup),
),
),
Future<void> ChangeProfile(BuildContext context) {
final pro = Provider.of<Pro>(context, listen: false);
US(context);
int count = 0;
return showDialog<void>(
// paste rest of your code here
);
}