navigation bar issue after clicking on card flutter - flutter

[![When I click on any card the navigation bar doesn't work anymore ][2]][2]
the 1st image shows my navigation bar which works very well. The moment I click on a card the navigation bar doesn't work anymore and I didn't find out what is the reason
**This is my navigation bar Widget code that I'm using after clicking on the card **
class MenuBarWidget extends StatefulWidget {
#override
_State createState() => _State();
}
class _State extends State<MenuBarWidget> {
int _index = 0;
#override
Widget build(BuildContext context) {
return Container(
height: 70,
decoration: BoxDecoration(
gradient: AppGradients.linear,
),
child: BottomNavigationBar(
onTap: (newIndex) => setState(() => _index = newIndex),
currentIndex: _index,
unselectedItemColor: Colors.white70,
selectedItemColor: Colors.white,
elevation: 0,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home_outlined),
label: 'Principal',
backgroundColor: Colors.transparent,
),
BottomNavigationBarItem(
icon: Icon(Icons.assignment_outlined),
label: 'Pedidos',
backgroundColor: Colors.transparent,
),
BottomNavigationBarItem(
icon: Icon(Icons.chat_outlined),
label: 'Chat',
backgroundColor: Colors.transparent,
),
BottomNavigationBarItem(
icon: Icon(Icons.person_outline),
label: 'Perfil',
backgroundColor: Colors.transparent,
),
],
),
);
}
}
this is my code after clicking on the card
#override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: MenuBarWidget(),
appBar: PreferredSize(
preferredSize: Size.fromHeight(100),
child: Stack(children: [
Container(
height: 128,
decoration: BoxDecoration(
color: AppColors.green,
border: Border(
bottom: BorderSide(
color: AppColors.blueButton,
width: 2,
),
),
image: DecorationImage(
image: AssetImage(AppImages.dots),
fit: BoxFit.cover,
),
),
),
Align(
alignment: Alignment(-0.7, -0.3),
child: RichText(
text: TextSpan(
text: serviceName,
style: TextStyle(
color: AppColors.white,
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
),
),
Align(
alignment: Alignment(-0.6, 0.4),
child: RichText(
text: TextSpan(
text: 'Confira abaixo todos os profissionais desta categoria',
style: TextStyle(
color: AppColors.white,
fontSize: 9,
fontWeight: FontWeight.bold,
),
),
),
),
]),
),
**Any help is appreciated thanks **

create a new list of screens and add the screens you created as the list items
List screens = [
HomeScreen();
CarpentryScreen();
...
...
];
then make the body of your MenuBarWidget() return screens[_index]
class MenuBarWidget extends StatefulWidget {
#override
_State createState() => _State();
}
class _State extends State<MenuBarWidget> {
int _index = 0;
// new list of your screens
List<Widget> screens = [
PrincipalScreen(),
PedidosScreen(),
Chatscreen(),
Perfilscreen(),
];
#override
Widget build(BuildContext context) {
// return the screens
return Scaffold(
body: screens[_index],
bottomNavigationBar: BottomNavigationBar(
onTap: (newIndex) => setState(() => _index = newIndex),
currentIndex: _index,
unselectedItemColor: Colors.white70,
selectedItemColor: Colors.white,
elevation: 0,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home_outlined),
label: 'Principal',
backgroundColor: Colors.transparent,
),
BottomNavigationBarItem(
icon: Icon(Icons.assignment_outlined),
label: 'Pedidos',
backgroundColor: Colors.transparent,
),
BottomNavigationBarItem(
icon: Icon(Icons.chat_outlined),
label: 'Chat',
backgroundColor: Colors.transparent,
),
BottomNavigationBarItem(
icon: Icon(Icons.person_outline),
label: 'Perfil',
backgroundColor: Colors.transparent,
),
],
),
);
}
}
then you don't have to add bottomNavigationBar to the screens individually, this way, the screen is persistent across all screens

Related

On bottom navigation bar when clicking on icons it is not navigating to other page

