5

I currently have a MaterialApp in my flutter application which makes the use of the Navigator extremely easy, which is great. But, I'm now trying to figure out how to create more navigators for particular views/widgets. For example I've got a custom tab bar with another widget/view in. I'd now like that widget/view to have it's own navigation stack. So the goal is to keep the tab bar at the top while I navigate to other pages from within my widget/view.
This question is almost exactly this: Permanent view with navigation bar in Flutter but in that code, there is no MaterialApp yet. Nesting MaterialApps give funky results and I don't believe that that would be a solution.
enter image description here

Any ideas?

2 Answers 2

8

You can create new Navigator for each page. As a reference check CupertinoTabView

Or simple example:

import 'package:flutter/material.dart';

class Home extends StatelessWidget {
  Navigator _getNavigator(BuildContext context) {
    return new Navigator(
      onGenerateRoute: (RouteSettings settings) {
        return new MaterialPageRoute(builder: (context) {
          return new Center(
            child: new Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                new Text(settings.name),
                new FlatButton(
                  onPressed: () =>
                      Navigator.pushNamed(context, "${settings.name}/next"),
                  child: new Text('Next'),
                ),
                new FlatButton(
                  onPressed: () =>
                      Navigator.pop(context),
                  child: new Text('Back'),
                ),
              ],
            ),
          );
        });
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Column(
        children: <Widget>[
          new Expanded(
            child: _getNavigator(context),
          ),
          new Expanded(
            child: _getNavigator(context),
          ),
        ],
      ),
    );
  }
}

void main() {
  runApp(new MaterialApp(
    home: new Home(),
  ));
}

Screenshot

Sign up to request clarification or add additional context in comments.

9 Comments

I actually know about the TabView but that's not what I meant. I'd actually like to have a navigation stack for each tab page in my tabbar, any idea how that can be accomplished?
One more question, when I'm in my second level navigator (So the one not created by my MaterialApp) how can I make the back button work? It just closes the app for me :/
@BramVanbilsen, if it's your own button, check what Navigator you use. You should take the "second" one, not "main". If it's scaffold's button, make sure scaffold is placed in scope of "second" Navigator.
@Mogol Woops, should have mentioned that I was talking about the hardware Android back button. Any idea about that?
|
0

you could give first material app's context to secondary material app;
and in secondary material app ,when you need to go back first material app,
you could check whether the secondary app can pop, with this line:

 Navigator.of(secondaryContext).canPop()

if true, then you could keep using,else use first material app's context,
like this:

Navigator.of(parentContext).pop();

otherwise

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.