Page goes blank when calling My bottomnavigation class - flutter

I have a created a bottom navigation bar on another page and when i am calling this class on my home page only home page is bottom navigation bar is showing there the app bar, drawer all becomes invisible
Help me with this!
This is my home.dart page on which i am calling my bottomNavigation class
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 StatelessWidget {
const MyHomePage({super.key});
#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,
),),
),
)
],
),
),*/
drawer: MyDrawer(),
bottomNavigationBar: BottomNavigation(),
);
}
}
And the bottomnavigation.dart page is here
import 'package:flutter/material.dart';
import 'package:eva_icons_flutter/eva_icons_flutter.dart';
void main()
{
runApp(
BottomNavigation()
);
}
class BottomNavigation extends StatefulWidget {
const BottomNavigation({super.key});
#override
State<BottomNavigation> createState() => _BottomNavigationState();
}
class _BottomNavigationState extends State<BottomNavigation> {
#override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
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
),
]
),
);
}
}
After running home.dart page i am only getting bottom navigation bar not appbar and anything
Help me with this!
I have created two pages on flutter on first page named home.dart App bar is and drawer is there and i am calling bottomnavigation bar class which is created by me but when the code runs I am only getting bottom navigation bar not app bar and drawer
Help me with this!

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(),
);
}
}

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

Bottom Navigation Not Persisting On Some Pages

