How to have a BottomNavigationBar show on more than just the main screens, but not all screens?

I now observe the routing in main.dart and handle the rest of the navigation bar logic in a file I call NavigationConduit.

  • The navigation bar shows 5 main screens.
  • The bar should also remain visible on some other screens (such as settings, accessed from a main screen).
  • But there are some screens where it should not be visible (such as change password screen).

I have a feeling the RouteObserver is not working properly, already not on the main screens… What am I doing wrong in this approach?

Is there a better way, without having to put code in every screen’s initState and dispose?

This is my main.dart, reduced to only show related code:

import 'package:flutter/material.dart';
import 'package:app/navigation_conduit.dart';
import 'package:app/screens/profile/settings_1_overview.dart';
import 'package:app/screens/auth/change_password_1_current.dart';

final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'App',
      navigatorObservers: [routeObserver],
      home: const NavigationConduit(),
      routes: {
        '/screens/profile/settings_1_overview': (context) => const SettingsOverview(),
        '/screens/auth/change_password_1_current': (context) => const ChangePasswordScreen(),
      },
    );
  }
}

And then this is my navigation_conduit.dart:

import 'package:flutter/material.dart';
import 'package:app/screens/home/home_screen.dart';
import 'package:app/screens/overview/overview.dart';
import 'package:app/screens/scanner/scanner.dart';
import 'package:app/screens/favorites/favorites_overview.dart';
import 'package:app/screens/profile/user_profile.dart';
import 'package:app/screens/profile/settings_1_overview.dart';
import 'package:app/screens/auth/change_password_1_current.dart';

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

  @override
  _NavigationConduitState createState() => _NavigationConduitState();
}

class _NavigationConduitState extends State<NavigationConduit> with RouteAware {
  int _selectedIndex = 0;
  bool _showBottomNavBar = true;

  final List<Widget> _screens = [
    const HomeScreen(),
    const Overview(),
    const Scanner(),
    const FavoritesOverview(),
    const UserProfile(),
  ];

  void _onItemTapped(int index) {
    if (_selectedIndex == index) {
      Navigator.of(context).popUntil((route) => route.isFirst);
    } else {
      setState(() {
        _selectedIndex = index;
        _showBottomNavBar = true;
      });
    }
  }


  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    final ModalRoute? route = ModalRoute.of(context);
    setState(() {
      _showBottomNavBar = _shouldShowBottomNavBar(route);
    });
  }


  bool _shouldShowBottomNavBar(Route<dynamic>? route) {
    if (route == null) return true;
    final currentRouteName = route.settings.name ?? '';
    print('Checking route: $currentRouteName');
    return ![
      '/screens/profile/settings_6_info',
      '/screens/auth/change_password_1_current',
    ].contains(currentRouteName);
  }


  @override
  void didPush() {
    super.didPush();
    setState(() {
      _showBottomNavBar = _shouldShowBottomNavBar(ModalRoute.of(context));
    });
  }


  @override
  void didPopNext() {
    super.didPopNext();
    setState(() {
      _showBottomNavBar = _shouldShowBottomNavBar(ModalRoute.of(context));
    });
  }


  @override
  void dispose() {
    super.dispose();
  }


  String getIconAsset(int index, bool isSelected) {}
  // This gets all the images connected to the right icons


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _screens[_selectedIndex],
      bottomNavigationBar: _showBottomNavBar
          ? Container(
        decoration: const BoxDecoration(
          border: Border(
            top: BorderSide(color: Color(0xFFA4A4A4), width: 1.0),
          ),
          color: Colors.white,
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: List.generate(5, (index) {
            bool isSelected = _selectedIndex == index;
            return IconButton(
              icon: Image.asset(
                getIconAsset(index, isSelected),
                width: 24.0,
                height: 24.0,
              ),
              onPressed: () {
                _onItemTapped(index);
              },
            );
          }),
        ),
      )
          : null,
    );
  }
}

New contributor

MarcoL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật