gpt4 book ai didi

flutter - flutter 导航

转载 作者:行者123 更新时间:2023-12-03 03:56:21 29 4
gpt4 key购买 nike

有人可以解释为什么从pageE返回时不打印efeioi吗?

页面A

Navigator.pushNamed(context, PageB.ROUTE).then((onValue) {
print("efeioi");
});

Page B
  Navigator.of(context)
.pushReplacementNamed(PageC.ROUTE, arguments: onValue);

PageC
 Navigator.pushNamed(context, PageD.ROUTE,
arguments: onValue);

PageD
    Navigator.pop(context);  // back to Page C

Page C
   Navigator.pushNamed(context, PageE.ROUTE,
arguments: onValue);

Page E
 Navigator.of(context).popUntil(ModalRoute.withName(PageA.ROUTE));

我无法在Page E中使用Navigator.pop,因为它将返回到C页!

我在这里上传了完整的代码

https://github.com/tony123S/navigation

最佳答案

根据您的要求,我已实现如下
主镖

initState : this will be called when you navigate from E to A
refreshPage : it will not called as you already popped before returning to A Page



import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: A(),
routes: <String, WidgetBuilder>{
'/A': (BuildContext context) => new A(),
'/B': (BuildContext context) => new B(),
'/C': (BuildContext context) => new C(),
'/D': (BuildContext context) => new D(),
'/E': (BuildContext context) => new E(),
},
);
}
}

class A extends StatefulWidget {
@override
_FirstRouteState createState() => _FirstRouteState();
}

class _FirstRouteState extends State<A> {
final String fromPage;

_FirstRouteState({Key key, @required this.fromPage});

@override
void initState() {
// TODO: implement initState
super.initState();
print("Called askdfjaksdfj");
}

@override
Widget build(BuildContext context) {

return Scaffold(
appBar: AppBar(
title: Text('Page A'),
),
body: Center(
child: RaisedButton(
child: Text('Open B'),
onPressed: () {
// Navigate to second route when tapped.
// Navigator.push(
// context,
// MaterialPageRoute(builder: (context) => B()),
// );

Navigator.push(
context,
MaterialPageRoute(builder: (context) => B()),
).then((res) => refreshPage());
},
),
),
);
}

refreshPage() {
print("refresh page is called");
}
}

class B extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("B Page"),
),
body: Center(
child: RaisedButton(
onPressed: () {
// Navigate back to first route when tapped.
Navigator.of(context).pushNamed(
"/C",
);
},
child: Text('Go to C'),
),
),
);
}
}

class C extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("C Page"),
),
body: Center(
child: Column(
children: <Widget>[
RaisedButton(
onPressed: () {
// Navigate back to first route when tapped.
Navigator.pushNamed(
context,
"/D",
);
},
child: Text('Go to D'),
),
RaisedButton(
onPressed: () {
// Navigate back to first route when tapped.
Navigator.pushNamed(
context,
"/E",
);
},
child: Text('Go to E'),
),
],
),
),
);
}
}

class D extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("D Page"),
),
body: Center(
child: RaisedButton(
onPressed: () {
// Navigate back to first route when tapped.
Navigator.pop(context);
},
child: Text('Go back to C'),
),
),
);
}
}

class E extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("E Page"),
),
body: Center(
child: RaisedButton(
onPressed: () {
// Navigator.pop(context);
// Navigator.of(context).pushNamed("/A");
// Navigator.of(context).popUntil(ModalRoute.withName('/A'));
Navigator.of(context)
.pushNamedAndRemoveUntil('/A', (Route<dynamic> route) => false,);

},
child: Text('Go to A'),
),
),
);
}
}


如果遇到任何困难,请运行代码以更好地理解并回复

关于flutter - flutter 导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60985638/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com