How can I make BottomNavigationBar NOT stick on top of keyboard flutter? - flutter

So I wanted to make the sticking of BottomNavigationBar on the keyboard stop.
I have tried:
resizeToAvoidBottomPadding: false; (or true both not working),
The problem with the bottomnavigationbar sticking to my keyboard is when I try to type something, the suggestions are not clearly visible and also it has a nested app bar on another page, which in-turn make it very difficulty to type and see the suggestions.
Here is my code:
#override
void initState() {
super.initState();
getUser();
//PostController().getUser(widget.auth.getCurrentUser());
pages = [Home(), Search(), NewPostPage(), Notifications(), Profile()];
}
#override
Widget build(BuildContext context) {
return new Scaffold(
resizeToAvoidBottomPadding: true,
body: Home(context),
);
}
Widget Home(context) {
var bottomOptions = <BottomNavigationBarItem>[];
for (var i = 0; i < widget.bottomItems.length; i++) {
var d = widget.bottomItems[i];
bottomOptions.add(
new BottomNavigationBarItem(
icon: d.icon,
title: Padding(
padding: const EdgeInsets.fromLTRB(3, 3, 3, 0),
child: new Text(d.title, style: TextStyle(fontSize: 12.0)),
),
),
);
}
return Scaffold(
appBar: AppBar(
title: Text(title),
leading: Container(
alignment: Alignment.centerLeft,
child: IconButton(
icon: Icon(Icons.motorcycle),
onPressed: () {
Navigator.push(
context,
PageRouteBuilder(
pageBuilder: (context, anim1, anim2) => Finder(),
transitionsBuilder: (context, anim1, anim2, child) =>
FadeTransition(opacity: anim1, child: child),
transitionDuration: Duration(seconds: 0),
));
}),
),
body: isLoading
? CollectionScaleTransition(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(2.0),
child: Icon(Icons.add_circle, size: 10, color: Colors.blue),
),
Padding(
padding: const EdgeInsets.all(2.0),
child: Icon(Icons.add_circle, size: 10, color: Colors.blue),
),
Padding(
padding: const EdgeInsets.all(2.0),
child: Icon(Icons.add_circle, size: 10, color: Colors.blue),
),
],
)
: PageView(
controller: pageController,
children: pages,
onPageChanged: onPageChanged,
physics: NeverScrollableScrollPhysics(),
),
bottomNavigationBar: BottomNavigationBar(
showUnselectedLabels: true,
items: bottomOptions,
currentIndex: currentIndex,
selectedFontSize: 9,
unselectedFontSize: 9,
type: BottomNavigationBarType.fixed,
onTap: (index) {
setState(() {
onTap(index);
currentIndex = index;
});
},
),
);
}
}
How to make it stop appearing when I try to type on my text-field?
Thank you in Advance!

resizeToAvoidBottomPadding property is deprecated. We need to use resizeToAvoidBottomInset instead. More details here.
Also, I tried to recreate your case but was unable to replicate the issue you mentioned. I took some of your code and used it my sample, as below:
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: Text('test'),
leading: Container(
alignment: Alignment.centerLeft,
child: IconButton(
icon: Icon(Icons.motorcycle),
onPressed: () {}),
)),
bottomNavigationBar: BottomNavigationBar(
currentIndex: 0, // this will be set when a new tab is tapped
items: [
BottomNavigationBarItem(
icon: new Icon(Icons.home),
title: new Text('Home'),
),
BottomNavigationBarItem(
icon: new Icon(Icons.mail),
title: new Text('Messages'),
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
title: Text('Profile')
)
],
),
body: Center(
child: TextField(
keyboardType: TextInputType.text,
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Enter a search term'
),
))
);
With above code, when I tap on textfield, the bottom nav didn't show and I was able to type in properly.
Hope this helps.

Related

Show Bottom Modal Sheet above the Bottom Navigation Bar