This is my code for my homepage but my navigation is not working when I am tapping on my bottom navigation bar Icons
Please help me with this problem
All other things in the code is working properly except bottom navigation bar
import 'package:app_first/pages/Settings.dart';
import 'package:app_first/pages/about_us.dart';
import 'package:app_first/pages/contact_us.dart';
import 'package:app_first/pages/our_services.dart';
import 'package:app_first/utilities/route.dart';
import 'package:app_first/widgets/bottom_navigation.dart';
import 'package:app_first/widgets/drawer.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttericon/font_awesome5_icons.dart';
import 'package:eva_icons_flutter/eva_icons_flutter.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _currentIndex = 0;
final List<Widget> _children = [
MyHomePage(),
Ourservices(),
Aboutus(),
Contactus(),
SettingPage(),
];
void onTappedBar(int index)
{
setState(() {
_currentIndex = index;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title:Center(
child: Text("FOODIES#CU"),
),
),
body: SingleChildScrollView(
child: Column(
children: [
SizedBox(height: 40, width: double.infinity,),
Image.asset(
"assets/images/background.jpg",
fit: BoxFit.cover,
width: 500,
),
SizedBox(height: 40,),
Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(width: 2,color: Colors.orangeAccent,),
)
),
child: Text(
"TOP RATED SHOPS",
style: TextStyle(
height: 2,
fontSize: 20,
color: Colors.deepOrangeAccent,
fontWeight: FontWeight.bold,
// backgroundColor: Colors.black,
),
),
),
SizedBox(height: 35,),
Container
(decoration: BoxDecoration(
border: Border.all(width: 4),
),
child:
Image.asset("assets/images/fr.jpg", fit: BoxFit.cover, width: 300, height: 200,),
),
SizedBox(height: 20,),
GestureDetector(
onTap: (){
Navigator.pushNamed(context, MyRoute.loginroute);
},
child: Container(
child: Text("FOOD REPUBLIC" , style:
TextStyle(
color: Colors.brown,
fontSize: 20,
),),
),
),
SizedBox(height: 35,),
Container
(decoration: BoxDecoration(
border: Border.all(width: 4),
),
child:
Image.asset("assets/images/fr.jpg", fit: BoxFit.cover, width: 300, height: 200,),
),
SizedBox(height: 20,),
GestureDetector(
onTap: (){
Navigator.pushNamed(context, MyRoute.loginroute);
},
child: Container(
child: Text("FOOD REPUBLIC" , style:
TextStyle(
color: Colors.brown,
fontSize: 20,
),),
),
)
],
),
),
bottomNavigationBar: BottomNavigationBar(
onTap: onTappedBar,
currentIndex: _currentIndex,
fixedColor: Colors.black,
elevation: 100,
items: [
BottomNavigationBarItem(icon: Icon(EvaIcons.home, color: Colors.indigo,),
label: 'Home',
backgroundColor: Colors.white70
),
BottomNavigationBarItem(icon: Icon(EvaIcons.shoppingCart, color: Colors.indigo,),
label: 'Our services',
backgroundColor: Colors.white70
),
BottomNavigationBarItem(icon: Icon(EvaIcons.bookOpen, color: Colors.indigo,),
label: 'About us',
backgroundColor: Colors.white70
),
BottomNavigationBarItem(icon: Icon(EvaIcons.phoneCall, color: Colors.indigo,),
label: 'Contact us',
backgroundColor: Colors.white70
),
BottomNavigationBarItem(icon: Icon(EvaIcons.settings, color: Colors.indigo,),
label: 'Settings',
backgroundColor: Colors.white70
),
]
),
drawer: MyDrawer(),
);
}
}
On bottom navigation bar I am using onTapped for navigating please look into it
And help me with this please!
I am getting this error After adding body:_children[_currentindex];
I am getting this error please help me to resolve it
You are not specifying the page in your body
Change your body in Scaffold to
body: _children[_currentIndex];
Final Code:
import 'package:app_first/pages/Settings.dart';
import 'package:app_first/pages/about_us.dart';
import 'package:app_first/pages/contact_us.dart';
import 'package:app_first/pages/our_services.dart';
import 'package:app_first/utilities/route.dart';
import 'package:app_first/widgets/bottom_navigation.dart';
import 'package:app_first/widgets/drawer.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttericon/font_awesome5_icons.dart';
import 'package:eva_icons_flutter/eva_icons_flutter.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _currentIndex = 0;
final List<Widget> _children = [
MyHomePage(),
Ourservices(),
Aboutus(),
Contactus(),
SettingPage(),
];
void onTappedBar(int index)
{
setState(() {
_currentIndex = index;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title:Center(
child: Text("FOODIES#CU"),
),
),
body: _children[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
onTap: onTappedBar,
currentIndex: _currentIndex,
fixedColor: Colors.black,
elevation: 100,
items: [
BottomNavigationBarItem(icon: Icon(EvaIcons.home, color: Colors.indigo,),
label: 'Home',
backgroundColor: Colors.white70
),
BottomNavigationBarItem(icon: Icon(EvaIcons.shoppingCart, color: Colors.indigo,),
label: 'Our services',
backgroundColor: Colors.white70
),
BottomNavigationBarItem(icon: Icon(EvaIcons.bookOpen, color: Colors.indigo,),
label: 'About us',
backgroundColor: Colors.white70
),
BottomNavigationBarItem(icon: Icon(EvaIcons.phoneCall, color: Colors.indigo,),
label: 'Contact us',
backgroundColor: Colors.white70
),
BottomNavigationBarItem(icon: Icon(EvaIcons.settings, color: Colors.indigo,),
label: 'Settings',
backgroundColor: Colors.white70
),
]
),
drawer: MyDrawer(),
);
}
}

