颤振导航 [英] Flutter navigation

查看:85
本文介绍了颤振导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can someone explain why not printing efeioi when it is back from pageE?

A页

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

页面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

页面C

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

页面E

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

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

I can't use Navigator.pop in Page E because it will back to Page C!

我已经在此处上传了完整的代码

I have uploaded full code here

https://github.com/tony123S/navigation

推荐答案

根据您的要求,我已如下实现 main.dart

As per your requirement I have implemented as below main.dart

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'),
    ),
  ),
);
  }
}

请运行代码以更好地理解并发现困难

Please run code for better understanding and reply if you found any difficulty

这篇关于颤振导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