0

I am creating a nested naivagtion for the registration usecase.there is a common appbar and options are there.options differs from page to page.I could able to acheive this by using ShellRoute.if I'm using the router.pushNamed() , I am not able to get the different paths from the state.matchedLocation object in pageBuilder: (context, state, child).it returns always the initial path. if I'm using router.goNamed , I can get respective route path from the state.matchedLocation and the pop to previous screen not working.

I need to go to previous screen when backpressed. below given is my router class.

 GoRouter(
  navigatorKey: _rootNavigatorKey,
  initialLocation: '/',
  //debugLogDiagnostics: true,
  routes: [
    GoRoute(
      name: splashView,
      path: '/',
      pageBuilder: (context, state) => const MaterialPage(
        child: SplashScreen(),
      ),
    ),
     ShellRoute(
       navigatorKey: _shellNavigatorKey,
       //parentNavigatorKey: _rootNavigatorKey,
       pageBuilder: (context, state, child) {
         OnBoardViewType type = getOnBoardViewType(state);
         print('type=====${type.name}');
         return MaterialPage(
           child: OnBoardParentView(
             key: state.pageKey,
             type: type,
             child: child,
           ),
         );
       },
       routes: [
         GoRoute(
           parentNavigatorKey: _shellNavigatorKey,
           name: onBoardWelcome,
           path: '/onBoard',
           pageBuilder: (context, state) => const MaterialPage(
             child: GetStartedView(),
           ),
         ),
         GoRoute(
           parentNavigatorKey: _shellNavigatorKey,
           name: walletSetup,
           path: '/onBoard/1',
           pageBuilder: (context, state) => const MaterialPage(
             child: Page1(),
           ),
         ),
         GoRoute(
           parentNavigatorKey: _shellNavigatorKey,
           name: importFromSeed,
           path: '/onBoard/2',
           pageBuilder: (context, state) => const MaterialPage(
             child:  Page2(),
           ),
         ),

common parent page as follows.

    getOnBoardViewType(GoRouterState state) {
  print('state===${state.matchedLocation}');
  print('name===${state.path}');
  late OnBoardViewType type;
  switch (state.matchedLocation) {
    case '/onBoard':
      type = OnBoardViewType.getStarted;
      break;
    case '/onBoard/1':
      type = OnBoardViewType.page1;
      break;
    case '/onBoard/2':
      type = OnBoardViewType.page2;
      break;
}
class OnBoardParentView extends StatelessWidget {
  final Widget child;
  final OnBoardViewType type;

  const OnBoardParentView({super.key, required this.child, required this.type});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: AppStyles.imageDecoration,
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: AppBar(
          backgroundColor: Colors.transparent,
          leading: _backButton(context),
          leadingWidth: 17.w,
          title: _title(),
        ),
        body: child,
      ),
    );
  }
}
dev
  • 73
  • 3
  • 11

0 Answers0