简体   繁体   English

Flutter:在弹出对话框中导航

[英]Flutter: Navigation in a pop-up dialog

Is it possible to use a separate Navigator in a pop up window?是否可以在弹出窗口中使用单独的导航器 window? Separate BuildContext?单独的 BuildContext?

I have a working app (APP1) and I would like to show it in a new one (APP2) (eg on a button press to open the existing app).我有一个工作应用程序 (APP1),我想在一个新应用程序 (APP2) 中显示它(例如,按下按钮打开现有应用程序)。 APP1 has multiple pages. APP1 有多个页面。

I was able add the APP1 as an dependency to APP2 and load the main page in a popup dialog (Alert dialog).我能够将 APP1 添加为 APP2 的依赖项,并在弹出对话框(警报对话框)中加载主页。

The issue happens when I try to navigate through the APP1.当我尝试浏览 APP1 时会出现此问题。 If I click on the button in the APP1 it changes the page in the whole new app.如果我单击 APP1 中的按钮,它会更改整个新应用程序中的页面。

I would like to have separate navigation in the pop up dialog, so that the included app Navigator works only in the pop-up dialog.我想在弹出对话框中有单独的导航,以便包含的应用程序导航器仅在弹出对话框中工作。

What would be a preferred/possible way to achieve this?实现此目标的首选/可能方法是什么?

Here is a small example: Soure code这是一个小例子: 源代码

The example consists of 2 apps (APP1 and APP2).该示例包含 2 个应用程序(APP1 和 APP2)。 APP2 includes APP1 and shows it in a pop up dialog. APP2 包含 APP1 并在弹出对话框中显示它。

You can add a nested Navigator (if you're using Navigator 1.0 in Flutter) by adding the Navigator widget inside your dialog component, that way you manage the navigation of pages within that dialog itself, create nested routes and navigate only within that navigation stack.您可以通过在对话框组件中添加Navigator小部件来添加嵌套导航器(如果您在 Flutter 中使用 Navigator 1.0),这样您就可以在该对话框本身中管理页面导航,创建嵌套路由并仅在该导航堆栈中导航.

You can do something like:你可以这样做:

class NestedDialog extends StatelessWidget {
 
   @override
   Widget build(BuildContext context) {
     return Scaffold(
       body: Navigator(
          key: <ADD_UNIQUE_KEY>, // add a unique key to refer to this navigator programmatically
          initialRoute: '/',
          onGenerateRoute: (RouteSettings settings) {
              // here you return your own page widgets wrapped
              // inside a PageRouteBuilder
          }
       )
     );
   }
}

Then you can even use Flutter 's own showDialog and load your custom dialog widget like so:然后您甚至可以使用Flutter自己的showDialog并加载您的自定义对话框小部件,如下所示:

showDialog(context: context,
      barrierDismissible: true,
      builder: (BuildContext cxt) {
      return AlertDialog(
        content: NestedDialog()
      );
    });

Something along those lines.沿着这些路线的东西。 Give that a shot.试一试。 Here's a Github Gist you can run on DartPad.dev so you can see what I mean.这是一个 Github Gist,你可以在 DartPad.dev 上运行,这样你就明白我的意思了。 You don't have to create two apps;您不必创建两个应用程序; just distribute your app into two main widgets, and the child pages go under each main widget's child pages;只需将您的应用分发到两个主要小部件,以及每个主要小部件的子页面下的子页面 go; See gist https://gist.github.com/romanejaquez/d769e6e766fbacb2f5c166dd3bceab51 .参见要点https://gist.github.com/romanejaquez/d769e6e766fbacb2f5c166dd3bceab51 Run it on DartPad.dev.在 DartPad.dev 上运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM