skip to Main Content

I am using persistent_bottom_nav_bar package of flutter which showing 3 screens (Home screen, settings screen,profile screens). In the first screen I have a button that navigate user to another screen (mainscreen). The problem is i want to hide the navigation bar from showing in the profile screen.

Main.dart

void main() {
  runApp(const MyApp());
}

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return   const MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
     home: MainPage(),
    );
  }
}

The main screen

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

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  final PersistentTabController _controller =
      PersistentTabController(initialIndex: 0);


  List<Widget> _screens() {
    return [
      const HomeScreen(),
      const SettingsScreen(),
      const ProfileScreen(),
    ];
  }

  List<PersistentBottomNavBarItem> _navBarsItems() {
    return [
      PersistentBottomNavBarItem(
        icon: const Icon(Icons.home),
        inactiveIcon: const Icon(Icons.home_outlined),
        activeColorPrimary: CupertinoColors.activeBlue,
        inactiveColorPrimary: CupertinoColors.systemGrey,
      ),
      PersistentBottomNavBarItem(
        icon: const Icon(Icons.settings),
        activeColorPrimary: CupertinoColors.activeBlue,
        inactiveColorPrimary: CupertinoColors.systemGrey,
      ),
      PersistentBottomNavBarItem(
        icon: const Icon(Icons.person),
        inactiveIcon: const Icon(Icons.person_outline),
        activeColorPrimary: CupertinoColors.activeBlue,
        inactiveColorPrimary: CupertinoColors.systemGrey,
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PersistentTabView(
        context,
        navBarHeight: 60,
        padding: const NavBarPadding.only(top: 10),
        decoration: NavBarDecoration(
          boxShadow: [
            BoxShadow(
              color: Colors.grey.withOpacity(0.2),
              offset: const Offset(10, 5),
              spreadRadius: 7,
            ),
          ],
        ),
        controller: _controller,
        screens: _screens(),
        items: _navBarsItems(),
        navBarStyle: NavBarStyle.style6,
        stateManagement: true,
      ),
    );
  }
}

The Profile Screen

import 'package:flutter/material.dart';

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Profile Screen'),
      ),
    );
  }
}

2

Answers


  1. Chosen as BEST ANSWER

    In the PersistentTabView there is a function called onItemSelected

    return Scaffold(
          body: PersistentTabView(
            context,
            navBarHeight: 60, 
            hideNavigationBar: _controller.index == 2
            padding: const NavBarPadding.only(top: 10),
            decoration: NavBarDecoration(
              boxShadow: [
                BoxShadow(
                  color: Colors.grey.withOpacity(0.2),
                  offset: const Offset(10, 5),
                  spreadRadius: 7,
                ),
              ],
            ),
            controller: _controller,
            screens: _screens(),
            items: _navBarsItems(),
            navBarStyle: NavBarStyle.style6,
            stateManagement: true,
            onItemSelected: (index) {
              setState(() {
                _controller.index = index;
              });
            },
          ),
        );
    

    The bottom nav bar will be hidden when you click on the profile icon.


  2. You can programatically hide the navigation bar by using the hideNavigationBar argument of PersistentTabView. When set as true/false, the navigation bar will disappear/appear with an animation.

    For your usecase, ProfileScreen() is index 2 so we hideNavigationBar where the current index is 2 (_controller.index == 2). See below code sample:

    return Scaffold(
          body: PersistentTabView(
            context,
            navBarHeight: 60, 
            hideNavigationBar: _controller.index == 2
            padding: const NavBarPadding.only(top: 10),
            decoration: NavBarDecoration(
              boxShadow: [
                BoxShadow(
                  color: Colors.grey.withOpacity(0.2),
                  offset: const Offset(10, 5),
                  spreadRadius: 7,
                ),
              ],
            ),
            controller: _controller,
            screens: _screens(),
            items: _navBarsItems(),
            navBarStyle: NavBarStyle.style6,
            stateManagement: true,
          ),
        );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search