应用加载时将默认路由('/')添加到导航器堆栈中 [英] Will default route('/') add to the Navigator Stack when app loads

查看:145
本文介绍了应用加载时将默认路由('/')添加到导航器堆栈中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(home: MyApp(),));

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: RaisedButton(
          onPressed: () => Navigator.pop(context),
          child: Text('Go Back'),
        ),
      ),
    );
  }
}

上面示例的屏幕截图:

我知道我可以使用<$ c $停止这种行为c> Navigator.canPop(context)。

我的简单问题是,当应用启动时只有一页时,默认路由为 / 添加到 Navigator 列表中?

My simple question is, When app launches with one page, will default route "/" add to the Navigator list?

如果默认路由被推送到 Navigator 堆栈(如上所示)图片)是否会在按返回(在上面的示例中)时从 Navigator 堆栈弹出?

If default route pushes to the Navigator stack(as shown in the above image) will it pop from the Navigator stack when press Go Back(in above example)?

推荐答案

阅读导航器类的文档


MaterialApp是最简单的方式进行设置。 MaterialApp的
房屋成为导航器堆栈底部的路线。启动应用程序后,您会看到

A MaterialApp is the simplest way to set things up. The MaterialApp's home becomes the route at the bottom of the Navigator's stack. It is what you see when the app is launched.

请阅读以下 app.dart文件源代码

/// The widget for the default route of the app ([Navigator.defaultRouteName],
  /// which is `/`).
  ///
  /// This is the route that is displayed first when the application is started
  /// normally, unless [initialRoute] is specified. It's also the route that's
  /// displayed if the [initialRoute] can't be displayed.
  ///
  /// To be able to directly call [Theme.of], [MediaQuery.of], etc, in the code
  /// that sets the [home] argument in the constructor, you can use a [Builder]
  /// widget to get a [BuildContext].
  ///
  /// If [home] is specified, then [routes] must not include an entry for `/`,
  /// as [home] takes its place.
  ///
  /// The [Navigator] is only built if routes are provided (either via [home],
  /// [routes], [onGenerateRoute], or [onUnknownRoute]); if they are not,
  /// [builder] must not be null.
  ///
  /// The difference between using [home] and using [builder] is that the [home]
  /// subtree is inserted into the application below a [Navigator] (and thus
  /// below an [Overlay], which [Navigator] uses). With [home], therefore,
  /// dialog boxes will work automatically, [Tooltip]s will work, the [routes]
  /// table will be used, and APIs such as [Navigator.push] and [Navigator.pop]
  /// will work as expected. In contrast, the widget returned from [builder] is
  /// inserted _above_ the [MaterialApp]'s [Navigator] (if any).
final Widget home;

是的,当在 home 中添加了小部件时属性,它将添加到 Navigator 堆栈中,这意味着当在该页面中调用 pop 时,将删除/弹出默认路由从 Navigator 堆栈中。

Yes, when added a widget to home property, that will add to the Navigator stack which means when call pop in that page, will remove/pop default route from the Navigator stack.

注意:根据此问题在github 中,请勿尝试 pop 初始路径/页面(默认路径)

Notice: according to this issues in github, do not try to pop initial route/page(default route).

这篇关于应用加载时将默认路由('/')添加到导航器堆栈中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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