我正在调用一个方法,并试图在完成横切后返回。但是代码不起作用。这是我正在尝试的,
onPressed: (){
_updateResult(context);
},
void _updateResult(BuildContext context) async{
// some api calls and checks goes here
Navigator.of(context).pop();
}
这是我访问页面的方式
Navigator.push(context, SlideLeftRoute(page: EnterResult(item)));
我有一个自定义类SlideLeftRoute
class SlideLeftRoute extends PageRouteBuilder {
final Widget page;
SlideLeftRoute({this.page})
: super(
pageBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) =>
page,
transitionsBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) =>
SlideTransition(
position: Tween<Offset>(
begin: const Offset(1, 0),
end: Offset.zero,
).animate(animation),
child: child,
),
);
}
问题代码
我发现了哪个代码会产生问题。在这里,我正在调用另一种显示对话的方法。如果我评论该代码有效
void _showMsg(msg) {
// flutter defined function
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
backgroundColor: Colors.grey[800],
content: new Text(msg,
style: TextStyle(
color: Colors.white,
fontSize: 14.0,
decoration: TextDecoration.none,
fontFamily: 'Lato',
fontWeight: FontWeight.normal,
),
),
//content: new Text("Alert Dialog body"),
actions: <Widget>[
// usually buttons at the bottom of the dialog
new FlatButton(
child: new Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
因此,在完成api调用后,它应该转到后退屏幕,但是什么也没有发生。
谢谢。
答案 0 :(得分:1)
问题是因为当您从警报Navigator.of(context).pop();
的操作中调用AlertDialog
时,它将弹出显示的对话框。您必须再次弹出屏幕。为此,您可以使用将来通过showDialog
方法返回。
这将更改您的_showMsg
方法,如下所示。
请注意then(..)...
上的showDialog
void _showMsg(msg) async {
// flutter defined function
await showDialog(
context: context,
builder: (BuildContext localContext) {
// return object of type Dialog
return AlertDialog(
backgroundColor: Colors.grey[800],
content: new Text(
msg,
style: TextStyle(
color: Colors.white,
fontSize: 14.0,
decoration: TextDecoration.none,
fontFamily: 'Lato',
fontWeight: FontWeight.normal,
),
),
//content: new Text("Alert Dialog body"),
actions: <Widget>[
// usually buttons at the bottom of the dialog
new FlatButton(
child: new Text("Close"),
onPressed: () async {
print("poping");
Navigator.of(context).pop();
},
),
],
);
},
).then((value) {
print("poping from screen");
Navigator.of(context).pop();
});
}