Why I'm getting two app bar in home screen in flutter? - flutter

I have made multiple screens and these screen have separate appBar in flutter but my home Screen getting two appBar! How can remove it in flutter?
I have tried several things but I'm still getting two screen.
Here is image link
enter image description here
Home Screen And Category Screen have same appBar code
Here Is Code of Home Screen appBar
appBar: AppBar(
title: const Text('Online Survey'),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.search),
onPressed: () {
showSearch(context: context, delegate:CustomSearchDelegate(),);
},
),
IconButton(
onPressed: () {
showModalBottomSheet(
context: context,
builder: (context) => DraggableScrollableSheet(
expand: false,
builder: (context, scrollController) =>
SingleChildScrollView(
child: Column(
children: [
ListTile(
leading: const Icon(Icons.color_lens),
title: const Text('Dark Theme'),
subtitle: const Text(
'Better for eyesight and battery life'),
trailing: IconButton(
onPressed: () {},
icon: const Icon(Icons.toggle_off_rounded)),
),
const ListTile(
leading: Icon(Icons.restaurant_menu),
title: Text('Display Item'),
subtitle: Text('List(small item)'),
),
const ListTile(
leading: Icon(Icons.lock),
title: Text('Privacy Policy'),
subtitle: Text('App Terms & Policy'),
),
const ListTile(
leading: Icon(Icons.rate_review),
title: Text('Rate Us'),
subtitle: Text('Leave a review on the Google Play'),
),
const ListTile(
leading: Icon(Icons.more),
title: Text('More Apps'),
subtitle: Text('More Apps form developer'),
),
const ListTile(
leading: Icon(Icons.info_rounded),
title: Text('About'),
subtitle:
Text('App Info, Build Version, Copyright'),
),
],
),
//sheetButtons(),
),
),
shape: const RoundedRectangleBorder(
borderRadius:
BorderRadius.vertical(top: Radius.circular(30))),
isScrollControlled: true,
isDismissible: true,
// barrierColor: Colors.blue.shade100,
enableDrag: true,
elevation: 7,
);
},
icon: const Icon(Icons.more_vert)),
],
primary: true,
),

Please post the entire Scaffold code if you need to pinpoint the anomaly.
The code you shared isn't enough to find the bug since its above this tree level.
However from my experience,
Guess 1: You have implemented Home screen with AppBar and Category is a sub widget of Home screen and has its AppBar.
Guess 2:
You have implemented AppBar in appBar and body of Scaffold.
Do check and update your question. Thanks!

You are using the Bottom Navigation which is causing the issue. The Bottom navigation page contains a Scaffold with an AppBar and the other screen you are using has a separate AppBar. To resolve the issue you either need to remove the AppBar from the Bottom Navigation Screen or remove the AppBar from the individual screen.

I think you Extracted Widgets from root widget's body!
Example:
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Online Survey'),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.search),
onPressed: () {
//showSearch(context: context, delegate:CustomSearchDelegate(),);
},
),
IconButton(
onPressed: () {
showModalBottomSheet(
context: context,
builder: (context) => DraggableScrollableSheet(
expand: false,
builder: (context, scrollController) =>
SingleChildScrollView(
child: Column(
children: [
ListTile(
leading: const Icon(Icons.color_lens),
title: const Text('Dark Theme'),
subtitle: const Text(
'Better for eyesight and battery life'),
trailing: IconButton(
onPressed: () {},
icon: const Icon(Icons.toggle_off_rounded)),
),
const ListTile(
leading: Icon(Icons.restaurant_menu),
title: Text('Display Item'),
subtitle: Text('List(small item)'),
),
const ListTile(
leading: Icon(Icons.lock),
title: Text('Privacy Policy'),
subtitle: Text('App Terms & Policy'),
),
const ListTile(
leading: Icon(Icons.rate_review),
title: Text('Rate Us'),
subtitle: Text('Leave a review on the Google Play'),
),
const ListTile(
leading: Icon(Icons.more),
title: Text('More Apps'),
subtitle: Text('More Apps form developer'),
),
const ListTile(
leading: Icon(Icons.info_rounded),
title: Text('About'),
subtitle:
Text('App Info, Build Version, Copyright'),
),
],
),
//sheetButtons(),
),
),
shape: const RoundedRectangleBorder(
borderRadius:
BorderRadius.vertical(top: Radius.circular(30))),
isScrollControlled: true,
isDismissible: true,
// barrierColor: Colors.blue.shade100,
enableDrag: true,
elevation: 7,
);
},
icon: const Icon(Icons.more_vert)),
],
primary: true,
),
body: Exc(), ////here is the main body of widgets.
);
}
}
class Exc extends StatelessWidget {
const Exc({
super.key,
});
#override
Widget build(BuildContext context) {
////you returned another Scaffold and an appBar! this caused another
////appbar.
////return something else instead of Scaffold.
return Scaffold(
appBar: AppBar(),
body: ListView.builder(
itemBuilder: (context, index) => const Padding(
padding: EdgeInsets.all(8),
child: Text('Testing Chats'),
),
itemCount: 10),
);
}
}
Output:

