MaterialButton onPressed 侦听器不起作用(Flutter)
MaterialButton onPressed listener not work (Flutter)
我创建了这样的布局,问题是 onPressed 侦听器不起作用,但我不明白为什么:
Material(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(22.0)),
color: const Color.fromRGBO(33, 63, 132, 1),
clipBehavior: Clip.antiAlias,
child: MaterialButton(
height: 140.0,
minWidth: 380.0,
elevation: 20.0,
color: const Color.fromRGBO(33, 63, 132, 1),
textColor: Colors.white,
child: Text("Chiudi sessione / Cambia utente",
style: TextStyle(
fontSize: ScreenUtil().setSp(20),
)),
onPressed: () {
var future = endUserSession(badgeCode, false, context);
future.then((value) {
setUserBadgeForDualMode("");
setDoorStatusIntoPrefs("");
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const OpenDoorDualMode()));
});
}
),
)
我做错了什么?
问题似乎出在你的函数调用上,你不是在等待未来的价值,那样使用它
var future = await endUserSession(badgeCode, false, context);
或者尝试删除它并像这样调用你的函数
await endUserSession(badgeCode, false, context).then((value) async {
await setUserBadgeForDualMode("");
await setDoorStatusIntoPrefs("");
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const OpenDoorDualMode()));
});
在任何需要未来值的地方使用“await”。
我创建了这样的布局,问题是 onPressed 侦听器不起作用,但我不明白为什么:
Material(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(22.0)),
color: const Color.fromRGBO(33, 63, 132, 1),
clipBehavior: Clip.antiAlias,
child: MaterialButton(
height: 140.0,
minWidth: 380.0,
elevation: 20.0,
color: const Color.fromRGBO(33, 63, 132, 1),
textColor: Colors.white,
child: Text("Chiudi sessione / Cambia utente",
style: TextStyle(
fontSize: ScreenUtil().setSp(20),
)),
onPressed: () {
var future = endUserSession(badgeCode, false, context);
future.then((value) {
setUserBadgeForDualMode("");
setDoorStatusIntoPrefs("");
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const OpenDoorDualMode()));
});
}
),
)
我做错了什么?
问题似乎出在你的函数调用上,你不是在等待未来的价值,那样使用它
var future = await endUserSession(badgeCode, false, context);
或者尝试删除它并像这样调用你的函数
await endUserSession(badgeCode, false, context).then((value) async {
await setUserBadgeForDualMode("");
await setDoorStatusIntoPrefs("");
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const OpenDoorDualMode()));
});
在任何需要未来值的地方使用“await”。