Container(
margin: EdgeInsets.fromLTRB(220.w,155.h,0,0),
child: IconButton(
onPressed: (){
ChangeProfile(context).then(
setState(() {
profile = pro.currentProfile;
})
);
},
icon: Icon(Icons.wifi_protected_setup),
),
),
這是執行 ChangeProfile 函式的部分。更改執行后輸出到 main 的名為 profile 的變數
PrintImage(
imagetext: profile,
xsize: 100.0,
ysize: 100.0,
hhh: 90.0,
),
像這樣輸出
Future<void> ChangeProfile(BuildContext context) {
final pro = Provider.of<Pro>(context, listen: false);
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>[
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'),
),
),
有人告訴我使用 Future 以便完成 AlertDialog 并重繪 螢屏,但出現錯誤
error: This expression has a type of 'void' so its value can't be used. (use_of_void_result at [goodman] lib\3_userscreen\userscreen.dart:74)
在 AlertDialog 中,我通過使用 Provider 以使用 StatefulBuilder 顯示影像來檢查實時更改。父小部件的輸出部分包含 Provider 的內容,但它不會被反映,必須通過按下另一個按鈕來應用。
我使用了 FUTURE 然后,但我得到一個 void 錯誤
這是反饋之前的代碼:Statefulwidget does not refresh after AlertDialog is closed
uj5u.com熱心網友回復:
這部分不正確:
onPressed: (){
ChangeProfile(context).then(
setState(() {
profile = pro.currentProfile;
})
);
},
.then要求您向它傳遞一個回呼函式,如下所示:
onPressed: (){
ChangeProfile(context).then((_) {
setState(() {
profile = pro.currentProfile;
});
});
},
或者,您可以使用async/ await:
onPressed: () async {
await ChangeProfile(context);
setState(() {
profile = pro.currentProfile;
});
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/433465.html
