How to change the ToolBar Color for the Navigation Drawer - flutter

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

Related

Can I use one class many time just chainging Scaffold.Drawer.body?

I want to use class contain Scaffold.drawer like reusable class. I searched everyday but I can't find solution I can enough understand to apply.
Concleatly, I want to change class belong to body (part of Chart() in my code). hmmm I think It's like variable.
What can I do for it?
return Scaffold (
appBar: AppBar(title: const Text("")),
body: Chart(),
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
const DrawerHeader(
decoration: BoxDecoration(
color: Colors.blue,
),
child: Text(""),
),
ListTile(
title: const Text('#'),
onTap:(){
Navigator.push(
context, MaterialPageRoute(builder: (_) => ChartPage()));
},
),
ListTile(
title: const Text('##'),
onTap:(){
Navigator.push(
context, MaterialPageRoute(builder: (_) => NoticeBoard()));
},
),
ListTile(
title: const Text('###'),
onTap:(){},
),
]
),
),
);
Thank you.
You can create a new class named CustomDrawer.dart then add
import 'package:flutter/material.dart';
class CustomDrawer extends StatelessWidget {
const CustomDrawer({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
const DrawerHeader(
decoration: BoxDecoration(
color: Colors.blue,
),
child: Text(""),
),
ListTile(
title: const Text('#'),
onTap:(){
Navigator.push(
context, MaterialPageRoute(builder: (_) => ChartPage()));
},
),
ListTile(
title: const Text('##'),
onTap:(){
Navigator.push(
context, MaterialPageRoute(builder: (_) => NoticeBoard()));
},
),
ListTile(
title: const Text('###'),
onTap:(){},
),
]
),
);
}
}
Then in all other classes you only need to use
Scaffold(
drawer: CustomDrawer()
)
yes it's possible you can create your customDrawer widget with static keyword and just use this widget any where you want like this..
class CustomDrawer {
static Widget customDrawer(BuildContext context) {
return Drawer(
child: ListView(padding: EdgeInsets.zero, children: [
DrawerHeader(
decoration: BoxDecoration(
color: Colors.blue,
),
child: Text(""),
),
ListTile(
title: const Text('#'),
onTap: () {
// Navigator.push(
// context, MaterialPageRoute(builder: (_) => ChartPage()));
},
),
ListTile(
title: const Text('##'),
onTap: () {
// Navigator.push(
// context, MaterialPageRoute(builder: (_) => NoticeBoard()));
},
),
ListTile(
title: const Text('###'),
onTap: () {},
),
]),
);
}
}
and then other classes you only need call this widget with class name like this just like a variable..
return Scaffold(
drawer: CustomDrawer.customDrawer(context),
appBar: AppBar(
title: Text("Custom drawer screen"),
),
);

How can i implement navigation drawer under appbar in flutter

I want to implement navigation drawer in flutter like this screenshot. But don't know how.
Please give me some code hint.
Thank you.
This is the image I like to archive
use the Drawer Widget in scaffold
this is an example from the official documentation
Scaffold(
appBar: AppBar(
title: const Text('Drawer Demo'),
),
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: const <Widget>[
DrawerHeader(
decoration: BoxDecoration(
color: Colors.blue,
),
child: Text(
'Drawer Header',
style: TextStyle(
color: Colors.white,
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'),
),
],
),
),
);
this is the result =>
You have to use the Drawer widget.
Scaffold(
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
ListTile(
title: const Text('Item 1'),
onTap: (){
// do something
},
),
ListTile(
title: const Text('Item 2'),
onTap: (){
// do something
},
),
],
),
),
...
And that's pretty much it! Learn more about Drawer, here.
An easy to archive this is using another Scaffold on body and using drawer there. And to control the drawer use ScaffoldState GlobalKey.
Result
Widget
class _MyApp extends StatefulWidget {
#override
State<_MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<_MyApp> {
static final GlobalKey<ScaffoldState> _key = GlobalKey();
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
onPressed: () {
if (_key.currentState != null) {
if (_key.currentState!.isDrawerOpen) {
Navigator.pop(_key.currentContext!);
} else {
_key.currentState!.openDrawer();
}
}
},
icon: const Icon(
Icons.more,
),
),
),
body: Scaffold(
key: _key,
drawer: Drawer(
child: Container(
color: Colors.red,
),
),
body: Column(
children: const [
Text("Child"),
],
),
));
}
}

how to locate ListWheelScrollView to a given index?

