Positioning in list tile drawer flutter? - flutter

I have this drawer in a flutter app. The List Tile has a leading, title and a trailing. I used an icon button in the leading in the first tile as opposed to using asset image in other tiles, so the alignment got a little distorted. How can I make it align with rest of the tiles. This is the code of the first tile:
SizedBox(
height: 40,
child: ListTile(
leading: IconButton(
onPressed: () {},
icon: const Icon(Icons.notifications_none_sharp,
color: Colors.white),
),
title: Transform.translate(
offset: const Offset(-14, 0),
child: const Text('Notifications',
style: TextStyle(
fontSize: 12, color: Colors.white))),
trailing: SizedBox(
),
other list tiles code :
SizedBox(
height: 40,
child: ListTile(
leading: SizedBox(
height: 20,
width: 20,
child: Image.asset(
'assets/sideMenu/Group.png',
),
),
title: Transform.translate(
offset: const Offset(-14, 0),
child: const Text(
'Grid',
style: TextStyle(
fontSize: 12,
color: Colors.white,
),
),
),
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => const BlurGrid()));
},
),
),
I just want the notification icon to align with other leading asset images:

just replace the IconButton with an Icon wrapped in a GestureDetector and then add padding if required
SizedBox(
height: 40,
child: ListTile(
leading: GestureDetector(
onTap: () {} ,
child: const Icon(
Icons.notifications_none_sharp,
color: Colors.white,
),
),
title: Transform.translate(
offset: const Offset(-14, 0),
child: const Text('Notifications',
style: TextStyle(fontSize: 12, color: Colors.white))),
trailing: SizedBox(),
),
),

that caused by IconButton.
if you want to keep use the IconButton, then you have to remove the padding and reset the constrain.
ListTile(
leading: IconButton(
onPressed: () {},
icon: Icon(Icons.abc),
padding: EdgeInsets.zero, // remove the padding
constraints: BoxConstraints(), // reset the constarins
),
title: Text("abcdef"),),

Related

How to add image for buttons in flutter

I am trying to add image for buttons in flutter like below the image. Here i have used ElevatedButton. So How to set background image for the ElevatedButton. I do not know how to add image for it. If anyone know please help to find the solution.
child:SingleChildScrollView(
child: Wrap(
alignment: WrapAlignment.center,
runSpacing: 20.0, // Or more
spacing: 20, // Or more
children: [
const SizedBox(
height: 520,
),
SizedBox(
width: double.infinity, // <-- Your width
height: 50, // <-- Your height
),
SizedBox(
height: 50, // <-- Your height
child: ElevatedButton(
onPressed: () {
onSignIn(context);
},
style: ElevatedButton.styleFrom(
primary: Color(0xff557de3),
shape: StadiumBorder()
),
child: const Text(
"GMAIL",
style: TextStyle(
color: Colors.white,
fontSize: 13,
fontWeight: FontWeight.bold
),
),
),
),
SizedBox(
height: 50, // <-- Your height
child: ElevatedButton(
onPressed: () {
//validateForm();
},
style: ElevatedButton.styleFrom(
primary: Color(0xff557de3),
shape: StadiumBorder()
),
child: const Text(
"Phone",
style: TextStyle(
color: Colors.white,
fontSize: 13,
fontWeight: FontWeight.bold
),
),
),// Button
),
],
),
),
You can use:
ElevatedButton.icon(
onPressed: (){},
icon: Icon(Icons.phone),
label: Text("hello"),
),
You can use gestures detector
GestureDetector(
onTap: () {
debugPrint('The image button has been tapped');
},
child: SizedBox(
width: 300,
height: 100,
child: Image.network(
'https://picsum.photos/250?image=9',
fit: BoxFit.cover,
),
),
),
or use icon button
IconButton(
splashRadius: 100,
iconSize: 200,
icon: Ink.image(
image: const NetworkImage(
'https://picsum.photos/250?image=9'),
),
onPressed: () {
// do something when the button is pressed
debugPrint('Hi there');
},
),
You can add image from asset:
ElevatedButton(
onPressed: () {},
child: Image.asset('your_asset_path')
)
Or you can add network image:
ElevatedButton(
onPressed: () {},
child: Image.network('your_image_url_path')
)
Or you can create your custom button:
GestureDetector(
onTap: () {},
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('your_asset_image_path'),
),
),
)
)
Instead of using the ElevatedButton, have you tried IconButton, GestureDetector, InkWell, InkResponse and Material?
IconButton(
splashRadius: 100, // optional
onPressed: {YOUR_LOGIC_COMPONENT}
icon: Container(
child : {YOUR_WIDGET},
),
),
or
GestureDetector(
child: SizedBox(child: {YOUR_WIDGET}),
onTap: {YOUR_LOGIC_COMPONENT},
);
or
InkWell(
child: CircleAvatar(child: {YOUR_WIDGET}),
onTap: {YOUR_LOGIC_COMPONENT},
);
You can use MaterialButton and customize it according to your need.
MaterialButton(
padding: EdgeInsets.all(8.0),
textColor: Colors.white,
splashColor: Colors.red,
elevation: 8.0,
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(''), //some asset image
fit: BoxFit.cover),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox(),
),
),
// ),
onPressed: () {
print('Tapped');
},
),

