Container(
margin: EdgeInsets.fromLTRB(220.w,155.h,0,0),
child: IconButton(
onPressed: (){
setState(() {
ChangeProfile(context);
});
},
icon: Icon(Icons.wifi_protected_setup),
),
),
這是打開 AlertDialog 以更改組態檔的代碼。
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'),
),
),
這樣,當AlertDialog中的按鈕被按下時,Provider中的currentProfile變數就會改變。
actions: <Widget>[
TextButton(
child: Text(
"??",
style: TextStyle(
color: Colors.black,
),
),
onPressed: () {
setState(() {
Navigator.pop(context);
});
},
),
],
點擊關閉按鈕,它會彈出
PrintImage(
imagetext: pro.currentProfile,
xsize: 100.0,
ysize: 100.0,
hhh: 90.0,
),
當 currentProfile 變數發生變化時,父視窗小部件會輸出類似這樣的內容
AlertDialog 中的值也已成功更改,當我使用 StatefulBuilder 檢查時,它實時更改得很好。當我關閉 AlertDialog 時,父小部件什么也不做。
當按下另一個 TextButton 時,PrintImage 會回應更改的 currentProfile 值。
有沒有辦法關閉 AlertDialog 并重繪 構建?
uj5u.com熱心網友回復:
是的,有辦法。從您的方法中回傳showDialog(即 Future)ChangeProfile由于ChangeProfile方法回傳 Future,我們現在可以在 .then 上使用 .then 方法,ChangeProfile并且可以使用setState.
一旦警報對話框關閉,future 就會完成并執行 .then 方法。
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
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/433470.html
