How to add icon from the assets/icons into my button components - flutter

this is my button component code :
Widget build(BuildContext context) {
return MaterialButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(40)),
side: BorderSide(color: kPrimaryColor)
),
padding: padding,
color: color,
minWidth: 280,
onPressed: press,
child: Text(
text,
style: TextStyle(
color: Colors.grey[900],
fontSize: 15,
),
),
);
}

Wrap your child with a Row and add the icon as the second element of your Row's children. It will turn out something like below:
child: Row(
children: [
Text(
text,
style: TextStyle(
color: Colors.grey[900],
fontSize: 15,
),
),
Image.asset('assets/icons/my_icon.png'),
],
),

You can use a TextButton.icon widget
And flip the label and icon (both accept a widget)
TextButton.icon(
icon: Text("MyText",
style: TextStyle(
fontSize: 11.0,
fontFamily: "Raleway"
),
),
label: Image.asset('assets/myImage.png', width: 20.0,height: 20.0,),
style: ButtonStyle(
// backgroundColor:
// MaterialStateProperty.all(Colors.green.shade200),
overlayColor: MaterialStateProperty.all(Colors.green),
side: MaterialStateProperty.resolveWith((states) {
return BorderSide(color: Colors.grey, width: 1);
})),
)

Inside the TextButton, I manage to add a row that contains icon and text
TextButton(
onPressed: (){},
child: Row(
mainAxisSize: MainAxisSize.min,
children: const [
Icon(
Icons.arrow_back_ios,
color: Themes.primaryColor,
),
Text(
'Back',
style: TextStyle(
fontSize: 17,
color: Themes.primaryColor,
),
),
],
),
),

Related

How can I give my text a background color?

I want to give color to the text background like the picture below:
while mine is still like this:
And the code that I have written is like this:
Text(
'Last Activity',
style: TextStyle(
fontSize: 16,
color: ColorName.whitePrimary,
),
),
body: Center(
child: TextButton.icon(
style: TextButton.styleFrom(
textStyle: TextStyle(color: Colors.blue),
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6.0),
),
),
onPressed: () => {},
icon: Text('Last Activity'),
label: Icon(Icons.chevron_right),
),
),
Set the style parameter in your Text widget:
Text(
'Hello, World!',
style: TextStyle(fontSize: 32, backgroundColor: Colors.green),
);
See also
TextStyle (flutter.dev)
Try the following code:
Text(
'Last Activity',
style: TextStyle(
fontSize: 16,
color: ColorName.whitePrimary,
backgroundColor: Colors.indigo[400],
),
),
Refer below code hope its help to you, I have try 2 ways TextButton.icon and Row
1. Using TextButton Widget
TextButton.icon(
style: TextButton.styleFrom(
foregroundColor: Colors.white,
backgroundColor: const Color.fromRGBO(60, 75, 175, 1),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6.0),
),
),
onPressed: () => {},
icon: const Text('Last Activity'),
label: const Icon(Icons.chevron_right),
),
Result Using TextButton.icon->
2. Using Row
GestureDetector(
onTap: () {},
child: Container(
height: 30,//change on your need
width: 120,//change on your need
padding: const EdgeInsets.all(5),
alignment: Alignment.center,
color: const Color.fromRGBO(60, 75, 175, 1),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: const [
Text(
'Last Activity',
style: TextStyle(
color: Colors.white,
),
),
Icon(
Icons.chevron_right,
color: Colors.white,
),
],
),
),
),
Result Using Row->
Try the following code:
Text(
'Last Activity',
style: TextStyle(
fontSize: 16,
color: ColorName.whitePrimary,
),
),

Reducing the space between icon and text - Flutter OutlinedButton

Is there an easy way to reduce the space between the icon and text on an OutlinedButton?
Below is my code. Have made a few different attempts but no luck.
OutlinedButton.icon(
onPressed: () {},
icon: Icon(Icons.flash_on_outlined, size: 20.0),
label: Text(
'Surge',
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
),
style: OutlinedButton.styleFrom(
padding: EdgeInsets.zero,
//fixedSize: Size(40, 25),
backgroundColor: Colors.blue[100],
side: BorderSide(
color: Colors.blue,
width: 1,
),
),
),
OutlinedButton.icon(
onPressed: () {},
icon: Wrap(
// mainAxisAlignment: MainAxisAlignment.start,
children: [
Icon(Icons.flash_on_outlined, size: 20.0),
Text(
'Surge',
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
)
],
),
label: Text(""),
style: OutlinedButton.styleFrom(
padding: EdgeInsets.zero,
//fixedSize: Size(40, 25),
backgroundColor: Colors.blue[100],
side: BorderSide(
color: Colors.blue,
width: 1,
),
),
)
You are using OutlinedButton.icon. If you look into its source code, you'll see that it's nothing magical at all: it simply puts your icon and text in a Row and places a SizedBox in the middle as a gap, its source code is as follows:
// Flutter source code: `outlined_button.dart`, line 378.
final double gap = scale <= 1 ? 8 : lerpDouble(8, 4, math.min(scale - 1, 1))!;
return Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[icon, SizedBox(width: gap), Flexible(child: label)],
);
So, if you don't like this default 8-unit gap, simply don't use .icon constructor. Just use the normal constructor and pass in a Row as its child, with whatever gap you want:
OutlinedButton(
child: Row(
mainAxisSize: MainAxisSize.min,
children: const [
Icon(Icons.star),
SizedBox(width: 8),
Text('Add to bookmark'),
],
),
onPressed: () {},
)
The best way to implement such case I think is to replace icon button with button with RichText as a child:
OutlinedButton(
onPressed: () {},
child: RichText(
text: TextSpan(children: [
// Icon as a font character
TextSpan(
text: String.fromCharCode(Icons.flash_on_outlined.codePoint),
style: TextStyle(
color: Colors.amber,
fontFamily: Icons.flash_on_outlined.fontFamily,
package: Icons.flash_on_outlined.fontPackage,
),
),
// Button text
TextSpan(
text: 'Surge',
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
),
]),
),
style: OutlinedButton.styleFrom(
backgroundColor: Colors.blue[100],
side: BorderSide(
color: Colors.blue,
width: 1,
),
),
)
This solution removes any margin from an icon.
your size, about the icon and the text, are block or you can modify it?

Flutter MouseRegion is not working when the child is a Chip Widget

The mouse cursor in my flutter web program is not changing to a click cursor on hover when the child is a Chip widget. I changed the Chip to a Text and a Container widget and the mouse cursor changes without any issues.
Below is the code of the MouseRegion.
return MouseRegion(
cursor: SystemMouseCursors.click,
child: Container(
width: 200,
alignment: Alignment.centerRight,
child: GestureDetector(
onTap: () {},
child: Chip(
backgroundColor: kLightPrimary,
avatar: const Icon(
Feather.phone_call,
size: 18.0,
color: kPrimaryColor,
),
label: Text(
"Test num",
style: GoogleFonts.poppins(
fontWeight: FontWeight.w500, color: kPrimaryColor),
textAlign: TextAlign.end,
),
padding: const EdgeInsets.all(kDefaultPadding),
),
),
),
),
Instead of MouseRegion and GestureDetector on a Chip, you want to use ActionChip instead.
ActionChip(
onPressed() {/* use onPressed even if it would be empty */}
backgroundColor: kLightPrimary,
avatar: const Icon(
Feather.phone_call,
size: 18.0,
color: kPrimaryColor,
),
label: Text(
"Test num",
style: GoogleFonts.poppins(
fontWeight: FontWeight.w500, color: kPrimaryColor),
textAlign: TextAlign.end,
),
padding: const EdgeInsets.all(kDefaultPadding),
)
Change your GestureDetector to InkWell this is working for me.
return Container(
width: 200,
alignment: Alignment.centerRight,
child: InkWell( //use InkWell insted of GestureDetector
onTap: () {},
child: Chip(
backgroundColor: kLightPrimary,
avatar: const Icon(
Feather.phone_call,
size: 18.0,
color: kPrimaryColor,
),
label: Text(
"Test num",
style: GoogleFonts.poppins(
fontWeight: FontWeight.w500, color: kPrimaryColor),
textAlign: TextAlign.end,
),
padding: const EdgeInsets.all(kDefaultPadding),
),
),
),

OutlinedButton exception when trying to set max size

I'm trying to create something like the below image:
I used two OutlinedButton in a row:
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
OutlinedButton(
child: const Icon(
Icons.arrow_back,
color: colorBackButton,
),
style: ButtonStyle(
fixedSize: MaterialStateProperty.resolveWith(
(states) => Size(65.0, 65.0)))),
Padding(padding: EdgeInsets.only(left: 14.0)),
OutlinedButton(
child: Text(
'Next',
style: GoogleFonts.inter(
textStyle: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white)),
),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.resolveWith((states) {
return colorPrimaryButton;
}),
}),
])
This has led me to this result:
I tried to wrap the Next button with Sizedbox.expand and also add a minimumSize to the button style:
minimumSize: MaterialStateProperty.resolveWith((states) => const Size.fromHeight(55.0))
Both results with a BoxConstraints forces an infinite width. These invalid constraints were provided to RenderPhysicalShape's layout() function by t
Digging into the exception, it's written:
The offending constraints were: BoxConstraints(w=Infinity, 55.0<=h<=Infinity)
How can I make the Next button fill the entire row's space (discluding the padding of course)?
You can wrap with Expanded widget to get maximum available space.
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
OutlinedButton(
onPressed: () {},
child: const Icon(
Icons.arrow_back,
color: Colors.amber,
),
style: ButtonStyle(
fixedSize: MaterialStateProperty.resolveWith(
(states) => const Size(65.0, 65.0),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 14.0),
child: OutlinedButton(
onPressed: () {},
child: const Text('Next',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white)),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.deepPurple),
fixedSize: MaterialStateProperty.resolveWith(
(states) => const Size(65.0, 65.0),
),
),
),
),
),
],
);
You can play with padding. Also, you can use LayoutBuilder.
try use expanded widget Wrap next button with expanded like that
Expanded(
child: OutlinedButton(
child: Text(
'Next',
style: GoogleFonts.inter(
textStyle: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white)),
),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.resolveWith((states) {
return colorPrimaryButton;
}),
}),
),

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