Home > Net >  How to pass named routes of main to BottomNavigationBar in other class flutter?
How to pass named routes of main to BottomNavigationBar in other class flutter?

Time:12-02

My main.dart screen calls SplashScreen and then SplashScreen calls BottomNavigationScreen. I am manually calling all the screens for _widgetOptions.elementAt(_selectedIndex) in widgetOptions.

I want not to call all screens manually. Instead I want to call screens by using named routes defined in my main.dart file. How to pass named routes to my another class where I am defining my BottomNavigatioBar?

How to add BottomNavigationBar in class other than main and call the named routes instead of manually calling classes?

main.dart

import 'package:flutter/material.dart';
import 'package:form_data_api/screens/all_articles_screen.dart';
import 'package:form_data_api/screens/article_detail_screen.dart';
import 'package:form_data_api/screens/user_profile_screen.dart';

// import 'package:form_data_api/screens/all_children_screen.dart';
// import 'package:form_data_api/screens/child_detail_screen.dart';
// import 'package:form_data_api/screens/user_profile_screen.dart';

import 'screens/all_children_screen.dart';
import 'screens/bottom_navigation_screen.dart';
import 'screens/child_detail_screen.dart';
import 'splash_screen.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: '/splashScreen',
      routes: {
        // When navigating to the "/" route, build the FirstScreen widget.
        '/splashScreen': (_) =>  const SplashScreen(),
        // When navigating to the "/second" route, build the SecondScreen widget.
        '/bottomNavigationScreen': (_) =>  const BottomNavigationScreen(),
        '/userProfileScreen': (_) =>  const UserProfileScreen(),
        '/allChildrenScreen': (_) => const AllChildrenScreen(),
        '/childDetailScreen': (context) => const ChildDetailScreen(childName: '', childId: '',),
        '/allArticleScreen': (context) => const AllArticleScreen(),
        '/articleDetailScreen': (context) => const ArticleDetailScreen(articleName: '', articleId: '',),
      },
      // home: SplashScreen(),
    );
  }
}

bottom_navigation_screen.dart

import 'package:flutter/material.dart';
import 'all_articles_screen.dart';
import 'all_children_screen.dart';
import 'user_profile_screen.dart';


class BottomNavigationScreen extends StatefulWidget {
  const BottomNavigationScreen({super.key});

  @override
  State<BottomNavigationScreen> createState() => _BottomNavigationScreenState();
}

class _BottomNavigationScreenState extends State<BottomNavigationScreen> {
  int _selectedIndex = 0;
  // static const TextStyle optionStyle =
  // TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  static const List<Widget> _widgetOptions = <Widget>[
    UserProfileScreen(),
    AllChildrenScreen(),
    AllArticleScreen(),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        index: _selectedIndex,
        children: _widgetOptions,
      ),
      // body: Center(
      //   child: _widgetOptions.elementAt(_selectedIndex),
      // ),
      bottomNavigationBar: BottomNavigationBar(
        backgroundColor: Colors.blue[50],
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: 'Profile',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.business),
            label: 'Children',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.article),
            label: 'Articles',
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.blue,
        onTap: _onItemTapped,
      ),
    );
  }
}

My main.dart screen calls SplashScreen and then SplashScreen calls BottomNavigationScreen. I am manually calling all the screens for _widgetOptions.elementAt(_selectedIndex) in widgetOptions.

I want not to call all screens manually. Instead I want to call screens by using named routes defined in my main.dart file. How to pass named routes to my another class where I am defining my BottomNavigatioBar?

CodePudding user response:

use this

onTap: (index){
  switch(index){
      case 0:
        Navigator.pushNamed(context, "/userProfileScreen");
        break;
      case 1:
        Navigator.pushNamed(context, "/allChildrenScreen");
        break;
      case 2:
        Navigator.pushNamed(context, "/childDetailScreen");
        break;
      case 3:
        Navigator.pushNamed(context, "/allArticleScreen");
        break;

        ...etc
    }
 },

CodePudding user response:

Hello Brother How Are You? Is Very Easy You Should Create This Class Class

This Is Your Material App

return MaterialApp(
  initialRoute: '/splashScreen',
  routes: {
    // When navigating to the "/" route, build the FirstScreen widget.
    AppRoute.splashScreen: (_) =>  const SplashScreen(),
    // When navigating to the "/second" route, build the SecondScreen widget.
    AppRoute.bottomNavigationScreen: (_) =>  const 
    BottomNavigationScreen(),
    .......
  },
  // home: SplashScreen(),
);

And This AppRoute Class You Should Make

Class AppRoute{
static const String splashScreen = '/splash-screen';
static const String bottomNavigationScreen = '/bottom-navigation-bar-screen';
....
}

After That When You Want Call The Page;

Navigator.pushNamed(context, AppRoute.splashScreen);

I Will It Help You

  • Related