BackButton and BottomNavigationBar - flutter

I want to navigate between my pages, when I'm on the settings page I want a back button that takes me to the home page and the same for the favorites, from my pages there is also a bottom navigation bar that allows you to navigate between them. I have already tried with the Navigator() but it forces me to put another button on the home page. I don't know if I made myself clear, but I hope you can help me.
Thank you!
Update : I have used getX 's package.

If the settings page doesn't include the bottom app bar, then you can use Navigator.of(context).pop() or you can use a top app bar and set automaticallyImplyLeading = true to display a back button which also pops.
Here is a example of one of my bottom app bars which allows smooth navigation across multiple pages:
class _MainScreenState extends State<MainScreen> {
int _currentIndex = 0;
final List<Widget> _children = [
Home(),
InboxMain(),
DashboardMain(),
ProfileMain(),
FriendsMain()
];
#override
Widget build(BuildContext context) {
return Scaffold(
body: _children[_currentIndex],
bottomNavigationBar: new Theme(
data: Theme.of(context).copyWith(
canvasColor: Color.fromRGBO(41, 34, 78, 1),
), //
child: BottomNavigationBar(
//selectedItemColor: Colors.red,
//selectedIconTheme: IconThemeData(color: Colors.red),
type: BottomNavigationBarType.fixed,
showSelectedLabels: false,
showUnselectedLabels: false,
backgroundColor: Color.fromRGBO(41, 34, 78, 1),
onTap: onTabTapped,
currentIndex: _currentIndex,
items: [
BottomNavigationBarItem(
icon: _currentIndex == 0
? Icon(Icons.home_outlined, color: Colors.white)
: Icon(
Icons.home_outlined,
color: Colors.grey[600],
),
title: Container(
height: 0,
)),
BottomNavigationBarItem(
icon: _currentIndex == 1
? Icon(
Icons.email_outlined,
color: Colors.white,
)
: Icon(
Icons.email_outlined,
color: Colors.grey[600],
),
title: Container(
height: 0,
)),
BottomNavigationBarItem(
icon: _currentIndex == 2
? Icon(Icons.dashboard, color: Colors.white)
: Icon(Icons.dashboard, color: Colors.grey[600]),
title: Container(
height: 0,
)),
BottomNavigationBarItem(
icon: _currentIndex == 3
? Icon(
Icons.person,
color: Colors.white,
)
: Icon(Icons.person, color: Colors.grey[600]),
title: Container(
height: 0,
)),
BottomNavigationBarItem(
icon: _currentIndex == 4
? Icon(
Icons.people_alt,
color: Colors.white,
)
: Icon(Icons.people_alt, color: Colors.grey[600]),
title: Container(
height: 0,
)),
],
),
),
);
}
void onTabTapped(int index) {
setState(() {
_currentIndex = index;
});
}
}
My app (once logged in) navigates to this, and all navigation is controlled here, other than extended screens which I use a back button and pop().

Related

navigation bar issue after clicking on card 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

Where is the black behind my navigation bar coming from?

After rounding the corners of my navigation bar I noticed there's still something there. When I set the scaffolds background color to transparent, what's behind my navigation bar is black.. if I change it to blue, it's blue.. So is it the scaffold? How do I get it to show my home page behind it? I'm new at this...
class Navigation extends StatefulWidget {
#override
_NavigationState createState() => _NavigationState();
}
class _NavigationState extends State<Navigation> {
int _selectedIndex = 0;
List<Widget> _widgetOptions = <Widget>[
Home(),
Options(),
Menu(),
Search (),
Text ('Profile')
];
void _onItemTap(int index) {
setState(() {
_selectedIndex = index;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.transparent,
body: Stack (
children: [_widgetOptions.elementAt(_selectedIndex)],
),
bottomNavigationBar: Container(
margin: EdgeInsets.zero,
decoration: BoxDecoration(
color: Color (0xFF213359),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30.0),
topRight: Radius.circular(30.0),),),
child: Theme(
data: ThemeData(
splashColor: Colors.transparent,
highlightColor: Colors.transparent,),
child: BottomNavigationBar(
backgroundColor: Colors.transparent,
showUnselectedLabels: false,
showSelectedLabels: false,
type: BottomNavigationBarType.fixed,
elevation: 0,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(MyFlutterAppHouse.sketch_house,
size: 27.5,), label: (''),),
BottomNavigationBarItem(
icon: Icon(MyFlutterAppOptionsLong.sketch_options_long,
size: 35), label: (''),),
BottomNavigationBarItem(
icon: Icon((MyFlutterAppBook.sketch_book),
size: 34, ), label: (''),),
BottomNavigationBarItem(
icon: Icon(MyFlutterAppSearch.sketch_search,
size: 34), label: (''),),
BottomNavigationBarItem(
icon: Icon(MyFlutterAppPerson.sketch_person,
size: 30.5), label: (''),),
],
currentIndex: _selectedIndex,
onTap: _onItemTap,
unselectedItemColor: Colors.grey[600],
selectedItemColor: Colors.white,
),
),
),
);
}
}
yes it is the scaffold once its transparent you see the material app which is black ,
one you put color back it appears , it is that simple.
and if you want to see your home page from the radius gap you can add extendBody property to your scaffold
extendBody: true,