I have been trying to make the bottom navigation bar persisting on all page screens but it looks like it is only persisting for the pages that are on the bottom navigation only i.e HomeScreen(), DiscoverScreen(), GivingScreen(), EventsScreen() and SettingsScreen(). Other pages are not getting the bottom navigation. Here is the code of my bottom navigation bar. How can I add the bottom nav bar on all pages I have in the app?
class CustomBottomNavBar extends StatefulWidget {
const CustomBottomNavBar({Key key}) : super(key: key);
#override
_CustomBottomNavBarState createState() => _CustomBottomNavBarState();
}
class _CustomBottomNavBarState extends State<CustomBottomNavBar> {
int currentIndex = 0;
final screens = [
HomeScreen(),
DiscoverScreen(),
GivingScreen(),
EventsScreen(),
SettingsScreen(),
];
#override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: currentIndex,
children: screens,
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
backgroundColor: kPrimaryColor,
selectedItemColor: kSelectedItemColor,
iconSize: kBottomNavIconSize,
unselectedItemColor: kAlternativeColor,
selectedFontSize: kBottomNavFontSize,
unselectedFontSize: kBottomNavFontSize,
showUnselectedLabels: true,
showSelectedLabels: true,
currentIndex: currentIndex,
onTap: (index) => setState(() => currentIndex = index),
items: [
BottomNavigationBarItem(
icon: Icon(
CupertinoIcons.mic_fill,
),
label: 'Sermons',
),
BottomNavigationBarItem(
icon: Icon(
CupertinoIcons.wand_stars_inverse,
),
label: 'Discover',
),
BottomNavigationBarItem(
icon: Icon(
CupertinoIcons.heart_fill,
),
label: 'Giving',
),
BottomNavigationBarItem(
icon: Icon(
CupertinoIcons.calendar_today,
),
label: 'Events',
),
BottomNavigationBarItem(
icon: Icon(
CupertinoIcons.gear_alt_fill,
),
label: 'Settings',
),
],
),
);
}
}
Here is a picture of a view that has a bottom nav
Here is a picture of a view that has not a bottom nav
import 'package:church_app/components/widgets/animated_like_button.dart';
import 'package:church_app/components/widgets/custom_bottom_nav_bar.dart';
import 'package:church_app/components/widgets/navigation_drawer.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:church_app/utilities/constants.dart';
import 'package:church_app/components/widgets/action_and_text.dart';
class SermonDescriptionScreen extends StatefulWidget {
#override
_SermonDescriptionScreenState createState() =>
_SermonDescriptionScreenState();
}
class _SermonDescriptionScreenState extends State<SermonDescriptionScreen> {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
endDrawer: NavigationDrawer(),
appBar: AppBar(
leading:
(ModalRoute.of(context)?.canPop ?? false) ? BackButton() : null,
iconTheme: IconThemeData(color: kPrimaryColor),
elevation: 0,
title: Text(
'Protect The Vessel',
style: kMainStyling,
),
centerTitle: true,
),
body: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: SafeArea(
left: true,
right: true,
top: true,
bottom: true,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
height: 250,
decoration: BoxDecoration(
boxShadow: <BoxShadow>[
BoxShadow(
color: Colors.grey[300],
blurRadius: 30,
offset: Offset(0, 10),
),
],
image: DecorationImage(
fit: BoxFit.cover,
image: AssetImage('assets/images/pastor.jpg'),
),
),
),
addVSpace(30),
Container(
margin: EdgeInsets.only(left: 10, right: 10, bottom: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Protect The Vessel',
style: kDescriptionTitle,
softWrap: true,
),
addVSpace(3),
Text(
'Pastor James Wiseman',
style: kMainStyling.copyWith(
color: kPrimaryColor.withOpacity(.8),
),
softWrap: true,
),
addVSpace(3),
Row(
children: [
Text(
'Jan 25, 2021',
style: kMainStyling.copyWith(
color: kPrimaryColor.withOpacity(.8),
),
softWrap: true,
),
Padding(
padding: const EdgeInsets.only(left: 10, right: 10),
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
color: kPrimaryColor,
),
width: 8,
height: 8,
),
),
Container(
child: AnimatedLikeButton(),
),
],
),
addVSpace(20),
Text(
'Are you protecting what matters? In Protect What Matters, Pastor James Wiseman reminds us that we are vessels that can either foster bitterness or make way for the healing hand of God in our lives.',
style: kMainStyling,
softWrap: true,
textAlign: TextAlign.justify,
maxLines: 6,
),
addVSpace(30),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ActionAndText(
correspondingIcon:
CupertinoIcons.arrow_down_to_line_alt,
correspondingText: 'Download',
gesture: () {},
),
ActionAndText(
correspondingIcon: CupertinoIcons.heart,
correspondingText: 'Like',
gesture: () {},
),
ActionAndText(
correspondingIcon: CupertinoIcons.play,
correspondingText: 'Listen',
gesture: () {
Navigator.pushNamed(context, '/playerScreen');
},
),
ActionAndText(
correspondingIcon: CupertinoIcons.square_arrow_up,
correspondingText: 'Share',
gesture: () {},
),
],
),
],
),
),
],
),
),
),
),
);
}
}
to get this you can use pageView with bottomNavigationBar... this is a sample code. this will give idea how to go about it..
Scaffold buildAuthScreen() {
return Scaffold(
body: PageView(
scrollDirection: Axis.horizontal,
children: [
Timeline(),
ActivityFeedItem(),
Search(),
Upload(),
Profile(
currentUser: _auth.currentUser.uid,
),
],
controller: _pageController,
onPageChanged: (value) {
setState(() {
pageIndex = value;
});
},
physics: BouncingScrollPhysics(),
),
bottomNavigationBar: CurvedNavigationBar(
key: _bottomNavigationKey,
index: pageIndex,
height: 50.0,
color: Theme.of(context).primaryColor,
items: [
Icon(Icons.whatshot, color: Colors.white),
Icon(Icons.notifications_active, color: Colors.white),
Icon(Icons.search, color: Colors.white),
Icon(Icons.photo_camera, color: Colors.white),
Icon(Icons.account_circle, color: Colors.white),
],
animationDuration: Duration(milliseconds: 200),
buttonBackgroundColor: Theme.of(context).accentColor,
backgroundColor: Colors.white,
onTap: (int pageIndex1) {
_pageController.animateToPage(pageIndex1,
duration: Duration(microseconds: 300), curve: Curves.bounceInOut);
},
),
);
}
Use custom_navigator 0.3.0 package from pub dev website
You can use this package https://pub.dev/packages/persistent_bottom_nav_bar for persistent bottom navigator in all Screen,
And when try to navigate to new screen use this below navigator function,
pushNewScreen(
context,
screen: MainScreen(),
withNavBar: true, // OPTIONAL VALUE. True by default.
pageTransitionAnimation: PageTransitionAnimation.cupertino,
);
The problem with inbuilt BottomNavigationBar doesn't have ability to show to every screen,there is plugin called Presistant_bottom_nav_bar,here is some sample code for more checkout other properties of PersistentTabView,animation,state,hide bottombar when keyboard comes up kind of stuff
Also checkout this material guideline do's and don'ts bottom-navigation if you are not interested in using the plugin
PersistentTabController _controller =PersistentTabController(initialIndex:
0);
//Screens for each nav items.
List<Widget> _NavScreens() {
return [
HomeScreen(),
DiscoverScreen(),
GivingScreen(),
EventsScreen(),
SettingsScreen(),
];
}
List<PersistentBottomNavBarItem> _navBarsItems() {
return [
PersistentBottomNavBarItem(
icon: Icon(CupertinoIcons.mic_fill),
title: ("Home"),
activeColor: CupertinoColors.activeBlue,
inactiveColor: CupertinoColors.systemGrey,
),
PersistentBottomNavBarItem(
icon: Icon(CupertinoIcons.wand_stars_inverse),
title: ("Discover"),
activeColor: CupertinoColors.activeGreen,
inactiveColor: CupertinoColors.systemGrey,
),
PersistentBottomNavBarItem(
icon: Icon(CupertinoIcons.heart_fill),
title: ("Giving"),
activeColor: CupertinoColors.systemRed,
inactiveColor: CupertinoColors.systemGrey,
),
PersistentBottomNavBarItem(
icon: Icon(CupertinoIcons.calendar_today),
title: ("Events"),
activeColor: CupertinoColors.systemIndigo,
inactiveColor: CupertinoColors.systemGrey,
),
PersistentBottomNavBarItem(
icon: Icon(CupertinoIcons.gear_alt_fill),
title: ("Settings"),
activeColor: CupertinoColors.systemIndigo,
inactiveColor: CupertinoColors.systemGrey,
),
];
}
#override
Widget build(BuildContext context) {
return Center(
child: PersistentTabView(
controller: _controller,
screens: _NavScreens(),
items: _navBarsItems(),
confineInSafeArea: true,
backgroundColor: Colors.white,
handleAndroidBackButtonPress: true,
resizeToAvoidBottomInset: true,
hideNavigationBarWhenKeyboardShows: true,
decoration: NavBarDecoration(
borderRadius: BorderRadius.circular(10.0),
),
popAllScreensOnTapOfSelectedTab: true,
navBarStyle: NavBarStyle.style9,
),
);
}
You can use this navigate and use the BottomNavigationBar with navigator provided by the plugin
pushNewScreen(
context,
screen: SubScreenNames(),
withNavBar: true,
pageTransitionAnimation: PageTransitionAnimation.cupertino,
);