How to put one image and text in one screen and leave the other screens with icons

I'm learning Flutter now and I'm not very handy yet. I have this problem:
basically I have created more screens and I can navigate through them thanks to a "bottomNavigationBar". Now the problem is I just don't know how to put an image and text on the Home screen but at the same time leave the icons on the other screens. I'm doing a project and now I need to do this thing.
I also need to know how to adjust this image (https://images.unsplash.com/photo-1604357209793-fca5dca89f97?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8bWFwfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=500&q=60") to the screen so that it stays between the AppBar and the bottomNavigationBar. (I now how to add images, I only need to know can it be fitted in the way I want).
(I leave here all the code that I written until now).
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
static const String _title = 'Start Pages';
#override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: StartPages(),
);
}
}
class StartPages extends StatefulWidget {
const StartPages({super.key});
#override
State<StartPages> createState() => StartPagesState();
}
class StartPagesState extends State<StartPages> {
int _selectedIndex = 2;
//static const TextStyle optionStyle =
//TextStyle(fontSize: 30, fontWeight: FontWeight.bold, color: Colors.white);
static const List<Widget> _widgetOptions = <Widget>[
Icon(
Icons.calendar_month,
size:150,
color: Colors.white,
),
Icon(
Icons.location_on,
size:150,
color: Colors.white,
),
Image(
image: NetworkImage('https://images.unsplash.com/photo-1604357209793-fca5dca89f97?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8bWFwfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=500&q=60'),
),
Icon(
Icons.group,
size:150,
color: Colors.white,
),
Icon(
Icons.groups,
size:150,
color: Colors.white,
),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('FindASpot'),
backgroundColor: Colors.grey[900],
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
backgroundColor: Colors.grey[700],
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.shifting,
selectedIconTheme: IconThemeData(color: Colors.amberAccent),
selectedLabelStyle: TextStyle(fontWeight: FontWeight.bold),
selectedItemColor: Colors.amberAccent,
items: const <BottomNavigationBarItem>[BottomNavigationBarItem(
icon: Icon(Icons.calendar_month),
label: 'Events',
backgroundColor: Colors.black45,
),
BottomNavigationBarItem(
icon: Icon(Icons.location_on),
label: 'Map',
backgroundColor: Colors.black45,
),
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
backgroundColor: Colors.black45,
),
BottomNavigationBarItem(
icon: Icon(Icons.group),
label: 'Friends',
backgroundColor: Colors.black45,
),
BottomNavigationBarItem(
icon: Icon(Icons.groups),
label: 'Groups',
backgroundColor: Colors.black45,
),
],
currentIndex: _selectedIndex,
//selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
}

I want to navigate with bottom navigation bar