Adding onPressed or onTab to BubbleBottomBarItem in Flutter

Been trying to solve this all day.
I am basically new to flutter.
I want the Bottom icon buttons to have its own onTap methods like the normal FAB or BottomNavigationBar. please help! Thank you
Here is the code.
return BubbleBottomBar(
hasNotch: true,
opacity: .2,
currentIndex: currentIndex,
onTap: changePage,
borderRadius: BorderRadius.vertical(
top: Radius.circular(
16)),
elevation: 8,
items: <BubbleBottomBarItem>[
BubbleBottomBarItem(
backgroundColor: Colors.indigo,
icon: Icon(
Icons.home,
color: Colors.black,
),
activeIcon: Icon(
Icons.home,
color: Colors.indigo,
),
title: Text("Home")),
BubbleBottomBarItem(
backgroundColor: Colors.indigo,
icon: Icon(
Icons.folder_open,
color: Colors.black,
),
activeIcon: Icon(
Icons.folder_open,
color: Colors.indigo,
),
title: Text("Get Job")),
BubbleBottomBarItem(
backgroundColor: Colors.indigo,
icon: Icon(
Icons.add_circle_outline,
color: Colors.black,
),
activeIcon: Icon(
Icons.add_circle_outline,
color: Colors.indigo,
),
title: Text("Add Job")),
],
);
}
}
The BubbleBottomBarItem does not have any onTap or onPressed method, hence you need to use onTap in BubbleBottomBar. You can implement changePage as given in below example to handle the onTap in BubbleBottomBar.
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int currentIndex;
#override
void initState() {
// TODO: implement initState
super.initState();
currentIndex = 0;
}
void changePage(int index) {
setState(() {
currentIndex = index;
});
//You can have a switch case to Navigate to different pages
switch (currentIndex){
case 0:
Navigator.push( context,
MaterialPageRoute(builder: (context) => AnyClass()),
);
break;
...
}
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
bottomNavigationBar: BubbleBottomBar(
hasNotch: true,
fabLocation: BubbleBottomBarFabLocation.end,
opacity: .2,
currentIndex: currentIndex,
onTap: changePage,
borderRadius: BorderRadius.vertical(
top: Radius.circular(
16)),
elevation: 8,
items: <BubbleBottomBarItem>[
...
],
),
);
}
}

Flutter - after clicking bottom navigation menu icon => selected icon color and text color not changing

