skip to Main Content

In my flutter project I have a bottomnavigation bar that switches between different pages. When I try to add the same page function to an OnPressed button it only shows the text in default. To get more idea here is the main.dart that links to bottombar:

  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      routes: {
        '/': (context) => _defaultHome,
        '/home': (context) => const BottomBar(),
      },
    );
  }

Here is the bottom bar:

class BottomBar extends StatefulWidget {
  const BottomBar({Key? key}) : super(key: key);
 
  @override
  State<BottomBar> createState() => _BottomBarState();
}
 
class _BottomBarState extends State<BottomBar> {
  int _selectedIndex = 0;
  static final List<Widget> _widgetOptions = <Widget>[
    HomeScreen(),
.............................
    const User_Profile()
  ];
 
  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
      //print('Tapped index is: ${_selectedIndex}');
    });  }
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: _widgetOptions[_selectedIndex],
      ),
      bottomNavigationBar: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          BottomNavigationBar(
            currentIndex: _selectedIndex,
            onTap: _onItemTapped,
......................................
            type: BottomNavigationBarType.fixed,
            unselectedItemColor: const Color(0xFF526480),
            items: const [
................................
              BottomNavigationBarItem(
                  icon: Icon(FluentSystemIcons.ic_fluent_person_regular),
                  activeIcon: Icon(FluentSystemIcons.ic_fluent_person_filled),
                  label: "Profile")
            ],          ),        ],      ),    );  }

Now the bottombar is working perfectly fine but when I try to add User_Profile() in a homescreen button the outcome is not showing the same way as when clicking it from the bottonbar button.
Here is the homescreen button:

GFButton(
                                          onPressed: () {
                                            Navigator.push(
                                                context,
                                                MaterialPageRoute(
                                                    builder: (context) =>
                                                        const User_Profile()));
                                          },
                                          text: snapshot.data![index].name,
                                          blockButton: true,
                                        )

Here is the outcome when clicking on User_Profile() from bottombar.

Here is the outcome when clicking from homescreen.

My question why is the outcome different when the same page is clicked from the home screen than the bottom bar. My required outcome is to be the same as thee bottombar.

enter image description here

enter image description here

enter image description here

2

Answers


  1. I think the you need to make your profile page material aware. So make sure your parent widget in the profile page returns something like a Scaffold()

    ...
     return Scaffold();
    
    Login or Signup to reply.
  2. Use IndexStack widget it may help you to solve this issue.

                   IndexedStack(
                      index: _selectedIndex,
                      children: [
                        DashboardFragment(dashboardModel: dashboardModel),
                        user?.isAuthenticated == true
                            ? BlocProvider(
                                create: (_) => ProfileCubit(),
                                child: CustomerScreenFragment(),
                              )
                            : LoginFragment(),
                        CollectionsFragment(),
                        MoreFragment(),
                      ],
                    ),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search