Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router outlet use first module children route to render #464

Closed
Jictyvoo opened this issue Jun 25, 2021 · 5 comments
Closed

Router outlet use first module children route to render #464

Jictyvoo opened this issue Jun 25, 2021 · 5 comments

Comments

@Jictyvoo
Copy link

Jictyvoo commented Jun 25, 2021

Hey guys!

I really like how Modular looks and works, congrats to you all that develop this awesome library.
But I have a question, and a request if possible, it has someway to define the RouterOutlet first child to render?
I'm currently developing an app that uses a BottomNavigationBar and a RouterOutlet as the Scaffold body (just like in the documentation), but when I enter in the Widget, my navigationBar appear, but the Scaffold body is empty. How I can fix that? (I already tried to add a guard, but I fall in a loop because I can't get the full path of the router (exemple below).

  • '/' => Route with RouterOutlet and BottomNavigationBar
    • '/home' => Route with children content to shows up in '/' RouterOutlet (it's another module)
    • '/another' => Another children content
    routes = [
          ChildRoute<StartPage>(
            '/',
            child: (_, args) => StartPage(),
            guards: [AuthGuard(), RedirectGuard()], // AuthGuard checks access token with guardedRoute = '/login'
            children: [
              // Routes in BottomNavigationBar
              // Almost every route should be a ModuleRoute, except MenuPage
              ModuleRoute(
                '/home',
                module: HomeModule(),
              ),
              ModuleRoute(
                '/another',
                module: AnotherModule(),
              ),
              ChildRoute(
                '/menu',
                child: (_, args) => MenuPage(),
              ),
            ],
          ),
          ChildRoute(
            '/login',
            child: (_, args) => LoginPage(),
          ),
    ];

One way that I think is if in ModularRoute we can set an index with default children to be rendered by RouterOutlet

@Jictyvoo
Copy link
Author

Jictyvoo commented Jun 26, 2021

I made a test with a RouteGuard (which I add in guards of the route '/'). The code is shown below

import 'package:flutter_modular/flutter_modular.dart';

class RedirectGuard implements RouteGuard {
  @override
  Future<bool> canActivate(String url, ModularRoute route) async {
    print(
      'URL: `$url` ---- Fragment: `${route.fragment}` ---- '
      'Path: `${route.path}` ----- RouterName: `${route.routerName}`'
      'ModulePath: `${route.modulePath}` ----- Uri: `${route.uri}`',
    );
    // Return `true`/`false` to allow/disallow access
    return Future.value(true);
  }

  @override
  String? get guardedRoute => '/home';
}

When my page redirects from '/login' to '/home', the home route is a child of '/' route, the output from the print is

 URL: `/` ---- Fragment: `` ---- Path: `/` ----- RouterName: `/`ModulePath: `/` ----- Uri: `/`

So, I don't see how I can get the current route

@lloss
Copy link

lloss commented Jul 3, 2021

@Jictyvoo You have to set initial route in the material / cupertino app class instance. But there is a problem, it does not solve the issue with guards, which described above.

@Jictyvoo
Copy link
Author

Jictyvoo commented Jul 4, 2021

@Jictyvoo You have to set initial route in the material / cupertino app class instance. But there is a problem, it does not solve the issue with guards, which described above.

Yes, this seems to solve the problem at first, thanks!
But yeah, I think that bring the possibility to do this using guards can be good also

@rahmanrezaee
Copy link

set init in app_widget.dart Modular.setInitialRoute('/chat');
image

@terov
Copy link

terov commented Nov 23, 2023

It doesn't solve the problem with multiple nested modules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants