我有4个屏幕。 X,A,B,C导航模式应如下
这是代码
X屏幕
class X extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: FlatButton(
child: Text('Navigate to A'),
onPressed: () {
A.show(context);
},
),
);
}
}
一个屏幕
class A extends StatelessWidget {
static Future<void> show(BuildContext context) async {
await Navigator.of(context).push(
MaterialPageRoute(builder: (context) => A(), fullscreenDialog: false),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: FlatButton(
child: Text('Navigate to B'),
onPressed: () {
B.show(context);
},
),
),
);
}
}
B级
class B extends StatelessWidget {
static Future<void> show(BuildContext context) async {
await Navigator.of(context).push(
MaterialPageRoute(builder: (context) => B(), fullscreenDialog: false),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: FlatButton(
child: Text('Navigate to C'),
onPressed: () {
C.show(context);
},
),
),
);
}
}
C级
class C extends StatelessWidget {
static Future<void> show(BuildContext context) async {
await Navigator.of(context).push(
MaterialPageRoute(builder: (context) => C(), fullscreenDialog: false),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: FlatButton(
child: Text('Navigate back to A'),
onPressed: () {
A.show(context); //<=== here is problem
},
),
),
);
}
}
我遇到的问题是,当我从屏幕C导航回到屏幕A,然后按应用栏上的后退按钮,然后又回到屏幕C。我需要能够从C导航回到B,从B导航到A,然后从A到X,但是一旦我从C导航到AI,则需要在按下按钮时导航回到X或再次导航到B
更新
我想我是用尝试失败技术来解决这个问题的,老实说,我不明白为什么它会按照我的意愿工作。如果您知道为什么这样做并有解释,请提供答案。这是我所做的
我在屏幕A中添加了新的静态功能
static Future<void> back(BuildContext context) async {
await Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) => A(),
fullscreenDialog: false),
ModalRoute.withName('/'));
}
并在我的按钮的屏幕C中调用它
A.back(context)
答案 0 :(得分:1)
更改代码
class A extends StatelessWidget {
static Future<void> show(BuildContext context) async {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => A(),
settings: RouteSettings(name: 'A'), // <-- change 1 give a name
fullscreenDialog: false),
);
}
...
class C extends StatelessWidget {
...
onPressed: () {
Navigator.popUntil(context, (route) => route.settings.name == 'A'); //<=== will pop until A change 2
...
更新A.back
中发生了什么?
缺点-在iOS swipe back
上调用Navigator.pop
的行为,在这种情况下无法直接调用A.back
static Future<void> back(BuildContext context) async {
await Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) => A(),
fullscreenDialog: false),
ModalRoute.withName('/'));
}
答案 1 :(得分:1)
class C extends StatelessWidget {
static Future<void> show(BuildContext context) async {
await Navigator.of(context).push(
MaterialPageRoute(builder: (context) => C(), fullscreenDialog: false),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: FlatButton(
child: Text('Navigate back to A'),
onPressed: () {
int count = 0;
Navigator.of(context).popUntil((_) => count++ >= 2);
},
),
),
);
}
}