我有一个可以来自应用程序各个部分的小部件。我该怎么做,如果我来自 StatefulWidget1 推送,它会直接将我带到主小部件,如果我来自 StatefulWidget2 推送,它会在堆栈具有的路由。
我当前的 WillPopScope 函数总是转到主小部件。下面评论说我输入了我想做的伪代码。
return WillPopScope(
onWillPop: () async {
Navigator.of(context).popUntil((route) => route.isFirst);
//if(the route name of the parent widget where i come via push == analysis)
//Navigator.of(context).popUntil((route) => route.isFirst);
// else
//Navigator.of(context).pop();
return false;
},
child: Scaffold( ...
我已经搜索过了,但找不到任何对这个问题有用的东西。
最佳答案
您可以在您的类中传递一个可以从应用程序的各个部分访问的属性。
class YourClass extends StatefulWidget {
final String whereICameFrom;
YourClass({@required this.whereICameFrom});
@override
YourClassState createState() => YourClassState();
}
并且在您的 WillPopScope 中,您可以在 switch case 中使用此属性,例如:
onWillPop: () async {
switch(widget.whereICameFrom){
case "StatefulWidget1":
Navigator.of(context).popUntil((route) => route.isFirst);
break;
case "StatefulWidget2":
Navigator.of(context).pop();
break;
}
return false;
},
关于Flutter:如何根据你来自哪里弹出/导航到不同的路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64881565/