arbazadam / navigation-poc

0 stars 0 forks source link

Problem while coming back to the main route on tapping the bottom navbar item #1

Open arbazadam opened 2 years ago

arbazadam commented 2 years ago

Whenever the user presses the bottom nav item, it shall check if its the first route on the tab on the navigation stack or not. If it is then the user shall be taken to the first route nested-nav-issue .

SEGVeenstra commented 2 years ago

Hi! As I've mentioned on Twitter, this is not a setup I'm very familiar with. I probably would be doing it something like this:

(You can copy-past it as the main)

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:nav_poc/bloc/home_cubit.dart';
import 'package:nav_poc/pages/favorites_page.dart';
import 'package:nav_poc/pages/search_page.dart';
import 'package:nav_poc/pages/settings_page.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (_) => HomeCubit(),
      child: const MaterialApp(
        home: MainContent(),
      ),
    );
  }
}

class MainContent extends StatelessWidget {
  const MainContent({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<HomeCubit, OPage>(
      builder: (context, state) {
        return Scaffold(
          body: {
            OPage.search: const SearchPage(),
            OPage.favorites: const FavoritesPage(),
            OPage.settings: const SettingsPage()
          }[state]!,
          bottomNavigationBar: BottomNavigationBar(
            currentIndex: {
              OPage.search: 0,
              OPage.favorites: 1,
              OPage.settings: 2
            }[state]!,
            onTap: (index) => context
                .read<HomeCubit>()
                .goTo([OPage.search, OPage.favorites, OPage.settings][index]),
            items: const [
              BottomNavigationBarItem(
                  icon: Icon(Icons.search), label: 'Search'),
              BottomNavigationBarItem(
                  icon: Icon(Icons.favorite), label: 'Favorites'),
              BottomNavigationBarItem(
                  icon: Icon(Icons.settings), label: 'Settings'),
            ],
          ),
        );
      },
    );
  }