Here in the below image I have added showModalBottomSheet on clicking on the item of "Bottom Navigation Bar", but the BottomNavigationBar is hidden by the modal sheet, So I want to make it visible even the bottom sheet is present. Can anyone please anyone help me out.
This is my bottom navigation code:
Widget _bottomNavigationBar() {
return BottomNavigationBar(
type: BottomNavigationBarType.fixed,
key: scaffoldState,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.branding_watermark_outlined),
label: 'Brands',
),
BottomNavigationBarItem(
icon: Icon(Icons.category),
label: 'Category',
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Profile',
)
],
currentIndex: _selectedIndex,
selectedItemColor: AppColors.blue,
onTap: (newIndex) => {
if (newIndex == 1)
{showBrandsBottomSheet(context)}
else if (newIndex == 2)
{showCategoryBottomSheet(context)}
else
{
setState(() {
_selectedIndex = newIndex;
})
}
});
}
Here is my code for bottom model sheet:
showBrandsBottomSheet(BuildContext context) {
return showModalBottomSheet<void>(
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15.0), topRight: Radius.circular(15.0)),
),
context: context,
useRootNavigator: true,
isScrollControlled: true,
builder: (BuildContext _) {
return Container(
color: Colors.white,
height: MediaQuery.of(context).size.height / 2,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 5, top: 3),
child: GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: const Align(
alignment: Alignment.topRight,
child: Icon(Icons.close)),
),
),
Container(
color: Colors.white,
height: 350,
child: ListView.builder(
key: Key('builder ${_selected.toString()}'), //a
scrollDirection: Axis.vertical,
// shrinkWrap: true,
// physics: NeverScrollableScrollPhysics(),
itemCount: brandList.length,
itemBuilder: (BuildContext context, int index) {
return Theme(
data: Theme.of(context)
.copyWith(dividerColor: Colors.white),
child: ExpansionTile(
// tilePadding: const EdgeInsets.all(0),
key: Key(index.toString()), //attention
initiallyExpanded: index == _selected, //attention
collapsedIconColor: Colors.blue,
iconColor: Colors.blue,
backgroundColor: Colors.white,
title: Text(
brandList[index],
style: const TextStyle(
fontSize: 13.0,
color: Colors.black,
fontWeight: FontWeight.w600),
),
children: <Widget>[
Container(
color: Colors.blue[50],
child: Column(
children: _buildExpandableBrands(brandList),
),
),
],
onExpansionChanged: ((newState) {
if (newState) {
setState(() {
const Duration(seconds: 20000);
_selected = index;
});
} else {
setState(() {
_selected = -1;
});
}
}),
));
},
),
),
],
),
),
);
},
);
}
set useRootNavigator=true.This will display model sheet above all other content.
showModalBottomSheet(
context: context,
isScrollControlled: true,
useRootNavigator: true,
builder: (context) {
return BottomBarView(
);
});
Finally, this question helped me in getting this done.
Here is my working code:
Widget _bottomNavigationBar() {
return BottomNavigationBar(
type: BottomNavigationBarType.fixed,
key: scaffoldState,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.branding_watermark_outlined),
label: 'Brands',
),
BottomNavigationBarItem(
icon: Icon(Icons.category),
label: 'Category',
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Profile',
)
],
currentIndex: _selectedIndex,
selectedItemColor: AppColors.blue,
onTap: (newIndex) => {
if (newIndex == 1)
{
_scaffoldKey.currentState?.showBottomSheet((_) => Container(
child: showBrandsBottomSheet(),
))
}
else if (newIndex == 2)
{
{
_scaffoldKey.currentState?.showBottomSheet((_) => Container(
child: showCategoryBottomSheet(),
))
}
}
else
{
setState(() {
_selectedIndex = newIndex;
})
}
});
}
showBrandsBottomSheet:
showBrandsBottomSheet() {
return Container(
color: Colors.white,
height: MediaQuery.of(context).size.height / 2,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 5, top: 3),
child: GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: const Align(
alignment: Alignment.topRight, child: Icon(Icons.close)),
),
),
Container(
color: Colors.white,
height: 350,
child: ListView.builder(
key: UniqueKey(),
scrollDirection: Axis.vertical,
itemCount: brandList.length,
itemBuilder: (BuildContext context, int index) {
cardKeyList.add(GlobalKey(debugLabel: "index :$index"));
return Theme(
data: Theme.of(context)
.copyWith(dividerColor: Colors.white),
child: ExpansionTileCard(
key: cardKeyList[index],
initiallyExpanded: false,
title: Text(
brandList[index],
style: const TextStyle(
fontSize: 13.0,
color: Colors.black,
fontWeight: FontWeight.w600),
),
children: <Widget>[
Container(
color: Colors.blue[50],
child: Column(
children: _buildExpandableBrands(brandList),
),
),
],
onExpansionChanged: (value) {
if (value) {
Future.delayed(const Duration(milliseconds: 200),
() {
for (var i = 0; i < cardKeyList.length; i++) {
if (index != i) {
cardKeyList[i].currentState?.collapse();
}
}
});
}
},
));
},
),
),
],
),
),
);
}
Now it is started showing above the bottom navigation.