Selecting a specific BottomNavigationBar when routing from another widget (Flutter)

Please, help. This is my first project using Flutter. I am making an app for pets.
I have Homepage widget that has 4 bottom Navbar (Home, Photos, Medical, Profile). In Profile I have a button "About", which opens a widget about us.
But when I am trying to return to back from About, it returns to "Home" NavBottombar. I am trying to select Profile NavBottomBar.
I think that is related to current index in Homepage, which equals 0 at the beginning. But how can I change from 0 to 2 while routing?
homepage.dart
import 'package:flutter/material.dart';
import 'placeholder_widget.dart';
import 'dashboard.dart';
import 'medical.dart';
import 'profile.dart';
import 'about.dart';
void main() => runApp(MaterialApp(
home: Homepage(),
));
class Homepage extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<Homepage> {
int _currentIndex = 0;
final List<Widget> _children = [
Dashboard(),
PlaceholderWidget(Colors.deepOrange),
PlaceholderWidget(Colors.red),
// Medical(),
Profile()
];
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Furball Tales'),
backgroundColor: Colors.cyanAccent[400],
),
body: _children[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
onTap: onTabTapped,
currentIndex: _currentIndex,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.perm_media),
title: Text('Photos'),
),
BottomNavigationBarItem(
icon: Icon(Icons.local_hospital),
title: Text('Medical'),
),
BottomNavigationBarItem(
icon: Icon(Icons.perm_identity),
title: Text('Profile'),
),
],
selectedItemColor: Colors.cyanAccent[400],
unselectedItemColor: Colors.grey[600],
),
),
);
}
void onTabTapped(int index) {
setState(() {
_currentIndex = index;
});
}
}
about.dart
import 'package:flutter/material.dart';
import 'profile.dart';
import 'homepage.dart';
class About extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('About FurBallTales'),
backgroundColor: Colors.cyanAccent[400],
leading: GestureDetector(
onTap: () {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) {
return Homepage();
}), ModalRoute.withName('/'));
},
child: Icon(
Icons.arrow_back,
)),
),
body: ListView(
padding: const EdgeInsets.all(2),
children: <Widget>[
Container(
decoration:
BoxDecoration(border: Border.all(color: Colors.blueAccent)),
child: Column(
children: <Widget>[
Container(
height: 30,
margin: EdgeInsets.all(5),
child: Text(
'OUR MISSION',
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Colors.tealAccent[400]),
),
),
Container(
height: 25,
margin: EdgeInsets.all(5),
child: Text(
'Strengthening the bond of owners and pets, more than ever',
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.black),
),
),
],
),
),
Container(
height: 50,
color: Colors.amber[600],
child: const Center(child: Text('Entry A')),
),
Container(
height: 50,
color: Colors.amber[500],
child: const Center(child: Text('Entry B')),
),
Container(
height: 50,
color: Colors.amber[100],
child: const Center(child: Text('Entry C')),
),
],
),
);
}
}
profile.dart
import 'package:flutter/material.dart';
import 'sign_in.dart';
import 'login_page.dart';
import 'about.dart';
import 'donation.dart';
class Profile extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircleAvatar(
radius: 80,
backgroundImage: NetworkImage(
'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSxDoD5caxFUy_dn0w6wl01m882CeJHNVOCRg&usqp=CAU'),
),
Text(
'<$name>',
style: TextStyle(
fontFamily: 'SourceSansPro',
fontSize: 25,
),
),
Text(
'<$email>',
style: TextStyle(
fontSize: 20,
fontFamily: 'SourceSansPro',
color: Colors.red[400],
letterSpacing: 2.5,
),
),
SizedBox(
height: 20.0,
width: 200,
child: Divider(
color: Colors.teal[200],
),
),
// this is about page-----------------------------------------
InkWell(
onTap: () {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) {
return About();
}), ModalRoute.withName('/about'));
},
child: Card(
color: Colors.white,
margin:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
child: ListTile(
leading: Icon(
Icons.help,
color: Colors.teal[900],
),
title: Text(
'About',
style:
TextStyle(fontFamily: 'BalooBhai', fontSize: 20.0),
),
)),
),
InkWell(
onTap: () {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) {
return Donation();
}), ModalRoute.withName('/about'));
},
child: Card(
color: Colors.white,
margin:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
child: ListTile(
leading: Icon(
Icons.monetization_on,
color: Colors.teal[900],
),
title: Text(
'Donation',
style: TextStyle(fontSize: 20.0, fontFamily: 'Neucha'),
),
),
),
),
InkWell(
onTap: () {
signOutGoogle();
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) {
return LoginPage();
}), ModalRoute.withName('/'));
},
child: Card(
color: Colors.white,
margin:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
child: ListTile(
leading: Icon(
Icons.account_circle,
color: Colors.teal[900],
),
title: Text(
'LOGOUT',
style: TextStyle(fontSize: 20.0, fontFamily: 'Neucha'),
),
),
),
),
],
),
),
),
);
}
}
Instead of using pushAndRemoveUntil in your About page, you can use Navigator.pop(context)( which removes the view from the Navigator stack):
I added a demo using your code as an example:
class About extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('About FurBallTales'),
backgroundColor: Colors.cyanAccent[400],
leading: GestureDetector(
onTap: () {
Navigator.pop(context); // pop the view
},
child: Icon(
Icons.arrow_back,
)),
),
...
);
}
}
NOTE:
With this approach, the state of the BottomNavigationBar is reserved.
Thank you for everyone, who helped me.
The problem has been solved this way:
First, I pushed to new page.
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => About()));
}
Then made pop context.
onTap: () {
Navigator.pop(context);
},
It returned initial position.
Where it says return Homepage(); in about.dart you could change that to return the profile widget.

