Flutter navigation

天涯浪子 提交于 2020-04-16 12:47:51

问题


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

Page 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));

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


回答1:


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



来源:https://stackoverflow.com/questions/60985638/flutter-navigation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!