Reduce space between Tabbar and Listtile

I want to reduce the space between Tabbar and listtile. I wrote them in two different files so i'll add both the codes. tried adding padding but no use.
main.dart
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
themeMode: ThemeMode.system,
home: DefaultTabController(
length: 4,
child: NestedScrollView(
headerSliverBuilder: (context, innerBoxIsScrolled) => [
SliverAppBar(
pinned: true,
floating: true,
systemOverlayStyle: SystemUiOverlayStyle.dark,
title: const Text(
"WhatsApp",
style: TextStyle(letterSpacing: 1.0),
),
backgroundColor: colour,
actions: [
MaterialButton(
minWidth: 1.0,
onPressed: () {},
child: const Icon(
Icons.search,
color: Colors.white,
)),
MaterialButton(
minWidth: 1.0,
onPressed: () {},
child: const Icon(
Icons.more_vert,
color: Colors.white,
))
],
bottom: const TabBar(
dragStartBehavior: DragStartBehavior.start,
indicatorColor: Colors.white,
tabs: [
Tab(
iconMargin: EdgeInsets.only(left: 1.0, right: 1.0),
icon: Icon(Icons.photo_camera),
),
Tab(child: Text("CHATS")),
Tab(child: Text("STATUS")),
Tab(child: Text("CALLS"))
]))
],
body: const TabBarView(
dragStartBehavior: DragStartBehavior.start,
children: [Camera(), Chats(), Status(), Calls()]),
),
));
}
}
chats.dart
class _ChatsState extends State<Chats> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.only(top: 1.0),
child: ListView.separated(
itemBuilder: (context, index) => ListTile(
title: Text("Person $index"),
subtitle: Text("your message is this"),
trailing: Text("$hr:$mi $ti"),
leading: MaterialButton(
padding: EdgeInsets.all(1.0),
height: 1.0,
minWidth: 1.0,
elevation: 0,
onPressed: () {},
shape: CircleBorder(),
child: CircleAvatar(
radius: 25.0,
backgroundImage: AssetImage('assets/images/images.jfif'),
),
),
),
separatorBuilder: (context, index) {
return Divider();
},
itemCount: 50),
),
floatingActionButton: FloatingActionButton(
backgroundColor: colour,
onPressed: () {},
child: Icon(Icons.chat_rounded),
),
);
}
}
try padding: EdgeInsets.zero inside ListView.separated, not inside ListTile

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

How can I keep showing bottomNavigationBar in nested page?

there is shopping app, and there is 4 items in bottomNavBar.
in home page, fetch all categories and shown in page, when pressed on category, he should see the products in category. but how can I keep bottom navigation bar when I want to navigate him into next pages?
this is myBottomNavigationBar code:
Scaffold(
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
actions: [
IconButton(
icon: Icon(
Icons.search,
color: Constants.blackBackgroundColor,
),
onPressed: () {})
],
),
body: IndexedStack(
index: _mainController.tabIndex,
children: [
HomePage(),
OfferPage(),
CartPage(),
ProfilePage(),
],
),
bottomNavigationBar: bottomNavigationBar(context),
);
Don't know if this is what you're looking for but here is one way of achieving it.
List<dynamic> _screens = [ HomePage(), OfferPage(),CartPage(), ProfilePage()];
int _index = 0;
PageController controller = PageController();
Scaffold(
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
actions: [
IconButton(
icon: Icon(
Icons.search,
color: Constants.blackBackgroundColor,
),
onPressed: () {})
],
),
body: PageView.builder(
itemCount: 4,
controller: controller,
onPageChanged: (page) {
setState(() {
_index = page;
});
},
itemBuilder: (context, position) {
return Container(
color: Colors.white,
child: Center(child: _screens[position]),
);
}),
bottomNavigationBar: bottomNavigationBar(context),
);