Related

Custom widget of app bar with a humburger menu as ElevatedButton with a Drawer doesn't work OnPressed

I am facing a problem with my custom app bar. I would like it to have a drawer but it doesn't work. I just don't know how to make it being able to open.
It is a custom app bar widget named Pasek (which stands for top bar in my language).
It has a logo picture which on click moves you to the first page of the app.
Next to it there is humburger menu with a drawer but I do not know why it doesn't work. I click on it and nothing happens.
Any ideas?
class Pasek extends StatelessWidget with PreferredSizeWidget {
#override
Size get preferredSize => Size.fromHeight(50.0);
const Pasek({
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return AppBar(
actions: <Widget>[
Padding(
padding: EdgeInsets.only(right: 35.0),
child: ElevatedButton(
child: Icon(Icons.menu, color: Colors.white),
onPressed: () {
Scaffold(
endDrawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: const <Widget>[
DrawerHeader(
decoration: BoxDecoration(
color: Colors.black,
),
child: Text(
'Menu',
style: TextStyle(
color: Colors.pink,
fontSize: 24,
),
),
),
ListTile(
leading: Icon(Icons.message),
title: Text('Messages'),
),
ListTile(
leading: Icon(Icons.account_circle),
title: Text('Profile'),
),
ListTile(
leading: Icon(Icons.settings),
title: Text('Settings'),
),
],
),
),
);
},
)),
],
backgroundColor: Colors.black,
title: GestureDetector(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => FirstPage(),
),
);
}, // Image tapped
child: Image.asset(
'images/logo.png',
fit: BoxFit.scaleDown,
height: 30,
width: 200,
),
));
}
}

Switch screen in flutter without context in flutter

I have this dart file(app_bar.dart) and am storing Appbars inside it and i have logged_home.dart file where am calling the app_bar.dart from. Now i want to be able to navigate to a new screen when i click next screen it sends me to PostData() while in app_bar.dart.
app_bar.dart:
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:kopala_dictionary/screens/authenticate/login.dart';
import 'package:kopala_dictionary/screens/author/post_data.dart';
import 'package:kopala_dictionary/screens/home/unlogged_home.dart';
//for logged in user
final loggedBar = AppBar(
title: Text('Kopalaz Dictionary'),
backgroundColor: Colors.green,
elevation: 0.0,
actions: <Widget>[
FlatButton.icon(
color: Colors.white,
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => PostData()));
},
icon: Icon(Icons.add),
label: Text(
'Post',
style: TextStyle(color: Colors.white),
)),
],
);
//For unlogged users
final unloggedBar = AppBar(
title: Text('Kopalaz Dictionary'),
backgroundColor: Colors.green,
elevation: 0.0,
);
logged_home.dart:
import 'package:flutter/material.dart';
import 'package:kopala_dictionary/screens/wrapper.dart';
import 'package:kopala_dictionary/services/auth.dart';
import 'package:kopala_dictionary/shared/app_bar.dart';
class LoggedInUserHome extends StatelessWidget {
final AuthService _auth = AuthService();
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.green[10],
appBar: loggedBar,
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text(
'Kopalationary Menu',
style: TextStyle(color: Colors.white),
),
decoration: BoxDecoration(
color: Colors.green,
),
),
ListTile(
title: Text('Home'),
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => Wrapper()));
},
),
ListTile(
title: Text(
'My profile',
),
onTap: () {},
),
ListTile(
title: Text('Logout'),
onTap: () async {
dynamic result = await _auth.logoutUser();
},
),
ListTile(
title: Text(
'About',
),
onTap: () {},
),
],
),
),
body: Center(
child: Text('Development in progress!'),
),
);
}
}
do not forget that in Flutter everything is a widget !
So my advice would be to create functions which returns a widget:
import 'package:flutter/material.dart';
//for logged in user
AppBar loggedBar(BuildContext context) {
return AppBar(
title: Text('Kopalaz Dictionary'),
backgroundColor: Colors.green,
elevation: 0.0,
actions: <Widget>[
FlatButton.icon(
color: Colors.white,
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => PostData()));
},
icon: Icon(Icons.add),
label: Text(
'Post',
style: TextStyle(color: Colors.white),
)),
],
);
}
//For unlogged users
AppBar unloggedBar(BuildContext context) {
return AppBar(
title: Text('Kopalaz Dictionary'),
backgroundColor: Colors.green,
elevation: 0.0,
);
}
Do keep in mind that flutter does a lot in the background and it is therefore better practice not to store widget in global variable. If you need to access those widgets from different part of your app you should look into Inherited Widget or other form of state management (see list here).
see below link:
Navigate without context in Flutter with a Navigation Service
https://medium.com/flutter-community/navigate-without-context-in-flutter-with-a-navigation-service-e6d76e880c1c
also, you can use GetX library