Container widget overflows other widgets- Flutter

This is the expected screen and the container will collapse and expand based on the text displayed and should only occupy the space left out by placing other icons.
But see the flutter implemented screen.
The icons are moved to the right on container expansion and shows overflowed pixel error.
My code
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
icon: const Icon(
Icons.menu,
color: Colors.black,
size: 34,
),
onPressed: () {},
),
//container section
GestureDetector(
child: Container(
margin: const EdgeInsets.only(left: 10, right: 10),
decoration: BoxDecoration(
color: Colors.red.shade100,
borderRadius: BorderRadius.circular(40.0)),
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
child: Align(
alignment: Alignment.centerLeft,
child: Row(children: const <Widget>[
Text(
"Content is here",
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Colors.black54,
fontSize: 19.0,
fontWeight: FontWeight.bold),
),
Icon(
Icons.arrow_drop_down,
color: Colors.black,
),
]),
),
),
),
onTap: () {
//todo show bottom sheet dialog here.
},
),
const Spacer(), // I just added one line
IconButton(
icon: Image.asset('assets/images/ic_scan.png'),
iconSize: 20,
onPressed: () {},
),
IconButton(
icon: Image.asset('assets/images/ic_notification.png'),
iconSize: 20,
onPressed: () {},
),
IconButton(
icon: Image.asset('assets/images/ic_search.png'),
iconSize: 20,
onPressed: () {},
),
],
),
);
Remove the spacer and add the dropdown in a flexible widget
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
icon: const Icon(
Icons.menu,
color: Colors.black,
size: 34,
),
onPressed: () {},
),
Flexible(//<-------Flexible
child: GestureDetector(
child: Container(
margin: const EdgeInsets.only(left: 10, right: 10),
decoration: BoxDecoration(
color: Colors.red.shade100,
borderRadius: BorderRadius.circular(40.0)),
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
child: Align(
alignment: Alignment.centerLeft,
child: Row(
children: const <Widget>[
Flexible(//<-------Flexible
child: Text(
"Content is here",
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Colors.black54,
fontSize: 19.0,
fontWeight: FontWeight.bold),
),
),
Icon(
Icons.arrow_drop_down,
color: Colors.black,
),
]),
),
),
),
onTap: () {
//todo show bottom sheet dialog here.
},
),
),
//Spacer() //<--------remove this
IconButton(
icon: Image.asset('assets/images/ic_scan.png'),
iconSize: 20,
onPressed: () {},
),
IconButton(
icon: Image.asset('assets/images/ic_notification.png'),
iconSize: 20,
onPressed: () {},
),
IconButton(
icon: Image.asset('assets/images/ic_search.png'),
iconSize: 20,
onPressed: () {},
),
],
),
);

Button with icon and text position left