My app closed when i pressed back from navigation drawer selected item in flutter

I have navigation drawer, i select one item from it and after i select second item when i pressed back after selecting second item app got closed i need to go on first item when i pressed back .
#override
Widget build(BuildContext context) {
List<Widget> drawerOptions = [];
for (var i = 0; i < drawerItems.length; i++) {
var d = drawerItems[i];
drawerOptions.add(new ListTile(
leading: new Icon(d.icon),
title: new Text(
d.title,
style: new TextStyle(fontSize: 14.0, fontWeight: FontWeight.w400),
),
selected: i == _selectedIndex,
onTap: () => _onSelectItem(i),
));
}
return new Scaffold(
appBar: SearchBar(
loader: QuerySetLoader<ProductModel>(
querySetCall: _getItemListForQuery,
itemBuilder: _buildItemWidget,
loadOnEachChange: true,
animateChanges: true,
),
defaultBar: AppBar(
title: Text('Home'),
),
),
drawer: new Drawer(
child: SingleChildScrollView(
child: new Column(
children: <Widget>[
DecoratedBox(
position: DecorationPosition.background,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/bac_image.png'),
fit: BoxFit.cover),
),
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(20.0),
child: new Image(
image: new AssetImage("assets/blik_mobile.png"),
height: 100,
width: 100,
),
),
Column(children: drawerOptions)
],
),
)
],
),
),
),
body: _getDrawerItemScreen(_selectedIndex),
bottomNavigationBar: BottomNavigationBar(
onTap: (int index) {
setState(() {
this.index = index;
// Navigator.of(context).pop();
});
_navigateToScreens(index);
},
type: BottomNavigationBarType.fixed,
currentIndex: index,
items: [
BottomNavigationBarItem(
title: Text('Home'),
icon: Icon(
Icons.home,
color: Colors.black,
)),
BottomNavigationBarItem(
title: Text('Categories'),
icon: Icon(Icons.dashboard, color: Colors.black)),
BottomNavigationBarItem(
title: Text('Cart'),
icon: Icon(Icons.shopping_cart, color: Colors.black)),
BottomNavigationBarItem(
title: Text('WishList'),
icon: Icon(Icons.favorite, color: Colors.black)),
BottomNavigationBarItem(
title: Text('Profile'),
icon: Icon(Icons.person, color: Colors.black)),
],
),
);
}
on select item of drawer
_onSelectItem(int index) {
setState(() {
_selectedIndex = index;
_getDrawerItemScreen(_selectedIndex);
});
Navigator.of(context).pop();
}
get selected drawer screen method is here
_getDrawerItemScreen(int pos) {
switch (pos) {
case 0:
return new FirstScreen(drawerItem: drawerItems[_selectedIndex]);
case 1:
return new OrderHistory(drawerItem: drawerItems[_selectedIndex]);
case 2:
return new WalletScreen(
drawerItem: drawerItems[_selectedIndex],
);
case 3:
return new AddressList(drawerItem: drawerItems[_selectedIndex]);
case 5:
return new AboutUs(drawerItem: drawerItems[_selectedIndex]);
// return new AddAddress(drawerItem: drawerItems[_selectedIndex],);
default:
return new FirstScreen(drawerItem: drawerItems[_selectedIndex]);
}
}
i want to handle back after selecting any of item from my navigation drawer .
If you need to control screen stack, you can use
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
}
detail reference https://medium.com/flutter-community/flutter-push-pop-push-1bb718b13c31
If you want to handle back button
you can wrap your Scaffold with WillPopScope
return WillPopScope(
onWillPop: () => _exitApp(context),
child: Scaffold(
appBar: AppBar(
title: Text("Navigation Demo"),
and ask user Do you want to exit this application or current screen
Future<bool> _exitApp(BuildContext context) {
return showDialog(
context: context,
child: AlertDialog(
title: Text('Do you want to exit this application?'),
content: Text('We hate to see you leave...'),
actions: <Widget>[
FlatButton(
onPressed: () => Navigator.of(context).pop(false),
child: Text('No'),
),
FlatButton(
onPressed: () => Navigator.of(context).pop(true),
child: Text('Yes'),
),
],
),
) ??
false;
}
detail reference https://codingwithjoe.com/flutter-navigation-how-to-prevent-navigation/