Skip to content

Instantly share code, notes, and snippets.

@bparol
Created May 23, 2021 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bparol/2ebaf11af962115dc598294bf136cb8d to your computer and use it in GitHub Desktop.
Save bparol/2ebaf11af962115dc598294bf136cb8d to your computer and use it in GitHub Desktop.
class TabBarWidget extends StatefulWidget {
@override
_TabBarWidgetState createState() => _TabBarWidgetState();
}
class _TabBarWidgetState extends State<TabBarWidget> {
List<Widget> _widgets = [
DashboardPage(),
SettingsWidget()
];
static final titles = ['Dashboard', 'Settings'];
PersistentTabController _controller;
@override
void initState() {
super.initState();
_controller = PersistentTabController(initialIndex: 0);
}
@override
Widget build(BuildContext context) {
var selectedColor = context.color(
forLightMode: ThemeColors.lightTabBarContentActiveColor,
forDarkMode: ThemeColors.darkTabBarContentActiveColor
);
var unselectedColor = context.color(
forLightMode: ThemeColors.lightTabBarContentInactiveColor,
forDarkMode: ThemeColors.darkTabBarContentInactiveColor
);
var backgroundColor = context.color(
forLightMode: ThemeColors.lightTabBarBackgroundColor,
forDarkMode: ThemeColors.darkTabBarBackgroundColor
);
var borderColor = context.color(
forLightMode: ThemeColors.lightTabBarBorderColor,
forDarkMode: Color(0xFF3333330)//ThemeColors.darkTabBarBorderColor
);
var border = Border(
top: BorderSide(
color: borderColor,
)
);
Widget widget;
if (context.isAndroid()) {
widget = PersistentTabView(
context,
controller: _controller,
screens: _widgets,
items: [
PersistentBottomNavBarItem(
icon: Icon(Icons.dashboard),
title: titles[0],
activeColorPrimary: selectedColor,
inactiveColorPrimary: unselectedColor,
routeAndNavigatorSettings: RouteAndNavigatorSettings(onGenerateRoute: getRoutes)
),
PersistentBottomNavBarItem(
icon: Icon(Icons.settings),
title: titles[1],
activeColorPrimary: selectedColor,
inactiveColorPrimary: unselectedColor,
routeAndNavigatorSettings: RouteAndNavigatorSettings(onGenerateRoute: getRoutes)
)
],
confineInSafeArea: true,
backgroundColor: Theme.of(context).backgroundColor,
handleAndroidBackButtonPress: true, // Default is true.
resizeToAvoidBottomInset: true, // This needs to be true if you want to move up the screen when keyboard appears. Default is true.
stateManagement: true, // Default is true.
hideNavigationBarWhenKeyboardShows: true, // Recommended to set 'resizeToAvoidBottomInset' as true while using this argument. Default is true.
decoration: NavBarDecoration(
colorBehindNavBar: Colors.white,
border: border
),
popAllScreensOnTapOfSelectedTab: true,
popActionScreens: PopActionScreensType.all,
itemAnimationProperties: ItemAnimationProperties( // Navigation Bar's items animation properties.
duration: Duration(milliseconds: 200),
curve: Curves.ease,
),
navBarStyle: NavBarStyle.style8, // Choose the nav bar style with this property.
);
} else {
widget = CupertinoTabScaffold(
tabBar: CupertinoTabBar(
activeColor: selectedColor,
inactiveColor: unselectedColor,
backgroundColor: backgroundColor,
items: [
BottomNavigationBarItem(
label: titles[0],
icon: Icon(Icons.dashboard),
),
BottomNavigationBarItem(
label: titles[1],
icon: Icon(context.platformIcons.settings),
),
]
),
tabBuilder: (context, index) => _getWidget(index)
);
}
return WillPopScope (
onWillPop: () async => false,
child: widget,
);
}
Widget _getWidget(int index) {
return _widgets[index];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment