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']));
}
}