I use flutter to do RaisedButton with icon and the the icon and text I want change position to left. How to do it? This is my code for button.
I want do it like in the image:
Card(
child: SizedBox(
width: double.infinity,
height: 60,
child: RaisedButton.icon(
icon: Icon(Icons.home,
size: 40, color: Theme.of(context).primaryColor),
color: Colors.white,
textColor: Theme.of(context).primaryColor,
label: const Text('Electrical and Wiring',
style: TextStyle(
fontWeight: FontWeight.normal, fontSize: 15)),
onPressed: () => {},
),
),
),
What you can do you can use Directionality widget in flutter with TextButton.icon(). Here is the Code.
Directionality(
textDirection: TextDirection.rtl,
child: TextButton.icon(
onPressed: null,
icon: Icon(
CupertinoIcons.ellipsis_circle_fill,
color: Colors.green,
),
label: Text(
"Business Name",
style: Theme.of(context).textTheme.subtitle1,
),
),
),
And here is the View.
in the Raised Button Widget you can put a Row() or a Column() as the child, so it can be easily done like this,
RaisedButton(
child: Row(
children: <Widget>[
Icon(Icons.call),
Text('Your Text'),
],
),
onPressed: (){
//Actions
}),
You can achieve that using RaisedButton.icon() widget..
You can get more information from documentation.
Here's a minimal example for you..
RaisedButton.icon(onPressed: null, icon: null, label: null);
Try this
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {},
child: Card(
child: SizedBox(
width: double.infinity,
height: 60,
child: Row(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Icon(Icons.home,
size: 40, color: Theme.of(context).primaryColor),
),
Text('Electrical and Wiring',
style: TextStyle(
fontWeight: FontWeight.normal, fontSize: 15)),
],
)),
),
),

flutter how to get rid of a icon padding?

I did eveything solution on Stackoverflow
but it still has a space horizontally
I want to set these icons very closely each other
my code is this
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
MediaQuery.removePadding(
context: context,
removeLeft: true,
removeRight: true,
child: GestureDetector(
onTap: () {},
child: Container(
padding: const EdgeInsets.all(0.0),
width: 30.0,
child: IconButton(
icon: Icon(
Icons.keyboard_arrow_right,
color: Color(0xff60B906),
),
color: Color(0xff60B906),
iconSize: 30,
),
),
),
),
MediaQuery.removePadding(
context: context,
removeLeft: true,
removeRight: true,
child: GestureDetector(
onTap: () {},
child: Container(
padding: const EdgeInsets.all(0.0),
width: 30.0,
child: IconButton(
icon: Icon(
Icons.keyboard_arrow_right,
color: Color(0xff60B906),
),
color: Color(0xff60B906),
iconSize: 30,
),
),
),
),
],
),
any solution that difference to mine please?
current image
and I want it to be very close
You can use Stack Widget
SAMPLE CODE
Stack(
alignment: Alignment.center,
children: <Widget>[
IconButton(
onPressed: null,
icon: Icon(
Icons.keyboard_arrow_right,
color: Color(0xff60B906),
),
color: Color(0xff60B906),
iconSize: 30,
),
Positioned(
right: 20,
child: IconButton(
onPressed: null,
icon: Icon(
Icons.keyboard_arrow_right,
color: Color(0xff60B906),
),
color: Color(0xff60B906),
iconSize: 30,
),
),
],
)
OUTPUT
if you want to achieve this
there are two options
1- download it as PNG image to your folder assets and make it a child
instead of row
GestureDetector(
onTap: () {},
child: Image.asset(
"imagePath",
color: Color(0xff60B906),
height: 16,
width: 16,
),
),
2- if you want it as Icon make it as Icon data from SVG from this site
https://fluttericon.com/
and make it as usual
Stack them on top of each other using a Stack, then wrap one of them in a padding with only left padding of the points you need to get it look the way you want.

How to implement ontap() function in a custom navigation-drawer list items

In my attempt to create a custom Navigation Drawer in Flutter, I am looking for how I can implement the onTap() method on the list items. I think I should be able to define the function and pass it into the _buildRow(Icons.person_pin, "Profile"), as an argument.
pls check my code below to understand what I actually mean
I simply want to respond to a click/tap event when the list menu are tap. in the default navigation drawer the is onTap attribute in the ListTile Widget.
That is
ListTile(
title: Text("Home"),
onTap: () {
Navigator.of(context).pop();
}
Here is my code snippet
import 'package:flutter/material.dart';
import 'package:payload/src/oval-right-clipper.dart';
class AppEntryHomeAuthenticated extends StatelessWidget {
final GlobalKey<ScaffoldState> _key = GlobalKey<ScaffoldState>();
final Color primary = Colors.white;
final Color active = Colors.grey.shade800;
final Color divider = Colors.grey.shade600;
#override
Widget build(BuildContext context) {
return Scaffold(
key: _key,
appBar: AppBar(
elevation: 50.0,
backgroundColor: Colors.white,
centerTitle: true,
title: Text('PayLoad', style: TextStyle(color: Colors.black)),
actions: <Widget>[
Icon(Icons.settings),
],
automaticallyImplyLeading: false,
leading: IconButton(
icon: Icon(Icons.menu),
onPressed: () {
_key.currentState.openDrawer();
},
),
),
drawer: _buildDrawer(),
body: SingleChildScrollView(),
);
}
_buildDrawer() {
return ClipPath(
clipper: OvalRightBorderClipper(),
child: Drawer(
child: Container(
padding: const EdgeInsets.only(left: 16.0, right: 40),
decoration: BoxDecoration(
color: primary, boxShadow: [BoxShadow(color: Colors.black45)]),
width: 300,
child: SafeArea(
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Container(
alignment: Alignment.centerRight,
child: IconButton(
icon: Icon(
Icons.power_settings_new,
color: active,
),
onPressed: () {},
),
),
Container(
height: 90,
alignment: Alignment.center,
decoration: BoxDecoration(
shape: BoxShape.circle,
gradient: LinearGradient(
colors: [Colors.white10, Colors.white10])),
child: CircleAvatar(
child: Image.asset(
"assets/images/logo.png",
fit: BoxFit.contain,
),
radius: 40,
),
),
SizedBox(height: 5.0),
Text(
"PayLoad Mobile",
//"PayLoad Wallet",
style: TextStyle(
color: active,
fontSize: 18.0,
fontWeight: FontWeight.w600),
),
// Text(
// "#ray",
// style: TextStyle(color: active, fontSize: 16.0),
// ),
SizedBox(height: 30.0),
_buildRow(Icons.home, "Home",),
_buildDivider(),
_buildRow(Icons.person_pin, "Profile"),
_buildDivider(),
_buildRow(Icons.save, "Saving Targets", showBadge: true),
_buildDivider(),
_buildRow(Icons.notifications, "Notifications",
showBadge: false),
_buildDivider(),
_buildRow(Icons.settings, "Settings"),
_buildDivider(),
_buildRow(Icons.question_answer, "Feedback"),
_buildDivider(),
_buildRow(Icons.info_outline, "Help"),
_buildDivider(),
_buildRow(Icons.share, "Share"),
_buildDivider(),
],
),
),
),
),
),
);
}
Divider _buildDivider() {
return Divider(
color: divider,
);
}
Widget _buildRow(IconData icon, String title, {bool showBadge = false}) {
final TextStyle tStyle = TextStyle(color: active, fontSize: 16.0);
return Container(
padding: const EdgeInsets.symmetric(vertical: 5.0),
child: Row(children: [
Icon(
icon,
color: active,
),
SizedBox(width: 10.0),
Text(
title,
style: tStyle,
),
Spacer(),
if (showBadge)
Material(
color: Colors.deepOrange,
elevation: 5.0,
shadowColor: Colors.red,
borderRadius: BorderRadius.circular(5.0),
child: Container(
width: 25,
height: 25,
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.deepOrange,
borderRadius: BorderRadius.circular(5.0),
),
child: Text(
"beta",
style: TextStyle(
color: Colors.white,
fontSize: 12.0,
fontWeight: FontWeight.bold),
),
),
)
]),
);
}
}
If I understood your issue correctly, you can wrap your items with a GestureDetector and use its onTap method:
GestureDetector(
onTap: () => myMethod(),
child: Container(
...
),
),
Works out well.
I only need to wrap the each list in Inkwell