How to change the ToolBar Color for the Navigation Drawer

How can I change the toolbar color for the Navigation drawer.
Just add padding: EdgeInsets.all(0.0), in your listview widget inside Drawer widget
Try this
class HomePage extends StatefulWidget {
#override
_HomePageScreen createState() => _HomePageScreen();
}
class _HomePageScreen extends State<HomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Home"),
),
drawer: Drawer(
child: ListView(
padding: EdgeInsets.all(0.0),
children: <Widget>[
UserAccountsDrawerHeader(
accountName: Text("Nilesh Rathod"),
accountEmail: Text("nilesh#gmail.com"),
currentAccountPicture: CircleAvatar(
backgroundColor: Colors.white,
child: Text("Nilu"),
),
),
ListTile(
title: Text("Home"),
trailing: Icon(Icons.new_releases),
),
Divider(),
ListTile(
title: Text("Profile"),
trailing: Icon(Icons.person),
onTap: () => {},
),
Divider(),
ListTile(
title: Text("Tab Layout"),
trailing: Icon(Icons.person),
onTap: () => {},
),
Divider(),
ListTile(
title: Text("Comman View Demo"),
trailing: Icon(Icons.person),
onTap: () => {},
),
Divider(),
ListTile(
title: Text("Close"),
trailing: Icon(Icons.close),
onTap: () => Navigator.of(context).pop(),
),
],
),
),
body: CachedNetworkImage(
imageUrl: 'https://i.stack.imgur.com/K8FFo.jpg?s=328&g=1',
placeholder: (context, url) => CircularProgressIndicator(), //<= ends here
errorWidget: (context, url, error) => Icon(Icons.error)),
);
}
}
OUTPUT
With padding: EdgeInsets.all(0.0),
Without padding: EdgeInsets.all(0.0),

How can I make BottomNavigationBar NOT stick on top of keyboard 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.

WillPopScope not working after using Drawer

I am using drawer of my flutter app .I register first then redirect to Home Page .I have used drawer in my Home Page . I added some some menu on drawer then redirect to some new activity . I have added WillPopScope in Home page to tigged android back press .But problem is when i clicked back button from other activity but home activity WillPopScope calling.
This is home page
return WillPopScope(
onWillPop: (){
exit(0);
},
child: MaterialApp(
home: Scaffold(
drawer: StudentDrawer(
number: widget.number,
),
appBar: AppBar(
actions: <Widget>[
InkWell(
child: new Icon(
Icons.notifications,
color: Colors.white,
),
onTap: () {},
),
InkWell(
child: Icon(
Icons.more_vert,
color: Colors.white,
),
onTap: () {
_scaffoldKey.currentState.openDrawer();
},
),
],
title: Text(
"Home",
style: new TextStyle(
color: Colors.white,
letterSpacing: 1.0,
fontSize: 20,
fontStyle: FontStyle.normal),
),
),
body: SingleChildScrollView(
child: new Column(
children: <Widget>[
//top(),
/*SizedBox(
height: 7,
),*/
bottom()
],
),
),
),
),
);
This is drawer page
return Drawer(
child: Column(
children: <Widget>[ header(), Expanded(child: list(context))],
),
);
Widget list(BuildContext context) => new ListView(
scrollDirection: Axis.vertical,
shrinkWrap: true,
children: <Widget>[
ListTile(
onTap: (){
Navigator.pop(context);
Navigator.of(context).push(new MaterialPageRoute(builder: (context)=>Profile(number:widget.number)));
},
leading: new Icon(
Icons.perm_identity,
color: Colors.green,
),
title: Text(
"Profile",
style: TextStyle(color: Colors.green),
),
),
And This is my profile activity
return WillPopScope(
onWillPop:(){
print(" It's not calling .....");
Navigator.pop(context);
}
,
child: MaterialApp(
home: Scaffold(
appBar: AppBar(
leading: InkWell(
onTap: (){
Navigator.pop(context);
},
child:Icon(Icons.arrow_back,color: Colors.white,),
),
title: Text("Profile"),
),
Basically everything fine but problem is when i click back button from profile activity its calling onWillPop of Home activity ..