Blasanka
Blasanka

Reputation: 22437

Will default route('/') add to the Navigator Stack when app loads

As a example:

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

Screenshot of the above example:

enter image description here

I know that I can stop that behaviour using Navigator.canPop(context).

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

enter image description here

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)?

Upvotes: 2

Views: 5885

Answers (1)

Blasanka
Blasanka

Reputation: 22437

Read the 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.

Read below inline comments of 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;

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.

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

Upvotes: 3

Related Questions