I want to locate the ListWheelScrollView to a randomly given index.
While locating, animation of spin must work as well.
How can I locate to a given index?
Edit: It will be like a slot machine.
You can copy paste run full code below
You can use FixedExtentScrollController and call animateToItem
code snippet
final FixedExtentScrollController _controller = FixedExtentScrollController();
...
body: Center(
child: ListWheelScrollView(
controller: _controller,
itemExtent: 80,
magnification: 1.2,
useMagnifier: true,
physics: FixedExtentScrollPhysics(),
children: listtiles, //List of widgets
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
_controller.animateToItem(random.nextInt(10),
duration: Duration(milliseconds: 500), curve: Curves.linear);
},
child: Icon(Icons.add),
backgroundColor: Colors.green,
),
working demo
full code
import 'package:flutter/material.dart';
import 'dart:math';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final FixedExtentScrollController _controller = FixedExtentScrollController();
Random random = Random();
List<Widget> listtiles = [
GestureDetector(
onTap: () {
print("clicked 1");
},
child: ListTile(
leading: Icon(Icons.portrait),
title: Text("Portrait"),
subtitle: Text("Beautiful View..!"),
trailing: Icon(Icons.arrow_forward_ios),
),
),
GestureDetector(
onTap: () {
print("clicked 2");
},
child: ListTile(
leading: Icon(Icons.landscape),
title: Text("LandScape"),
subtitle: Text("Beautiful View..!"),
trailing: Icon(Icons.remove),
),
),
GestureDetector(
onTap: () {
print("clicked 3");
},
child: ListTile(
leading: Icon(Icons.map),
title: Text("Map"),
subtitle: Text("Map View..!"),
trailing: Icon(Icons.wb_sunny),
),
),
ListTile(
leading: Icon(Icons.landscape),
title: Text("LandScape"),
subtitle: Text("Wonderful View..!"),
trailing: Icon(Icons.wb_sunny),
),
ListTile(
leading: Icon(Icons.list),
title: Text("List Example"),
subtitle: Text("List Wheel Scroll view .!"),
trailing: Icon(Icons.cloud),
),
ListTile(
leading: Icon(Icons.settings),
title: Text("Settings"),
subtitle: Text("Change the setting..!"),
trailing: Icon(Icons.portrait),
),
ListTile(
leading: Icon(Icons.event),
title: Text("Add data"),
subtitle: Text("Data View..!"),
trailing: Icon(Icons.add),
),
ListTile(
leading: Icon(Icons.landscape),
title: Text("LandScape"),
subtitle: Text("Beautiful View..!"),
trailing: Icon(Icons.wb_sunny),
),
ListTile(
leading: Icon(Icons.email),
title: Text("Email"),
subtitle: Text("Check Email..!"),
trailing: Icon(Icons.arrow_forward),
),
ListTile(
leading: Icon(Icons.games),
title: Text("Games"),
subtitle: Text("Play Games..!"),
trailing: Icon(Icons.zoom_out_map),
),
];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("ListView ScrollView Wheel"),
),
body: Center(
child: ListWheelScrollView(
controller: _controller,
itemExtent: 80,
magnification: 1.2,
useMagnifier: true,
physics: FixedExtentScrollPhysics(),
children: listtiles, //List of widgets
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
_controller.animateToItem(random.nextInt(10),
duration: Duration(milliseconds: 500), curve: Curves.linear);
},
child: Icon(Icons.add),
backgroundColor: Colors.green,
),
);
}
}

change drawer item icon to right flutter

I used drawer in the flutter, and I user end drawer to change the direction the drawer,
but I need also change the direction of drawer item icon to right flutter, how can I do it?
You can achieve this using ListTile widgets
Try this way
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() => runApp( HomeApp());
class HomeApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
// TODO: implement build
return new MaterialApp(
theme: new ThemeData(
primarySwatch: Colors.deepPurple,
brightness: Brightness.light,
accentColor: Colors.red),
home: new HomePage(),
);
}
}
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"),
),
endDrawer: 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"),
),
otherAccountsPictures: <Widget>[
CircleAvatar(
backgroundColor: Colors.white,
child: Text("Pilu"),
),
],
),
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: Center(
child: Text("Home Screen"),
),
);
}
}
SAMPLE CODE
Make use of ListTile trailing property
ListTile(
title: Text("text"),
trailing: Icon(Icons.account),
onTap:null,
),

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/