I am new to flutter , Please help me to get out of this issue,
Issue -> After click any of the menu icon , color is not changing
While staring app , icon colours are setting correctly , Home icon is default , if I click Scan or settings icon green color is not setting for icon as well as text ,
I have tried activeIcon of BottomNavigationBarItem still not working
Here is my code,
class TabNavigationState extends State<ScaffoldTest> {
int currentTabIndex = 1;
var tabs = [Home(), Camera(), Settings()];
onTabbed(index) => {
setState(() {
currentTabIndex = index;
})
};
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.teal,
leading: IconButton(
icon: Icon(
Icons.search,
color: Colors.white,
),
onPressed: null),
centerTitle: false,
actions: <Widget>[
IconButton(
icon: Icon(
Icons.info_outline,
color: Colors.white,
),
color: Colors.white,
onPressed: () => debugPrint("Icon tabbed")),
IconButton(
icon: Icon(
Icons.save,
color: Colors.white,
),
color: Colors.white,
onPressed: () => debugPrint("Icon tabbed")),
],
title: Text(
"Test",
style: TextStyle(color: Colors.white),
),
),
backgroundColor: Colors.white,
body: tabs[currentTabIndex],
floatingActionButton: FloatingActionButton(
onPressed: null,
child: IconButton(
icon: Icon(
Icons.camera,
color: Colors.white,
),
),
),
bottomNavigationBar: BottomNavigationBar(
// backgroundColor: Colors.blueAccent.shade100,
selectedItemColor: Colors.green,
unselectedItemColor: Colors.grey,
showSelectedLabels: true,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text(
"Home",
textDirection: TextDirection.ltr,
)),
BottomNavigationBarItem(
icon: Icon(Icons.camera),
title: Text(
"Scan",
textDirection: TextDirection.ltr,
)),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
title: Text(
"Settings",
textDirection: TextDirection.ltr,
))
],
onTap: onTabbed,
),
);
}
https://i.stack.imgur.com/aGJqG.png
BottomNavigationBar has attribute currentIndex to know which is current active tab. You need to set it in your onTabbed method like
int _selectedIndex = 0;
void onTabbed(int index) {
setState(() {
_selectedIndex = index;
...
});
....
}
// And use _selectedIndex in BottomNavigationBar
Widget build(BuildContext context) {
return Scaffold(
...
bottomNavigationBar: BottomNavigationBar(
currentIndex: _selectedIndex,
),
...
);
}

Customise flutter navbar / tabbar with individual background colors

In both a tabbar and nav bar, I would like the selected tab to have a different background to the rest of the bar.
For example: https://imgur.com/a/jxD8MTg
Is this possible in flutter?
If you are using stateful Widget, you could just set the state of the currentIndex in the on Tap method
#override
void initState() {
super.initState();
currentIndex = 0;
}
BottomNavigationBar(
backgroundColor: Colors.white,
type: BottomNavigationBarType.fixed,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
backgroundColor: currentIndex == 0 ? Colors.green : Colors.black,
title: Text('0'),
),
BottomNavigationBarItem(
backgroundColor: currentIndex == 1 ? Colors.green : Colors.black,
title: Text('1'),
),
],
currentIndex: currentIndex,
onTap: (int index) {
setState(() {
currentIndex = index;
});
_navigateToPage(index);
},
);
import 'package:flutter/material.dart';
class ChangeBottomNavBarTextColor extends StatefulWidget {
#override
ChangeBottomNavBarTextColorState createState() {
return new ChangeBottomNavBarTextColorState();
}
}
class ChangeBottomNavBarTextColorState
extends State<ChangeBottomNavBarTextColor> {
String text = 'Home';
var curIndex = 0;
_onTap(int index) {
setState(() {
curIndex = index;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Change Bottom Nav Bar Text Color Example"),
),
body: Center(
child: Text(text,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
)),
),
bottomNavigationBar: BottomNavigationBar(
selectedItemColor: Colors.red,
currentIndex: curIndex,
type: BottomNavigationBarType.fixed,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(
Icons.home,
),
title: Text("Home", style: TextStyle(color: Colors.teal)),
),
BottomNavigationBarItem(
icon: Icon(
Icons.favorite,
),
title: Text("Favorite", style: TextStyle(color: Colors.pink)),
),
BottomNavigationBarItem(
icon: Icon(
Icons.person,
),
title: Text("Profile", style: TextStyle(color: Colors.brown)),
),
BottomNavigationBarItem(
activeIcon: Icon(
Icons.info,
color: Colors.red,
),
icon: Icon(
Icons.settings,
),
title: Text("Settings", style: TextStyle(color: Colors.amber)),
),
],
onTap: _onTap,
),
);
}
}
You can use something like that. When you clicked, _onTap function will change currentIndex. Then selectedItemColor will define the color of the selected index. You can play with the colors whatever you want.