I have flutter app and in my app I have to show bottom navigatiobar for every page
I am confuse that should I can navigate with with bottom navigation bar or I have to add bottom navigation bar for every page
If I use Navigation I have to use scaffold as parent of all widget so is it any way to navigate with scaffold.
Here is my code:-
class WorkerHomeScreen extends StatelessWidget {
WorkerHomeScreen({Key? key}) : super(key: key);
int _selectedPageIndex = 0;
List<Map<String, dynamic>> get _pages {
return [
{"page": SignInScreen(), "name": "PROPERTY", "action": null},
{
"page": ChoseRoleScreen(),
"name": "CHAT",
"action": ["Search"]
},
{"page": LandingPage(), "name": "VIDEO", "action": null},
{"page": EmployerSignUpScreen(), "name": "PROFILE", "action": null}
];
}
void _selectPage(int index) {
_selectedPageIndex = index;
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
),
body: _pages[_selectedPageIndex]["page"],
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
onTap: _selectPage,
currentIndex: _selectedPageIndex,
items: [
BottomNavigationBarItem(
icon: Icon(workerNavigationLists[0].icon),
label: "Property",
activeIcon: Icon(workerNavigationLists[0].icon),
),
BottomNavigationBarItem(
icon: Icon(workerNavigationLists[1].icon),
label: "Chat",
activeIcon: Icon(workerNavigationLists[0].icon),
),
BottomNavigationBarItem(
icon: Icon(workerNavigationLists[0].icon),
label: "Video",
),
BottomNavigationBarItem(
icon: Icon(workerNavigationLists[0].icon),
label: "Profile",
activeIcon: Icon(workerNavigationLists[0].icon),
),
],
),
);
}
}
Here is my screen:-
class SignInMobile extends StatelessWidget {
SignInMobile({Key? key}) : super(key: key);
final SignInController signInController = Get.find();
#override
Widget build(BuildContext context) {
return Container(
child: Center(
child: RaisedButton(
onPressed: () {
/// I want to navigate to other page but I want navigation bar in other page also
},
child: Text("TAP TO NAVIGATE"),
),
),
);
}
}
So from the Discussion I have Create an example
Check this link below for demo:
https://github.com/sagaracharya24/bottomApp.git
So in order to make this implementation you have have a knowledge of following things
OnGenerate Route mechanism.
GlobalKeys and NavigatorState.
Know how Offstage widget works under the hood.
What you will get from the Example:
Each Page maintains the stack of pages as per BottomBarItem.
If you are deep inside the nested pages and click in the same item, it will remove all the pages and com back to the main page.
I have also add the sample Gif To show you the app is working.
Let me know the if it works for you thanks.
You can use persistent_bottom_nav_bar to achieve your expectation. Do as follows:
late PersistentTabController _controller;
#override
void initState() {
super.initState();
_controller = PersistentTabController(initialIndex: 0);
}
List<PersistentBottomNavBarItem> _navBarsItems() {
return [
PersistentBottomNavBarItem(
icon: Image.asset('assets/logowhite.png', height: 30, width: 30),
inactiveIcon:
Image.asset('assets/logowhite.png', height: 30, width: 30),
title: "Vesvusa",
textStyle: const TextStyle(color: Colors.white),
activeColorPrimary: MyTheme.grey,
activeColorSecondary: Colors.white,
contentPadding: 5,
inactiveColorPrimary: Colors.white,
inactiveColorSecondary: Colors.white,
routeAndNavigatorSettings: RouteAndNavigatorSettings(
initialRoute: '/dashboard',
routes: {
'/newsevents': (context) => NewsListScreen(
menuScreenContext: context,
hideMainAppBar: hideMainAppBar,
itemCount: null),
'/blogdetails': (context) => BlogDetails(
menuScreenContext: context,
hideMainAppBar: hideMainAppBar,
),
'/videoslist': (context) => VideosList(menuScreenContext: context),
'/bookings': (context) => Bookings(menuScreenContext: context),
'/lookstheme': (context) => LooksTheme(menuScreenContext: context),
'/catalog': (context) => Catalog(menuScreenContext: context),
'/productdetails': (context) =>
ProductDetails(menuScreenContext: context),
},
),
),
PersistentBottomNavBarItem(
icon: Image.asset(
'assets/search.png',
height: 25,
width: 25,
color: Colors.white,
),
inactiveIcon: Image.asset(
'assets/search.png',
height: 25,
width: 25,
color: Colors.white,
),
title: ("Search"),
activeColorPrimary: MyTheme.grey,
activeColorSecondary: Colors.white,
contentPadding: 5,
inactiveColorPrimary: Colors.white,
inactiveColorSecondary: Colors.white,
routeAndNavigatorSettings: const RouteAndNavigatorSettings(
initialRoute: '/search',
routes: {
// '/first': (context) => MainScreen2(),
// '/second': (context) => MainScreen3(),
},
),
),
PersistentBottomNavBarItem(
icon: Image.asset(
'assets/favourite.png',
height: 25,
width: 25,
color: Colors.white,
),
inactiveIcon: Image.asset(
'assets/favourite.png',
height: 25,
width: 25,
color: Colors.white,
),
title: ("Favorite"),
activeColorPrimary: MyTheme.grey,
activeColorSecondary: Colors.white,
contentPadding: 5,
inactiveColorPrimary: Colors.white,
inactiveColorSecondary: Colors.white,
textStyle: const TextStyle(color: Colors.white),
routeAndNavigatorSettings: const RouteAndNavigatorSettings(
initialRoute: '/favorite',
routes: {
// '/first': (context) => MainScreen2(),
// '/second': (context) => MainScreen3(),
},
),
// onPressed: (context) {
// pushDynamicScreen(context,
// screen: SampleModalScreen(), withNavBar: true);
// }
),
PersistentBottomNavBarItem(
icon: Image.asset(
'assets/cart.png',
height: 25,
width: 25,
color: Colors.white,
),
inactiveIcon: Image.asset(
'assets/cart.png',
height: 25,
width: 25,
color: Colors.white,
),
title: ("Cart"),
activeColorPrimary: MyTheme.grey,
activeColorSecondary: Colors.white,
contentPadding: 5,
inactiveColorPrimary: Colors.white,
inactiveColorSecondary: Colors.white,
textStyle: const TextStyle(color: Colors.white),
routeAndNavigatorSettings: const RouteAndNavigatorSettings(
initialRoute: '/cart',
routes: {
// '/first': (context) => MainScreen2(),
// '/second': (context) => MainScreen3(),
},
),
),
PersistentBottomNavBarItem(
icon: Image.asset(
'assets/profile.png',
height: 25,
width: 25,
color: Colors.white,
),
inactiveIcon: Image.asset(
'assets/profile.png',
height: 25,
width: 25,
color: Colors.white,
),
title: ("Profile"),
activeColorPrimary: MyTheme.grey,
activeColorSecondary: Colors.white,
contentPadding: 5,
inactiveColorPrimary: Colors.white,
inactiveColorSecondary: Colors.white,
textStyle: const TextStyle(color: Colors.white),
routeAndNavigatorSettings: RouteAndNavigatorSettings(
initialRoute: '/profile',
routes: {
'/orders': (context) => MyOrders(
menuScreenContext: context,
),
'/loginsecurity': (context) => LoginSecurity(
menuScreenContext: context,
),
'/payment': (context) => Payment(
menuScreenContext: context,
),
'/message': (context) => Messages(
menuScreenContext: context,
),
'/devices': (context) => Devices(
menuScreenContext: context,
),
'/devices': (context) => Devices(
menuScreenContext: context,
),
'/inboxdetails': (context) => InboxDetails(
menuScreenContext: context,
),
'/loyalty': (context) => Loyalty(menuScreenContext: context),
},
),
),
];
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: MyTheme.themeColor,
appBar: AppBar(
elevation: 0,
iconTheme: IconThemeData(
color: MyTheme.grey,
),
centerTitle: true,
backgroundColor: MyTheme.themeColor,
title: Image.asset("assets/titles.png", height: 60, width: 100),
actions: [
Builder(
builder: (context) {
return InkWell(
onTap: () {
Scaffold.of(context).openEndDrawer();
},
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 20, 0),
child: Icon(Icons.notifications_none_outlined,
color: MyTheme.grey),
));
},
)
],
),
drawer: MyDrawer(),
endDrawer: const NotificationDrawer(),
body: PersistentTabView(
context,
controller: _controller,
screens: _buildScreens(),
items: _navBarsItems(),
confineInSafeArea: true,
backgroundColor: MyTheme.themeColor,
handleAndroidBackButtonPress: true,
resizeToAvoidBottomInset: true,
stateManagement: true,
hideNavigationBarWhenKeyboardShows: true,
margin: const EdgeInsets.all(0.0),
popActionScreens: PopActionScreensType.once,
bottomScreenMargin: 0.0,
onWillPop: (context) async {
await showDialog(
context: context!,
useSafeArea: false,
builder: (context) => CommonAlert());
return false;
},
selectedTabScreenContext: (context) {
context = context;
},
hideNavigationBar: _hideNavBar,
popAllScreensOnTapOfSelectedTab: true,
itemAnimationProperties: const ItemAnimationProperties(
duration: Duration(milliseconds: 200),
curve: Curves.ease,
),
screenTransitionAnimation: const ScreenTransitionAnimation(
animateTabTransition: true,
curve: Curves.ease,
duration: Duration(milliseconds: 200),
),
navBarStyle:
NavBarStyle.style7, // Choose the nav bar style with this property
),
);
}
You can navigate to other page doing this
pushNewScreen(context,
screen: Loyalty(
menuScreenContext: context,
),pageTransitionAnimation:PageTransitionAnimation.scale);
},
You don't need to add BottomNavigationBar in all pages if you define it in your HomePage (Your first page) only.
if you dont want to use any packages in order to maintain the appsize or reduce it, please use the inbuilt flutter bottom navigation
import 'package:flutter/material.dart';
class Mynavigation extends StatefulWidget {
const Mynavigation({Key key}) : super(key: key);
#override
State<Mynavigation> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<Mynavigation> {
int _selectedIndex = 0;
static const TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
List<Widget> _widgetOptions = <Widget>[
Home(),Business(),School(), Settings()
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('BottomNavigationBar Sample'),
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
backgroundColor: Colors.red,
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
label: 'Business',
backgroundColor: Colors.green,
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
label: 'School',
backgroundColor: Colors.purple,
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
label: 'Settings',
backgroundColor: Colors.pink,
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
}
you do notice this line having
Home(),Business(),School(), Settings(),
those should be individual pages where you want to navigate to.SO PLEASE change them to point to the pages you want to use.
and then one more thing just replace Mynavigation with WorkerHomeScreen and you are good to go,
if your using flutter sdk >2.12.0
please use on this line
static const List<Widget> _widgetOptions = <Widget>[
else if your sdk is below 2.12.0 then use
List<Widget> _widgetOptions = <Widget>[
incase of any question, I will be waiting
Good luck bro
You can simply add BottomNevigatioBar using scaffold property and add multiple Page to the List. Hope you got your solution
import 'package:flutter/material.dart';
import 'package:traveling/helpers/AppColors.dart';
import 'package:traveling/screens/Employee/home/Home.dart';
import 'package:traveling/screens/Employee/profile/Profile.dart';
import 'package:traveling/screens/Employee/setting/Setting.dart';
class EmployeeBottomNavBar extends StatefulWidget {
const EmployeeBottomNavBar({Key? key}) : super(key: key);
#override
_EmployeeBottomNavBarState createState() => _EmployeeBottomNavBarState();
}
class _EmployeeBottomNavBarState extends State<EmployeeBottomNavBar> {
int pageIndex = 0;
bool visible = true;
List<Widget> pageList = <Widget>[EmployeeHome(), Profile(leadingIcon: false,), Setting()];
#override
Widget build(BuildContext context) {
return Scaffold(
body: pageList[pageIndex],
bottomNavigationBar: BottomNavigationBar(
fixedColor: Colors.redAccent[400],
currentIndex: pageIndex,
onTap: (value) {
setState(() {
pageIndex = value;
});
},
// type: BottomNavigationBarType.fixed,
items: [
BottomNavigationBarItem(
activeIcon: Container(
height: 40,
width: 80,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Color(0xff2161c0),
),
child: Icon(
Icons.home,
color: AppColors.white,
),
),
icon: Icon(
Icons.home,
color: Color(0xff2161c0),
),
label: ""),
BottomNavigationBarItem(
activeIcon: Container(
height: 40,
width: 80,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Color(0xff2161c0),
),
child: Icon(
Icons.person,
color: AppColors.white,
),
),
icon: Icon(
Icons.person,
color: AppColors.baseLightBlueColor,
),
label: ""),
BottomNavigationBarItem(
activeIcon: Container(
height: 40,
width: 80,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: AppColors.baseLightBlueColor,
),
child: Icon(
Icons.settings,
color: AppColors.white,
),
),
icon: Icon(
Icons.settings,
color: AppColors.baseLightBlueColor,
),
label: ""),
]
)
);
}
}

bottomNavigationBar OnTap does not trigger for navigate to another page

#override
void initState() {
super.initState();
}
int selectedPage = 0;
void changePage(int index) {
setState(() {
selectedPage = index;
});
}
bottomNavigationBar: BottomNavigationBar(
showUnselectedLabels: true,
currentIndex: selectedPage,
onTap: showPage,
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Main',
backgroundColor: Colors.blue),
BottomNavigationBarItem(
icon: Icon(Icons.category_outlined),
label: 'Category',
backgroundColor: Colors.blue),
BottomNavigationBarItem(
icon: Icon(Icons.book_online),
label: 'Photos',
backgroundColor: Colors.blue),
BottomNavigationBarItem(
icon: Icon(Icons.video_call),
label: 'Video',
backgroundColor: Colors.blue),
],
Widget showPage(int selectedPage) {
if (selectedPage == 0) {
return NewsViewDetail(id: '0');
} else if (selectedPage == 1) {
return NewsLoading(text: 'load');
}
return NewsLoading(text: '1');
}
When I tap first or second item on the there is no reaction from UI. It seems onTap does not navigate to different pages.
Could you please help me why this code is not working?
Edit: I think the problem causing Scaffold body. Current Scaffold body is:
body: TabBarView(
children: [
for (final tab in filteredList)
NewsView(
id: tab.id!,
),
],
),
How can I integrate showPage(_selectedIndex), into Scaffold Body without hurt the TabbarView?
here is the TabBarController
return DefaultTabController(
// length: snapshot.data!.data!.length,
length: filteredList.length,
child: Scaffold(
appBar: AppBar(
backgroundColor: (Colors.white),
iconTheme: const IconThemeData(color: Colors.black),
title: Transform.translate(
offset: const Offset(-24.0, 0.0),
child: Image.asset("assets/images/lo.png",
fit: BoxFit.contain, height: 22),
),
bottom: PreferredSize(
preferredSize: const Size.fromHeight(30.00),
child: ColoredBox(
color: Colors.white,
child: TabBar(
labelColor: Colors.purple[100],
indicatorColor: Colors.purple,
isScrollable: true,
labelPadding:
const EdgeInsets.symmetric(horizontal: 8.0),
tabs: tabs),
),
),
),
```
Check it, it will be helped you to solve your solution
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: MyNavigationBar(),
);
}
}
class MyNavigationBar extends StatefulWidget {
MyNavigationBar({Key key}) : super(key: key);
#override
_MyNavigationBarState createState() => _MyNavigationBarState();
}
class _MyNavigationBarState extends State<MyNavigationBar>
with TickerProviderStateMixin {
int _selectedIndex = 0;
void changePage(int index) {
setState(() {
_selectedIndex = index;
});
}
Widget showPage(int selectedPage) {
if (selectedPage == 0) {
return Container(
child: Text('Main Page',
style: TextStyle(fontSize: 35, fontWeight: FontWeight.bold)),
);
} else if (selectedPage == 1) {
return Container(
child: Text('Category page',
style: TextStyle(fontSize: 35, fontWeight: FontWeight.bold)),
);
} else if (selectedPage == 2) {
return Container(
child: Text('Photo page',
style: TextStyle(fontSize: 35, fontWeight: FontWeight.bold)),
);
}
return Container(
child: Text('video Page',
style: TextStyle(fontSize: 35, fontWeight: FontWeight.bold)),
);
}
TabController tabController;
#override
void initState() {
// TODO: implement initState
super.initState();
tabController = TabController(initialIndex: 0, length: 2, vsync: this);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Container(
decoration: BoxDecoration(
color: Colors.white.withOpacity(0.0),
//This is for bottom border that is needed
border:
Border(bottom: BorderSide(color: Colors.grey, width: 0.8))),
child: TabBar(
indicatorWeight: 2,
controller: tabController,
indicatorColor: Colors.purple,
labelColor: Colors.purple,
unselectedLabelColor: Color(0xff002540).withOpacity(0.7),
tabs: [
Tab(
child: Text(
"First",
),
),
Tab(
child: Text(
"Second",
),
),
],
),
),
backgroundColor: Colors.green,
),
body: SingleChildScrollView(
child: Column(
children: [
Container(
height: MediaQuery.of(context).size.height,
child: TabBarView(controller: tabController, children: [
Container(
child: Center(
child: showPage(_selectedIndex),
),
),
Container(
child: Center(
child: showPage(_selectedIndex),
),
),
]),
),
],
)),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Main',
backgroundColor: Colors.blue),
BottomNavigationBarItem(
icon: Icon(Icons.category_outlined),
label: 'Category',
backgroundColor: Colors.blue),
BottomNavigationBarItem(
icon: Icon(Icons.book_online),
label: 'Photos',
backgroundColor: Colors.blue),
BottomNavigationBarItem(
icon: Icon(Icons.video_call),
label: 'Video',
backgroundColor: Colors.blue),
],
type: BottomNavigationBarType.shifting,
currentIndex: _selectedIndex,
selectedItemColor: Colors.black,
iconSize: 40,
onTap: changePage,
elevation: 5),
);
}
}
Output:

How do you create a Flutter bottom navigation with a top line on active menu item?

I have the following Flutter bottom navigation bar
And I would like to add a top-line or boarder for active items like so
Is that even possible, my code is straight forward.
#override
Widget build(BuildContext context) {
return Scaffold(
body: _tabs[_tabIndex],
bottomNavigationBar: BottomNavigationBar(
backgroundColor: Colors.white,
selectedLabelStyle: TextStyle(fontSize: 14),
selectedItemColor: Theme.of(context).accentColor,
unselectedLabelStyle: TextStyle(fontSize: 14.0),
unselectedItemColor: Color(0xff546481),
type: BottomNavigationBarType.fixed,
showSelectedLabels: true,
showUnselectedLabels: true,
currentIndex: _tabIndex,
onTap: (int index) {
setState(() {
_tabIndex = index;
});
},
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home_outlined),
label: 'HOME',
),
BottomNavigationBarItem(
icon: Icon(Icons.history_outlined),
label: 'HISTORY',
),
BottomNavigationBarItem(
icon: Icon(Icons.person_outline),
label: 'PROFILE',
),
BottomNavigationBarItem(
icon: Icon(Icons.settings_outlined),
label: 'SETTINGS',
),
],
),
);
}
}
there are some packages can achieve this effect:
titled_navigation_bar
bottomNavigationBar: TitledBottomNavigationBar(
currentIndex: 2, // Use this to update the Bar giving a position
onTap: (index){
print("Selected Index: $index");
},
items: [
TitledNavigationBarItem(title: Text('Home'), icon: Icons.home),
TitledNavigationBarItem(title: Text('Search'), icon: Icons.search),
TitledNavigationBarItem(title: Text('Bag'), icon: Icons.card_travel),
TitledNavigationBarItem(title: Text('Orders'), icon: Icons.shopping_cart),
TitledNavigationBarItem(title: Text('Profile'), icon: Icons.person_outline),
]
)
bottom_indicator_bar
class _HomePageState extends State<HomePage> {
final List<BottomIndicatorNavigationBarItem> items = [
BottomIndicatorNavigationBarItem(icon: Icons.home),
BottomIndicatorNavigationBarItem(icon: Icons.search),
BottomIndicatorNavigationBarItem(icon: Icons.settings),
];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Indicator Bottom Bar"),
backgroundColor: Colors.teal,
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
],
),
),
bottomNavigationBar: BottomIndicatorBar(
onTap: (index) => {},
items: items,
activeColor: Colors.teal,
inactiveColor: Colors.grey,
indicatorColor: Colors.teal,
),
);
}
}
You can use a TabBar instead of a BottomNavigationBar using a custom decoration:
class TopIndicator extends Decoration {
#override
BoxPainter createBoxPainter([VoidCallback? onChanged]) {
return _TopIndicatorBox();
}
}
class _TopIndicatorBox extends BoxPainter {
#override
void paint(Canvas canvas, Offset offset, ImageConfiguration cfg) {
Paint _paint = Paint()
..color = Colors.cyan
..strokeWidth = 5
..isAntiAlias = true;
canvas.drawLine(offset, Offset(cfg.size!.width + offset.dx, 0), _paint);
}
}
Then pass the decoration to the TapBar using TapBar(indicator: TopIndicator ...).
To use the TabBar as the Scaffold.bottomNavigationBar, you will most likely want to wrap it in a Material to apply a background color:
Scaffold(
bottomNavigationBar: Material(
color: Colors.white,
child: TabBar(
indicator: TopIndicator(),
tabs: const <Widget>[
Tab(icon: Icon(Icons.home_outlined), text: 'HOME'),
...
],
),
),
...
)
Thanks Ara Kurghinyan for the original idea.
Column(
children: [
pageIndex == 2
? Container(
height: 5.w,
width: 35.h,
alignment: Alignment.center,
// margin: const EdgeInsets.only(left: 5),
decoration: const BoxDecoration(
color: Colors.teal,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(
3,
),
bottomRight: Radius.circular(3)),
),
// height: 5,
// width: 35,
)
: SizedBox(
height: 5.w,
width: 35.h,
),
SizedBox(
height: 4.h,
),
Flexible(
child: Container(
height: 25.h,
width: 25.w,
// alignment: Alignment.lerp(
// Alignment.bottomLeft, Alignment.bottomRight, 1),
decoration: BoxDecoration(
// color: Colors.red,
border: Border.all(
width: 1,
color: Colors.black,
),
borderRadius: const BorderRadius.all(
Radius.circular(20),
),
),
child: IconButton(
iconSize: 25,
alignment: Alignment.center,
padding: const EdgeInsets.only(top: 4, bottom: 4),
visualDensity:
const VisualDensity(horizontal: -4, vertical: -4),
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 2;
});
},
icon: pageIndex == 2
? Image.asset(
'assets/icons/icons8-alarm-50.png',
height: 24.h,
color: Colors.black,
// size: 35.sm,
)
: Image.asset(
'assets/icons/icons8-alarm-50.png',
color: Colors.black,
// size: 35.sm,
)),
),
),
pageIndex == 2
? Container(
height: 15.h,
width: 60.w,
alignment: Alignment.lerp(
Alignment.centerLeft, Alignment.centerRight, 0.75),
child: Text(
'Reminder',
style: GoogleFonts.lato(
textStyle: const TextStyle(
fontSize: 12, fontWeight: FontWeight.bold),
),
),
)
: Container(),
],
),