gpt4 book ai didi

dart - 将在应用加载时将默认路由('/')添加到导航器堆栈中

转载 作者:行者123 更新时间:2023-12-03 02:53:28 27 4
gpt4 key购买 nike

例如:

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

上面示例的屏幕截图:

enter image description here

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

我的简单问题是,当应用程序以一页启动时,默认路由 "/"是否会添加到 Navigator列表中?

enter image description here

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

最佳答案

阅读doc for Navigator class:

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 file source code的内联注释:
/// 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堆栈中删除/弹出默认路由。

注意:根据 this issues in github,请勿尝试 pop初始路由/页面(默认路由)。

关于dart - 将在应用加载时将默认路由('/')添加到导航器堆栈中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50317066/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com