Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hot reload not working #13

Open
anandrikka opened this issue Sep 18, 2020 · 0 comments
Open

Hot reload not working #13

anandrikka opened this issue Sep 18, 2020 · 0 comments

Comments

@anandrikka
Copy link

Hi,
Thanks for the amazing work. I encountered a problem, where hot reload not working for the screens inside the navigator. Here is my code

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  final Map<TabItem, Map<String, Object>> _navItems = {
    TabItem.CALCULATOR: {
      'icon': FlutterIcons.calc,
      'title': 'Calculator',
      'pageTitle': 'Stock Calculator',
      'key': GlobalKey<NavigatorState>()
    },
    TabItem.ACCOUNTS: {
      'icon': Icons.insert_chart,
      'title': 'Accounts',
      'pageTitle': 'Accounts',
      'key': GlobalKey<NavigatorState>()
    },
    TabItem.SETTINGS: {
      'icon': Icons.settings,
      'title': 'Settings',
      'pageTitle': 'Settings',
      'key': GlobalKey<NavigatorState>()
    },
  };

  TabItem _currentTab = TabItem.CALCULATOR;

  Widget _renderPage() {
    Map<String, Object> item = _navItems[_currentTab];
    var key = item['key'] as GlobalKey<NavigatorState>;
    var pageTitle = item['pageTitle'] as String;
    switch (_currentTab) {
      case TabItem.CALCULATOR:
        return HomePage(
          key: key,
          title: pageTitle,
        );
        break;
      case TabItem.ACCOUNTS:
        return AccountsPage(
          key: key,
          title: pageTitle,
        );
        break;
      case TabItem.SETTINGS:
        return SettingsPageNavigator(
          navigatorKey: key,
          tabItem: TabItem.SETTINGS,
        );
        break;
      default:
        return HomePage(
          key: key,
          title: pageTitle,
        );
    }
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        bool flag = true;
        try {
          if (TabItem.SETTINGS == _currentTab) {
            final isFirstRouteInCurrentTab = await (_navItems[_currentTab]
                    ['key'] as GlobalKey<NavigatorState>)
                .currentState
                .maybePop();
            if (isFirstRouteInCurrentTab) {
              _onTabSelect(TabItem.SETTINGS);
              flag = false;
            }
          }
        } catch (e) {}
        return flag;
      },
      child: Scaffold(
        backgroundColor: Colors.red,
        body: _renderPage(),
        bottomNavigationBar: BottomNavigationBar(
          backgroundColor: Theme.of(context).primaryColor,
          unselectedItemColor: Colors.white54,
          selectedItemColor: Colors.white,
          onTap: (index) => _onTabSelect(TabItem.values[index]),
          currentIndex: _currentTab.index,
          elevation: 5,
          type: BottomNavigationBarType.fixed,
          items: [
            _buildNavItem(tabItem: TabItem.CALCULATOR),
            _buildNavItem(tabItem: TabItem.ACCOUNTS),
            _buildNavItem(tabItem: TabItem.SETTINGS),
          ],
        ),
      ),
    );
  }

  _onTabSelect(TabItem tab) {
    setState(() {
      _currentTab = tab;
    });
  }

  BottomNavigationBarItem _buildNavItem({TabItem tabItem}) {
    return BottomNavigationBarItem(
        icon: Icon(_navItems[tabItem]['icon']),
        title: Text(_navItems[tabItem]['title']));
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant