How to add image for buttons in flutter - 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');
},
),

Related

Rounded button with Icon flutter

how to change this square text button to a circular button with a plus icon as the image shows. here's my designed button code. and left side image showing the button I designed so far. I want It to be styled as Right side image.
Padding(
padding: const EdgeInsets.fromLTRB(300, 150, 0, 0),
child: SizedBox(
height: 50,
child: TextButton(
style: TextButton.styleFrom( backgroundColor: Color(0xffCAEC93) ,
side: BorderSide(color: Color(0xffCAEC93),
),
),
onPressed: () {
Icon(
Icons.favorite,
size: 20,
color: Colors.grey,
);
},
child: Text('M',style: TextStyle(fontFamily: 'Sen',color:Color(0xffFFFFFF)),),
),
),
),
You should be using FloatingActionButton instead, however if you still want to use Button then make use of the below code:
ElevatedButton(
onPressed: (){},
style: ElevatedButton.styleFrom(
shape: const CircleBorder(),
primary: Colors.lightGreen,
fixedSize: const Size(60,60)
),
child: const Icon(Icons.add_circle_outline))
Result:
You can use Floating action button for that : https://api.flutter.dev/flutter/material/FloatingActionButton-class.html
I recommend to use FloatingActionButton instead of TextButton:
FloatingActionButton(
onPressed: (){},
child: Icon(Icons.add),
),
your code should be like:
Padding(
padding: const EdgeInsets.fromLTRB(300, 150, 0, 0),
child: SizedBox(
height: 50,
child: FloatingActionButton(
onPressed: (){},
child: Icon(Icons.add),
),
),
),
You can use a FloatingActionButton
Like so:
FloatingActionButton(
child: Icon(Icons.add_circle_outline),
backgroundColor: Color(0xffCAEC93),
onPressed: (){
//do something
}
)
Use Container and give border radius to container and wrap conatiner widget with InkWell.
Center(
child: InkWell(
onTap: (){
print("Clicked");
},
child: Container(
alignment: Alignment.center,
height: 106,
width: 106,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(80)),
),
child: Image.asset("assets/edit.png",scale: 8,)
),
),
),
Image Output:
My approach with text added
Center(
child: InkWell(
onTap: () {},
child: Row(children: [
Container(
alignment: Alignment.center,
height: 40,
width: 40,
decoration: BoxDecoration(
color: Color(0xFFfd9770),
borderRadius: BorderRadius.all(Radius.circular(80)),
),
child: Icon(
Icons.edit,
color: Colors.white,
size: 20,
),
),
SizedBox(
width: 10,
),
Text('edit'.tr)
]))),
Try this it will work fortextButton
SizedBox(
height: 50,
width: 50,
child: TextButton(
style: TextButton.styleFrom(
backgroundColor: Color(0xffCAEC93),
side: BorderSide(
color: Color(0xffCAEC93),
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50),
side: BorderSide(color: Colors.red)),
),
onPressed: () {},
child: Icon(
Icons.add,
color: Colors.white,
)),
),
Or you can try FloatingActionButton

How to ignore 'label' on the 'Elevated button'?

I want to make a button with an icon centered.
thie is my code.
ElevatedButton.icon(
onPressed: () {},
icon: Icon(Icons.arrow_forward),
label: Text(''),
style: ElevatedButton.styleFrom(
primary: Colors.red,
fixedSize: Size(100, 50),
)
Try below code hope its help to you. The lable field is required for ElevatedButton.icon refer documentation here you can used TextButton
Using Container
Container(
height: 50.0,
width: 150.0,
child: TextButton(
child: Icon(Icons.person),
onPressed: () {},
),
),
Using SizedBox
SizedBox(
height: 50.0,
width: 150.0,
child: TextButton(
child: Icon(Icons.person),
onPressed: () {},
),
),
please try with this
ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.red.shade800),
),
child: FittedBox(
fit: BoxFit.fitWidth,
child: Icon(Icons.person_add_alt_1_rounded, size: 18)),
)
InkWell(
child: Container(
height: 50,
width: 100,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
color: Colors.red,),
child: Icon(Icons.arrow_forward, color: Colors.white),
),
onTap: () {
},
),
the label field is the required field in the ElevatedButton.icon widget. But there are other options as well like this:
Container(
height: 50.0,
width: 100.0,
child: FlatButton(
child: Icon(Icons.arrow_forward),
onPressed: () {},
),
),
EDIT:
You could use Container with GestureDetector widget, like this:
GestureDetector(
onTap: (){},
child: Container(
height: 50.0,
width: 100.0,
decoration: BoxDecoration(
color: Colors.red
),
child: Center(child: Icon(Icons.arrow_forward)),
)
)
ElevatedButton(
onPressed: () {},
child: Icon(Icons.arrow_forward),
style: ElevatedButton.styleFrom(primary: Colors.red,fixedSize: Size(100, 50),)

flutter alertdialog with background image

Folloing is my alert dialog box code, i need to set an background image for the alert dialog instead of blue color, need to show a design
AlertDialog(
backgroundColor: Colors.blue,
titlePadding: EdgeInsets.all(0),
contentPadding: EdgeInsets.all(0),
title: Container(
decoration: BoxDecoration(
color: profile_edit_toolbar_color,
borderRadius: BorderRadius.all(Radius.circular(8))),
width: MediaQuery.of(context).size.width * 0.7,
height: MediaQuery.of(context).size.height * 0.5,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: const EdgeInsets.all(18),
child: Text(
'Select countries to show',
style: TextStyle(
color: Colors.yellow[300],
fontSize: 20,
fontFamily: fontFamily_3),
// style: CustomStyle.balooCustom(20, Colors.white)
),
),
IconButton(
onPressed: () {
Navigator.of(context).pop();
},
icon: Icon(
Icons.close,
size: 25,
color: Colors.white,
)),
],
),
Row(
children: [
Expanded(
child: Container(
height: 120,
child: DisplayCountriesForm(
countriesList: eventResponse.countriesList,
num: 0,
displayCountries: displayCountries,
onValueChanged: (updatedList) {
},
),
)),
Expanded(
child: Container(
height: 120,
child: DisplayCountriesForm(
countriesList: eventResponse.countriesList,
num: 1,
displayCountries: displayCountries,
onValueChanged: (updatedList) {
},
),
)),
Expanded(
child: Container(
height: 120,
child: DisplayCountriesForm(
countriesList: eventResponse.countriesList,
num: 2,
displayCountries: displayCountries,
onValueChanged: (updatedList) {
},
),
)),
],
),
],
),
),
);
Try below code I have add background image of the alert dialog hope your problem is solved , Use your widget also and change background image on your need
TextButton(
child: Text('Pressed Me'),
onPressed: () => showDialog(
context: context,
builder: (context) => AlertDialog(
content: Stack(
alignment: Alignment.center,
children: <Widget>[
Image.network(
'https://www.kindpng.com/picc/m/355-3557482_flutter-logo-png-transparent-png.png',
),
Text(
'Add Your Text Here',
style: TextStyle(
fontSize: 24,
),
),
],
),
),
),
),
Your Result Screen->

Flutter FlatButton is deprecated - alternative solution

This is my FlatButton. How can I solve it with textButton or elevatedButton?
Center(
child: FlatButton(
onPressed: () { },
child: Container(
margin:EdgeInsets.only(top: 25),
child: image != null
? Image.file(image,width:140,height:192,fit:BoxFit.fill)
: Container(
width: 240,
height: 200,
child: Icon(Icons.camera_front, size: 100, color:Colors.grey,)
),
),
),
),
Change FlatButton to TextButton.
Center(
child: TextButton(
onPressed: () { },
child: Container(
margin:EdgeInsets.only(top: 25),
child: image != null
? Image.file(image,width:140,height:192,fit:BoxFit.fill)
: Container(
width: 240,
height: 200,
child: Icon(Icons.camera_front, size: 100, color:Colors.grey,)
),
),
),
),
For styling using TextButton,
TextButton(
style: TextButton.styleFrom(
padding: const EdgeInsets.all(16.0),
primary: Colors.white,
textStyle: const TextStyle(fontSize: 20),
),
onPressed: () {},
child: const Text('Gradient'),
),
FlatButton is replaced with TextButton and RaisedButton is replaced with ElevatedButton.
Here is the code of TextButton with styling
TextButton(
onPressed: () { },
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.deepPurple)
),
child: Container(
margin:EdgeInsets.only(top: 25),
child: image != null
? Image.file(image,width:140,height:192,fit:BoxFit.fill)
: Container(
width: 240,
height: 200,
child: Icon(Icons.camera_front, size: 100, color:Colors.grey,)
),
),
),
Just change FlatButton to TextButton or ElevatedButton.

How to add onClick on image.asset in flutter?

I am using three images on clicking which will navigate to other page so how should I use onClick on these images? My code is below:
Row(
children: [
Expanded(
child: Column(
children: <Widget>[
Container(
child: ClipRRect(
borderRadius: BorderRadius.circular(20.0),
child: Image.asset('assets/cat.jpg',
width: 110.0, height: 110.0),
)),
Text(
'Tickets',
style:
TextStyle(fontSize: 16.0, fontWeight: FontWeight.bold),
)
],
),
),
Expanded(
child: Column(
children: <Widget>[
Container(
child: ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Image.asset('assets/cat.jpg',
width: 110.0, height: 110.0),
)),
Text(
'Buy Tickets',
style:
TextStyle(fontSize: 16.0, fontWeight: FontWeight.bold),
)
],
),
),
Expanded(
child: Column(
children: <Widget>[
Container(
child: ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Image.asset('assets/cat.jpg',
width: 110.0, height: 110.0),
)),
Text(
'Prizes',
style:
TextStyle(fontSize: 16.0, fontWeight: FontWeight.bold),
)
],
),
),
],
),
Expected : Adding an onClick on images
I used GestureDetector but it is throwing error so I need to know what I should use and how.
I read other answers and found that you were having issues with border, try this solution.
GestureDetector(
onTap: () {}, // Image tapped
child: Image.asset(
'assets/cat.jpg',
fit: BoxFit.cover, // Fixes border issues
width: 110.0,
height: 110.0,
),
)
If you want splash effects, then use Ink.image or Ink with decoration.
InkWell(
onTap: () {}, // Image tapped
splashColor: Colors.white10, // Splash color over image
child: Ink.image(
fit: BoxFit.cover, // Fixes border issues
width: 100,
height: 100,
image: AssetImage(
'assets/cat.jpg,
),
),
)
Material(
child: InkWell(
onTap: () {},
child: ClipRRect(
borderRadius: BorderRadius.circular(20.0),
child: Image.asset('assets/cat.jpg',
width: 110.0, height: 110.0),
),
),
)
You can use InkWell as show by #Murat Aslan.
And you can also use GestureDetector as shown below.
Material(
child: GestureDetector(
onTap: () {},
child: Container(
child: ClipRRect(
borderRadius: BorderRadius.circular(20.0),
child: Image.asset('assets/cat.jpg',
width: 110.0, height: 110.0),
),
),
),
)
An alternative is to use a FlatButton with an Image as a child:
FlatButton(
onPressed: () {
print('I got clicked');
},
child: Image.asset('images/ball1.png'),
),
To render a material splash during tap on image, use Ink.image
InkWell(
onTap: () {},
child: Ink.image(
image: AssetImage('assets/cat.jpg'),
// fit: BoxFit.cover,
width: 110,
height: 110,
),
)