0

I want bottom navigation to change if I press any button or link on other tab. For exampleenter image description here

If I am on this page and I click on "weekdays" list item, I go to this next page.

enter image description here

Now, my first page is in "Home" tab and my second page is in "Configuration" tab. How do I switch the tab while showing user this page? my origin page for "Configuration" tab is this next one.

enter image description here

My code for bottomNavigation bar is this.

index.dart

import 'package:flutter/material.dart';
import 'package:soneilcharging/Pages/settings/settings.dart';
import 'package:soneilcharging/tabNavigator.dart';

// functional widgets
import 'Pages/home.dart';
import 'Pages/configuration/configuration.dart';
import 'Pages/record/records.dart';
import 'Pages/scheduling/schedule.dart';

// welcome page

// navigation

class indexWidget extends StatefulWidget {
  const indexWidget({Key? key}) : super(key: key);

  @override
  State<indexWidget> createState() => _indexWidgetState();
}

class _indexWidgetState extends State<indexWidget> {
  int _selectedIndex = 0;
  String _currentPage = "Home";
  PageController pageController = PageController();
  List<String> pageKeys = ["Home", "Configuration", "History"];
  Map<String, GlobalKey<NavigatorState>> _navigatorKeys = {
    "Home": GlobalKey<NavigatorState>(),
    "Configuration": GlobalKey<NavigatorState>(),
    "History": GlobalKey<NavigatorState>(),
  };

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  void _onItemTapped(int index) {
    _selectTab(pageKeys[index], index);
    /* pageController.animateToPage(index,
        duration: Duration(milliseconds: 500), curve: Curves.ease); */
  }

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

  Widget _buildOffstageNavigator(String tabItem) {
    return Offstage(
      offstage: _currentPage != tabItem,
      child: TabNavigator(
        navigatorKey: _navigatorKeys[tabItem]!,
        tabItem: tabItem,
      ),
    );
  }

  void _selectTab(String tabItem, int index) {
    if (tabItem == _currentPage) {
      _navigatorKeys[tabItem]!.currentState!.popUntil((route) => route.isFirst);
    } else {
      setState(() {
        _currentPage = pageKeys[index];
        _selectedIndex = index;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        final isFirstRouteInCurrentTab =
            !await _navigatorKeys[_currentPage]!.currentState!.maybePop();
        if (isFirstRouteInCurrentTab) {
          if (_currentPage != "Home") {
            _selectTab("Home", 0);

            return false;
          }
        }
        // let system handle back button if we're on the first route
        return isFirstRouteInCurrentTab;
      },
      child: Scaffold(
        bottomNavigationBar: BottomNavigationBar(
          backgroundColor: Colors.black,
          items: const <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Icon(Icons.home),
              label: 'Home',
              backgroundColor: Colors.black,
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.schedule),
              label: 'Configuration',
              backgroundColor: Colors.black,
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.history),
              label: 'History',
              backgroundColor: Colors.black,
            ),
          ],
          currentIndex: _selectedIndex,
          unselectedItemColor: Colors.grey,
          onTap: _onItemTapped,
          type: BottomNavigationBarType.fixed,
          fixedColor: Colors.white,
          iconSize: 20,
        ),
        body: Stack(
          children: [
            _buildOffstageNavigator("Home"),
            _buildOffstageNavigator("Configuration"),
            _buildOffstageNavigator("History"),
          ],
        ),
      ),
    );
  }
}

tabNavigator.dart

import 'package:flutter/material.dart';
import 'package:soneilcharging/Pages/home.dart';

import 'Pages/record/records.dart';
import 'Pages/scheduling/schedule.dart';

class TabNavigatorRoutes {
  static const String root = '/';
  static const String detail = '/detail';
}

class TabNavigator extends StatelessWidget {
  final GlobalKey<NavigatorState> navigatorKey;
  final String tabItem;
  TabNavigator({required this.navigatorKey, required this.tabItem});

  @override
  Widget build(BuildContext context) {
    Widget? child;
    if (tabItem == "Home") {
      child = HomeWidget(onButtonPressed: () => {});
    } else if (tabItem == "Configuration") {
      child = const scheduleWidget();
    } else if (tabItem == "History") {
      child = const Records();
    }

    return Navigator(
      key: navigatorKey,
      onGenerateRoute: (routeSettings) {
        return MaterialPageRoute(builder: (context) => child!);
      },
    );
  }
}

And the way I am navigating from my settings page to schedule page is this:

Navigator.of(context).push(createRoute(departureWidget(
                                    pageMod: PageModeSched.Edit,
                                    editSetTimes: setTimes,
                                    editViewTimes: viewListTimes,
                                    editIndex: index,
                                  )))
                                  .then((value) => {
                                        isScheduleCharge = true,
                                        setState(
                                          () => {},
                                        )
                                      });

Any Idea how do I do this? I only want my schedule page to have single instance throughout the app. Currently, home tab creates its own version and configuration tab creates its own version.

Jeet Vyas
  • 31
  • 3

0 Answers0