【问题标题】:Flutter navigation颤振导航
【发布时间】:2020-07-14 01:46:02
【问题描述】:

有人能解释一下为什么当efeioi 从 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));

我不能在页面 E 中使用 Navigator.pop,因为它会返回页面 C!

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

https://github.com/tony123S/navigation

【问题讨论】:

  • 页面 E 不是导航到页面 A,仅弹出页面。这就是为什么 print("efeioi");不是执行者。
  • @AnantaPrasad 处理这个问题的正确方法是什么?我希望它从 E 回到 A
  • 在回答中我已经展示了正确的处理方式和工作代码。

标签: flutter dart navigation


【解决方案1】:

根据您的要求,我已实现如下 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'),
    ),
  ),
);
  }
}

如果您发现任何困难,请运行代码以便更好地理解和回复

【讨论】:

  • 但是我的项目流程需要从A页到E页,然后再到A页,
  • 这里 我共享的代码执行相同的操作,即导航 Page FirstRoute 到 SecondRoute,然后到 FirstRoute 并调用 pagerefresh。运行你会明白的代码
  • 在pageE中,我不能使用Navigator.pop(context);。如果使用它,它将返回页面 C。
  • 我已按要求更新了代码,请运行以加深理解
猜你喜欢
  • 2021-11-24
  • 2019-06-04
  • 2020-10-10
  • 2019-07-01
  • 1970-01-01
  • 2019-12-16
  • 2020-08-22
  • 2018-10-11
  • 1970-01-01
相关资源
最近更新 更多