How to create a tab bar at center of the screen in flutter?

I'm trying to create a tab bar at the center of the screen using flutter while trying it I gave TabBarView in a column and I was stuck in this error. Please resolve this.
I/flutter ( 3983): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 3983): The following assertion was thrown during performResize():
I/flutter ( 3983): Horizontal viewport was given unbounded height.
I/flutter ( 3983): Viewports expand in the cross axis to fill their container and constrain their children to match
I/flutter ( 3983): their extent in the cross axis. In this case, a horizontal viewport was given an unlimited amount of
I/flutter ( 3983): vertical space in which to expand.
The source code is
class profilePage extends StatefulWidget {
#override
profilePageState createState() => profilePageState();
}
class profilePageState extends State<profilePage>
with SingleTickerProviderStateMixin {
TabController _tabController;
#override
void initState() {
_tabController = new TabController(length: 2, vsync: this);
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: [
Container(
child: Column(crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
height: 40,
),DefaultTabController(
length: 2,
child: Column(children: [TabBar(
unselectedLabelColor: Colors.black,
labelColor: Colors.red,
tabs: <Widget>[
Tab(
icon: Icon(Icons.people),
),
Tab(
icon: Icon(Icons.person),
)
],controller: _tabController,
indicatorSize: TabBarIndicatorSize.tab,
),TabBarView(
children: <Widget>[Text('people'), Text('Person')],
controller: _tabController,
),
]),
),
],
),
),
],
),
);
}
}
You can see the above model of the image what I'm trying to achieve. I've tried many things but I've stuck here.
How to rectify this error and how to create a tab bar at the center of my screen?
I added a demo of what you are trying to get (I followed the Image you posted):
NOTE : I had to make few changes to the way you arranged your widget tree.
class profilePage extends StatefulWidget {
#override
profilePageState createState() => profilePageState();
}
class profilePageState extends State<profilePage> {
#override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
title: Text(
'My Profile',
),
centerTitle: true,
backgroundColor: Colors.grey[700].withOpacity(0.4),
elevation: 0,
// give the app bar rounded corners
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(20.0),
bottomRight: Radius.circular(20.0),
),
),
leading: Icon(
Icons.menu,
),
),
body: Column(
children: <Widget>[
// construct the profile details widget here
SizedBox(
height: 180,
child: Center(
child: Text(
'Profile Details Goes here',
),
),
),
// the tab bar with two items
SizedBox(
height: 50,
child: AppBar(
bottom: TabBar(
tabs: [
Tab(
icon: Icon(Icons.directions_bike),
),
Tab(
icon: Icon(
Icons.directions_car,
),
),
],
),
),
),
// create widgets for each tab bar here
Expanded(
child: TabBarView(
children: [
// first tab bar view widget
Container(
color: Colors.red,
child: Center(
child: Text(
'Bike',
),
),
),
// second tab bar viiew widget
Container(
color: Colors.pink,
child: Center(
child: Text(
'Car',
),
),
),
],
),
),
],
),
),
);
}
}
OUTPUT:
To put the TabBar at the center of the screen, your Profile Container's height should be the screen height divided by 2
Like this
class profilePage extends StatefulWidget {
#override
profilePageState createState() => profilePageState();
}
class profilePageState extends State<profilePage>
with SingleTickerProviderStateMixin {
TabController _tabController;
#override
void initState() {
_tabController = new TabController(length: 2, vsync: this);
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
height: MediaQuery.of(context).size.height /2,
child: Center(child: Text("Profile"),),
color: Colors.blue,
),
TabBar(
unselectedLabelColor: Colors.black,
labelColor: Colors.red,
tabs: [
Tab(
icon: Icon(Icons.people),
),
Tab(
icon: Icon(Icons.person),
)
],
controller: _tabController,
indicatorSize: TabBarIndicatorSize.tab,
),
Expanded(
child: TabBarView(
children: [Text('people'), Text('Person')],
controller: _tabController,
),
),
],
),
),
);
}
}
Result:
Also can try this,
AppBar(
leading: IconButton(
constraints: BoxConstraints(),
padding: EdgeInsets.zero,
icon: Container(
padding: EdgeInsets.fromLTRB(10, 5, 0, 5),
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(20)),
// color: MyColors.primaryColorLight.withAlpha(20),
color: Colors.white,
),
child: Icon(
Icons.arrow_back_ios,
color: kPrimaryColor,
// size: 16,
),
),
onPressed: () => Navigator.of(context).pop(),
),
backgroundColor: kPrimaryColor,
toolbarHeight: 80,
elevation: 5.0,
title: TabBar(//Add tab bar to title
indicator: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(30)),
color: Colors.white),
labelColor: kPrimaryColor,
unselectedLabelColor: Colors.white,
indicatorSize: TabBarIndicatorSize.label,
isScrollable: true,
tabs: [
Container(
height: 30.0,
width: 100,
child: Tab(
child: Align(
alignment: Alignment.center,
child: Text(
"Ongoing",
style: TextStyle(fontSize: 16),
),
),
),
),
Container(
width: 100,
height: 30.0,
child: Tab(
child: Align(
alignment: Alignment.center,
child: Text(
"Requests",
style: TextStyle(fontSize: 16),
),
),
),
),
],
),
)