How to set the width and height of a button in Flutter? - flutter

I have seen that I can't set the width of a ElevatedButton in Flutter. If I have well understood, I should put the ElevatedButton into a SizedBox. I will then be able to set the width or height of the box. Is it correct? Is there another way to do it?
This is a bit tedious to create a SizedBox around every buttons so I'm wondering why they have chosen to do it this way. I'm pretty sure that they have a good reason to do so but I don't see it.
The scaffolding is pretty difficult to read and to build for a beginner.
new SizedBox(
width: 200.0,
height: 100.0,
child: ElevatedButton(
child: Text('Blabla blablablablablablabla bla bla bla'),
onPressed: _onButtonPressed,
),
),

As said in documentation here
Raised buttons have a minimum size of 88.0 by 36.0 which can be
overidden with ButtonTheme.
You can do it like that
ButtonTheme(
minWidth: 200.0,
height: 100.0,
child: RaisedButton(
onPressed: () {},
child: Text("test"),
),
);

match_parent (Full width):
SizedBox(
width: double.infinity, // <-- match_parent
height: double.infinity, // <-- match-parent
child: ElevatedButton(...)
)
or
SizedBox.expand(
child: ElevatedButton(...)
)
Specific width:
SizedBox(
width: 100, // <-- Your width
height: 50, // <-- Your height
child: ElevatedButton(...)
)

With Flutter 2.0 RaisedButton is deprecated and replaced by ElevatedButton.
With that in mind, much more cleaner approach to give custom size to ElevatedButton is minimumSize property of ElevatedButton widget.
Output
Full Code
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.green,
onPrimary: Colors.white,
shadowColor: Colors.greenAccent,
elevation: 3,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(32.0)),
minimumSize: Size(100, 40), //////// HERE
),
onPressed: () {},
child: Text('Hey bro'),
)
Note: Also keep in mind that same approach can be used in new widgets like TextButton and OutlinedButton using TextButton.styleFrom(minimumSize: Size(100, 40)) and OutlinedButton.styleFrom(minimumSize: Size(100, 40)) respectively.

That's because flutter is not about size. It's about constraints.
Usually we have 2 use cases :
The child of a widget defines a constraint. The parent size itself is based on that information. ex: Padding, which takes the child constraint and increases it.
The parent enforce a constraint to its child. ex: SizedBox, but also Column in strech mode, ...
RaisedButton is the first case. Which means it's the button which defines its own height/width. And, according to material rules, the raised button size is fixed.
You don't want that behavior, therefore you can use a widget of the second type to override the button constraints.
Anyway, if you need this a lot, consider either creating a new widget which does the job for you. Or use MaterialButton, which possesses a height property.

I would recommend using a MaterialButton, than you can do it like this:
MaterialButton(
height: 40.0,
minWidth: 70.0,
color: Theme.of(context).primaryColor,
textColor: Colors.white,
child: new Text("push"),
onPressed: () => {},
splashColor: Colors.redAccent,
)

You need to use an Expanded Widget. But, if your button is on a column, the Expanded Widget fills the rest of the column. So, you need to enclose the Expanded Widget within a row.
Row(children: <Widget>[
Expanded(
flex: 1,
child: RaisedButton(
child: Text("Your Text"),
onPressed: _submitForm,
),
),),])

The new buttons TextButton, ElevatedButton, OutlinedButton etc. are to be changed in a different way.
One method I found is from this article: you need to "tighten" a constrained box around the button.
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Kindacode.com'),
),
body: Center(
child: ConstrainedBox(
constraints: BoxConstraints.tightFor(width: 300, height: 200),
child: ElevatedButton(
child: Text('300 x 200'),
onPressed: () {},
),
),
));
}

Use Media Query to use width wisely for your solution which will run the same for small and large screen
Container(
width: MediaQuery.of(context).size.width * 0.5, // Will take 50% of screen space
child: RaisedButton(
child: Text('Go to screen two'),
onPressed: () => null
),
)
You can apply a similar solution to SizeBox also.

My preferred way to make Raise button with match parent is that wrap it with Container.
below is sample code.
Container(
width: double.infinity,
child: RaisedButton(
onPressed: () {},
color: Colors.deepPurpleAccent[100],
child: Text(
"Continue",
style: TextStyle(color: Colors.white),
),
),
)

This piece of code will help you better solve your problem, as we cannot specify width directly to the RaisedButton, we can specify the width to it's child
double width = MediaQuery.of(context).size.width;
var maxWidthChild = SizedBox(
width: width,
child: Text(
StringConfig.acceptButton,
textAlign: TextAlign.center,
));
RaisedButton(
child: maxWidthChild,
onPressed: (){},
color: Colors.white,
);

Simply use FractionallySizedBox, where widthFactor & heightFactor define the percentage of app/parent size.
FractionallySizedBox(
widthFactor: 0.8, //means 80% of app width
child: RaisedButton(
onPressed: () {},
child: Text(
"Your Text",
style: TextStyle(color: Colors.white),
),
color: Colors.red,
)),

You can create global method like for button being used all over the app. It will resize according to the text length inside container. FittedBox widget is used to make widget fit according to the child inside it.
Widget primaryButton(String btnName, {#required Function action}) {
return FittedBox(
child: RawMaterialButton(
fillColor: accentColor,
splashColor: Colors.black12,
elevation: 8.0,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5.0)),
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 13.0),
child: Center(child: Text(btnName, style: TextStyle(fontSize: 18.0))),
),
onPressed: () {
action();
},
),
);
}
If you want button of specific width and height you can use constraint property of RawMaterialButton for giving min max width and height of button
constraints: BoxConstraints(minHeight: 45.0,maxHeight:60.0,minWidth:20.0,maxWidth:150.0),

If you want globally change the height and the minWidth of all your RaisedButtons, then you can override ThemeData inside your MaterialApp:
#override
Widget build(BuildContext context) {
return MaterialApp(
...
theme: ThemeData(
...
buttonTheme: ButtonThemeData(
height: 46,
minWidth: 100,
),
));
}

Wrap RaisedButton inside Container and give width to Container Widget.
e.g
Container(
width : 200,
child : RaisedButton(
child :YourWidget ,
onPressed(){}
),
)

We can also use ElevatedButton Widget, it have fixedSize property. latest Flutter version
ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
fixedSize: Size(120, 34), // specify width, height
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(
20,
))),
child: Text("Search"),
)
Preview

This worked for me. The Container provides the height and FractionallySizedBox provides the width for the RaisedButton.
Container(
height: 50.0, //Provides height for the RaisedButton
child: FractionallySizedBox(
widthFactor: 0.7, ////Provides 70% width for the RaisedButton
child: RaisedButton(
onPressed: () {},
),
),
),

Try with Container, I think we will have more control.
ElevatedButton(
style: ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
onPressed: () {
buttonClick();
},
child: Container(
height: 70,
width: 200,
alignment: Alignment.center,
child: Text("This is test button"),
),
),

you can do as they say in the comments or you can save the effort and work with RawMaterialButton . which have everything and you can change the border to be circular
and alot of other attributes. ex shape(increase the radius to have more circular shape)
shape: new RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),//ex add 1000 instead of 25
and you can use whatever shape you want as a button by using GestureDetector which is a widget and accepts another widget under child attribute.
like in the other example here
GestureDetector(
onTap: () {//handle the press action here }
child:Container(
height: 80,
width: 80,
child:new Card(
color: Colors.blue,
shape: new RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
elevation: 0.0,
child: Icon(Icons.add,color: Colors.white,),
),
)
)

If the button is placed in a Flex widget (including Row & Column), you can wrap it using an Expanded Widget to fill the available space.

we use Row or Column, Expanded, Container and the element to use example RaisedButton
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0),
),
Row(
children: <Widget>[
Expanded(
flex: 2, // we define the width of the button
child: Container(
// height: 50, we define the height of the button
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: RaisedButton(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
textColor: Colors.white,
color: Colors.blue,
onPressed: () {
// Method to execute
},
child: Text('Copy'),
),
),
),
),
Expanded(
flex: 2, // we define the width of the button
child: Container(
// height: 50, we define the height of the button
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: RaisedButton(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
textColor: Colors.white,
color: Colors.green,
onPressed: () {
// Method to execute
},
child: Text('Paste'),
),
),
),
),
],
),
],
),
),

SizedBox(
width: double.infinity,
child: ElevatedButton(
child: Text("FULL WIDTH"),
onPressed: () {},
),
),
Use ElevatedButton since RaisedButton is deprecated

In my case(the Button is a child of a horizontal ListView), I had to wrap the button with a padding widget and set right/left padding.
Padding(
padding: const EdgeInsets.only(right: 50, left: 50),
child: ElevatedButton(onPressed: () {}, child: Text("LOGOUT")),
)

In my case I used margin to be able to change the size:
Container(
margin: EdgeInsets.all(10),
// or margin: EdgeInsets.only(left:10, right:10),
child: RaisedButton(
padding: EdgeInsets.all(10),
shape: RoundedRectangleBorder(borderRadius:
BorderRadius.circular(20)),
onPressed: () {},
child: Text("Button"),
),
),

If you don't want to remove all the button theme set up.
ButtonTheme.fromButtonThemeData(
data: Theme.of(context).buttonTheme.copyWith(
minWidth: 200.0,
height: 100.0,,
)
child: RaisedButton(
onPressed: () {},
child: Text("test"),
),
);

If you have a button inside a Column() and want the button to take maximum width, set:
crossAxisAlignment: CrossAxisAlignment.stretch
in your Column widget. Now everything under this Column() will have maximum available width

I struggled with this problem and found what my problem was: I defined all my buttons inside a ListView. Does not matter what I did, the width of the button was always the width of the screen. I replaces ListView by Column and voila - suddenly I could control the button width.

Use SizeBox with width and height parameters
SizedBox(
width: double.infinity,
height: 55.0,
child: ElevatedButton(
.....
),
);

You don't need to use other widget to set the size. You can use minimumSize for ElevatedButton
ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: const Size(200, 50),
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50), // <-- Radius
),
),
onPressed: (){},
child: const Text("Text", style: TextStyle(fontSize: 20, fontWeight: FontWeight.w500),),
),

To set the height and width of Any Button Just Wrap it with SizedBox. you set easily the height and width of any button by Wrape with SizedBox .
And if you want to give Space between Two Any Kind of Widgets then you can Used SizedBox and inside the SizedBox you use height .As much as you want to give..

wrap your ElevatedButton with a Column PLUS add padding for the button for the style:
Column(
children: [
ElevatedButton(
style: TextButton.styleFrom(
backgroundColor: Colors.green,
padding: const EdgeInsets.symmetric(
horizontal: 20 * 1.5, vertical: 20),
),
onPressed: () {},
child: const Text('text')),],),

Related

Expanded Container resizes after wrapping container with TextButton or any other button

I have wrapped a Container with an expanded widget. There are many containers of that sort but let's look at how one changes as soon as I wrap the container with a Button; it becomes small.
why is that? and how can I fix it? I want the size to be just as it was after adding a button.
Expanded(
child: TextButton(
onPressed: () { },
child: Container(
child: Icon(FontAwesomeIcons.divide, size: 50,),
decoration: BoxDecoration(
color: Color(0xFFFCACC9),
shape: BoxShape.circle
),
),
),
),
all the containers but the small one with the division sign in it have no button added
add width and height in your container to fix it
Expanded(
child: TextButton(
onPressed: () { },
child: Container(
height: 100, // <------- for example 100
width: 100, // <------- for example 100
child: Icon(FontAwesomeIcons.divide, size: 50,),
decoration: BoxDecoration(
color: Color(0xFFFCACC9),
shape: BoxShape.circle
),
),
)

How to reduce size of raisedButton in flutter?

I want to reduce the size of raisedbutton. By default it is 88*36. I want smaller buttons on screen.
The outcome is like above:
Code for each button is like below:
ButtonTheme(
minWidth: 20,
height: 22,
child: RaisedButton(
textColor: Colors.black87,
color: Colors.blue,
child: Text(
"31",
),
onPressed: () {},
),
)
But, what is happening is, even though i am able to see button of size 20*22, button is getting clicked even when I click around the button. (In the space between two buttons)
Am I doing it the correct way?
It should work with ButtonTheme but you can wrap with parent widget like SizedBox to match to parent size:
SizedBox(
width: 60,
child: RaisedButton(
textColor: Colors.black87,
//..
),
)
Using ButtonTheme:
ButtonTheme(
padding: new EdgeInsets.all(0.0),
minWidth: 60,
child: RaisedButton(
textColor: Colors.black87,
//..
),
),

Flutter: Remove padding in buttons - FlatButton, ElevatedButton, OutlinedButton

I am looking to remove the default margin of the FlatButton but can't seem to set/override it.
Column(children: <Widget>[
Container(
children: [
FractionallySizedBox(
widthFactor: 0.6,
child: FlatButton(
color: Color(0xFF00A0BE),
textColor: Color(0xFFFFFFFF),
child: Text('LOGIN', style: TextStyle(letterSpacing: 4.0)),
shape: RoundedRectangleBorder(side: BorderSide.none)))),
Container(
margin: const EdgeInsets.only(top: 0.0),
child: FractionallySizedBox(
widthFactor: 0.6,
child: FlatButton(
color: Color(0xFF00A0BE),
textColor: Color(0xFF525252),
child: Text('SIGN UP',
style: TextStyle(
fontFamily: 'Lato',
fontSize: 12.0,
color: Color(0xFF525252),
letterSpacing: 2.0)))))
])
I've come across things like ButtonTheme and even debugDumpRenderTree() but haven't been able to implement them properly.
FlatButton(materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,)
UPDATE (New buttons)
TextButton
TextButton(
onPressed: () {},
style: TextButton.styleFrom(
minimumSize: Size.zero, // Set this
padding: EdgeInsets.zero, // and this
),
child: Text('TextButton'),
)
ElevatedButton
ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
minimumSize: Size.zero, // Set this
padding: EdgeInsets.zero, // and this
),
child: Text('ElevatedButton'),
)
OutlinedButton
OutlinedButton(
onPressed: () {},
style: OutlinedButton.styleFrom(
minimumSize: Size.zero, // Set this
padding: EdgeInsets.zero, // and this
),
child: Text('OutlinedButton'),
)
You can also use the raw MaterialButton
MaterialButton(
onPressed: () {},
color: Colors.blue,
minWidth: 0,
height: 0,
padding: EdgeInsets.zero,
child: Text('Button'),
)
I find it easier to just wrap the button in a ButtonTheme.
Specify the maxWith and height (set to zero to wrap the child) and then pass your button as the child.
You can also move most of your button properties from the button to the theme to gather all properties in one widget.
ButtonTheme(
padding: EdgeInsets.symmetric(vertical: 4.0, horizontal: 8.0), //adds padding inside the button
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, //limits the touch area to the button area
minWidth: 0, //wraps child's width
height: 0, //wraps child's height
child: RaisedButton(onPressed: (){}, child: Text('Button Text')), //your original button
);
FlatButton(
padding: EdgeInsets.all(0)
)
did the trick for me
Courtesy of FlatButton introduces phantom padding on flutter's git.
If anyone needs a widget with onPressed event without Flutter padding it.
You should use InkWell
InkWell(
child: Center(child: Container(child: Text("SING UP"))),
onTap: () => onPressed()
);
A rectangular area of a Material that responds to touch.
For all those who are wondering on how to remove the default padding around the text of a FlatButton, you can make use of RawMaterialButton instead and set the constraints to BoxConstraints() which will reset the default minimum width and height of button to zero.
RawMaterialButton can be used to configure a button that
doesn't depend on any inherited themes. So we can customize all default values based on our needs.
Example:
RawMaterialButton(
constraints: BoxConstraints(),
padding: EdgeInsets.all(5.0), // optional, in order to add additional space around text if needed
child: Text('Button Text')
)
Please refer to this documentation for further customization.
Text Button previously FlatButton
To remove spacing between 2 TextButton use tapTargetSize
set tapTargetSize to MaterialTapTargetSize.shrinkWrap
To remove padding
set padding to EdgeInsets.all(0)
TextButton(
child: SizedBox(),
style: TextButton.styleFrom(
backgroundColor: Colors.red,
padding: EdgeInsets.all(0),
tapTargetSize: MaterialTapTargetSize.shrinkWrap
),
onPressed: () {
print('Button pressed')
},
),
you can also change the button width by surrounding it with a sized box:
SizedBox(
width: 40,
height: 40,
child: RaisedButton(
elevation: 10,
onPressed: () {},
padding: EdgeInsets.all(0), // make the padding 0 so the child wont be dragged right by the default padding
child: Container(
child: Icon(Icons.menu),
),
),
),
Since FlatButton is now deprecated you can use TextButton. So if you want to remove the padding on TextButton, you would do it like this:
TextButton( style: ButtonStyle(padding: MaterialStateProperty.all(EdgeInsets.zero))
wrap your FlatButton inside a container and give your custom width
eg.
Container(
width: 50,
child: FlatButton(child: Text("WORK",style: Theme.of(context).textTheme.bodyText1.copyWith(fontWeight: FontWeight.bold),),
onPressed: () => Navigator.pushNamed(context, '/locationChange'),materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,padding: EdgeInsets.all(0),),
)
I faced the same thing, There is horizontal padding inside the RawMaterialButton Widget I don't need it.
I solved it using this way :
RawMaterialButton(
onPressed: () {
},
child: Container(
child: Row(
children: [
// Any thing you want to use it. Column or Container or any widget.
],
),
),
),

How to make button width match parent?

I want to know that how can I set a width to match parent layout width
new Container(
width: 200.0,
padding: const EdgeInsets.only(top: 16.0),
child: new RaisedButton(
child: new Text(
"Submit",
style: new TextStyle(
color: Colors.white,
)
),
colorBrightness: Brightness.dark,
onPressed: () {
_loginAttempt(context);
},
color: Colors.blue,
),
),
I know about little bit on Expanded widget but Expanded expands view to both direction, i dont know how to do it.
Update:
With Flutter 2.0 RaisedButton is deprecated and replaced by ElevatedButton. you can use minimumSize like this:
ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: Size.fromHeight(40), // fromHeight use double.infinity as width and 40 is the height
),
onPressed: () {},
child: Text('Text Of Button'),
)
Old answer for Flutter less than 2.0:
The correct solution would be to use the SizedBox.expand widget, which enforces its child to match its parent's size.
SizedBox.expand(
child: RaisedButton(...),
)
There are many alternatives, which allows for more or less customization:
SizedBox(
width: double.infinity,
// height: double.infinity,
child: RaisedButton(...),
)
or using a ConstrainedBox
ConstrainedBox(
constraints: const BoxConstraints(minWidth: double.infinity),
child: RaisedButton(...),
)
Container(
width: double.infinity,
child: RaisedButton(...),
),
After some research, I found out some solution, and thanks to #Günter Zöchbauer,
I used column instead of Container and
set the property to column CrossAxisAlignment.stretch to Fill match parent of Button
new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new RaisedButton(
child: new Text(
"Submit",
style: new TextStyle(
color: Colors.white,
)
),
colorBrightness: Brightness.dark,
onPressed: () {
_loginAttempt(context);
},
color: Colors.blue,
),
],
),
The size attribute can be provided using ButtonTheme with minWidth: double.infinity
ButtonTheme(
minWidth: double.infinity,
child: MaterialButton(
onPressed: () {},
child: Text('Raised Button'),
),
),
or after https://github.com/flutter/flutter/pull/19416 landed
MaterialButton(
onPressed: () {},
child: SizedBox.expand(
width: double.infinity,
child: Text('Raised Button'),
),
),
The simplest way is to use a FlatButton wrapped inside a Container, The button by default takes the size of its parent and so assign a desired width to the Container.
Container(
color: Colors.transparent,
width: MediaQuery.of(context).size.width,
height: 60,
child: FlatButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0),
),
onPressed: () {},
color: Colors.red[300],
child: Text(
"Button",
style: TextStyle(
color: Colors.black,
fontFamily: 'Raleway',
fontSize: 22.0,
),
),
),
)
Output:
You can set match parent of the widget by
1) set width to double.infinity :
new Container(
width: double.infinity,
padding: const EdgeInsets.only(top: 16.0),
child: new RaisedButton(
child: new Text(
"Submit",
style: new TextStyle(
color: Colors.white,
)
),
colorBrightness: Brightness.dark,
onPressed: () {
_loginAttempt(context);
},
color: Colors.blue,
),
),
2) Use MediaQuery:
new Container(
width: MediaQuery.of(context).size.width,
padding: const EdgeInsets.only(top: 16.0),
child: new RaisedButton(
child: new Text(
"Submit",
style: new TextStyle(
color: Colors.white,
)
),
colorBrightness: Brightness.dark,
onPressed: () {
_loginAttempt(context);
},
color: Colors.blue,
),
),
#Mohit Suthar,
Found one of the best solution for match parent to width as well as height as below
new Expanded(
child: new Container(
padding: EdgeInsets.all(16.0),
margin: EdgeInsets.all(16.0),
decoration: new BoxDecoration(
color: Colors.white,
borderRadius:
const BorderRadius.all(const Radius.circular(8.0)),
border: new Border.all(color: Colors.black, width: 1.0)),
child: new Text("TejaDroid")),
),
Here you can check that the Expanded Controller acquire whole remain width and height.
For match_parent you can use
SizedBox(
width: double.infinity, // match_parent
child: RaisedButton(...)
)
For any particular value you can use
SizedBox(
width: 100, // specific value
child: RaisedButton(...)
)
The simplest way to give match-parent width or height in the given code above.
...
width: double.infinity,
height: double.infinity,
...
There are many ways to make full width button. But I think you should understand the concept of making full width widgets in different scenarios:
When you are using nested widgets then it is hard to identify width of parent widget. Simply you can't specify width in nested widgets. So you should use either Expanded or Column with CrossAxisAlignment as Strech.
In other cases, you can use media query width or double.infinity.
Here are some examples for Nested widgets and single widget:
First:
Expanded( // This will work for nested widgets and will take width of first parent widget.
child: MaterialButton(
onPressed: () {},
child: const Text("Button Text"),
color: Colors.indigo,
textColor: Colors.white,
)
)
Second:
Column( // This will not work if parent widget Row.
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
MaterialButton(
onPressed: () {},
child: const Text("Button Text"),
color: Colors.indigo,
textColor: Colors.white,
)
]
)
Third:
ButtonTheme( // if use media query, then will not work for nested widgets.
minWidth: double.infinity, //Or use 'width: MediaQuery.of(context).size.width'
child: MaterialButton(
onPressed: () {},
child: const Text("Button Text"),
color: Colors.indigo,
textColor: Colors.white,
)
)
Forth:
SizedBox( // if use media query, then will not work for nested widgets.
width: double.infinity, //Or use 'width: MediaQuery.of(context).size.width'
child: MaterialButton(
onPressed: () {},
child: const Text("Button Text"),
color: Colors.indigo,
textColor: Colors.white,
)
)
Fifth:
Container( // if use media query, then will not work for nested widgets.
width: double.infinity, //Or use 'width: MediaQuery.of(context).size.width'
child: MaterialButton(
onPressed: () {},
child: const Text("Button Text"),
color: Colors.indigo,
textColor: Colors.white,
)
)
From my point of view, recommended will be the First one. Also you can change MaterialButton to ElevatedButton or TextButton or RaisedButton (Depreciated) or any other widget.
Cheers!
you can do that with MaterialButton
MaterialButton(
padding: EdgeInsets.all(12.0),
minWidth: double.infinity,
onPressed: () {},
child: Text("Btn"),
)
You can set the fixedSize.width of the ButtonStyle to a very large number, like double.maxFinite. You can also use Size.fromWidth() constructor if you don't want to specify the height:
ElevatedButton(
child: const Text('Button'),
style: ElevatedButton.styleFrom(
fixedSize: const Size.fromWidth(double.maxFinite),
),
),
Live Demo
The most basic approach is using Container by define its width to infinite. See below example of code
Container(
width: double.infinity,
child:FlatButton(
onPressed: () {
//your action here
},
child: Text("Button"),
)
)
OutlineButton(
onPressed: () {
logInButtonPressed(context);
},
child: Container(
width: MediaQuery.of(context).size.width / 2,
child: Text(
“Log in”,
textAlign: TextAlign.center,
),
),
)
Something like this works for me.
The Following code work for me
ButtonTheme(
minWidth: double.infinity,
child: RaisedButton(child: Text("Click!!", style: TextStyle(color: Colors.white),), color: Colors.pink, onPressed: () {}))
This is working for me in a self contained widget.
Widget signinButton() {
return ButtonTheme(
minWidth: double.infinity,
child: new FlatButton(
onPressed: () {},
color: Colors.green[400],
textColor: Colors.white,
child: Text('Flat Button'),
),
);
}
// It can then be used in a class that contains a widget tree.
This is working for me.
SizedBox(
width: double.maxFinite,
child: RaisedButton(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
child: new Text("Button 2"),
color: Colors.lightBlueAccent,
onPressed: () => debugPrint("Button 2"),
),
),
RaisedButton(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [Text('Submit')],
)
)
It works for me.
With Flutter 2.0 RaisedButton is deprecated and replaced by ElevatedButton.
minimumSize property of ElevatedButton widget exactly does that.
Example code:
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.green,
onPrimary: Colors.white,
shadowColor: Colors.greenAccent,
elevation: 3,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0)),
minimumSize: Size(100, 40), //////// HERE
),
onPressed: () {},
child: Text('MyButton'),
)
new SizedBox(
width: 100.0,
child: new RaisedButton(...),
)
Using a ListTile also works as well, since a list fills the entire width:
ListTile(
title: new RaisedButton(...),
),
This one worked for me
width: MediaQuery.of(context).size.width-100,
Wrap your (child widget having a fixed width) with a center widget. This will center your widget:
Center(child:Container(width:250,child:TextButton(child:Text("Button Name),),)
TextButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
fixedSize: MaterialStateProperty.all(
Size(double.maxFinite, 50.0),
),
),
onPressed: () {},
child: Text('Upgrade to Premium'),
),
TRY
Flexible(
child: Row(
children: [
ElevatedButton(
onPressed: () {},
child: Text("love you}"),
),
],
),
),

InkWell not showing ripple effect

Tapping the container triggers the onTap() handler but does not show any ink splash effect.
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new InkWell(
onTap: (){print("tapped");},
child: new Container(
width: 100.0,
height: 100.0,
color: Colors.orange,
),
),
),
);
}
}
I tried putting the InkWell inside the Container as well but in vain.
I think adding color to the container is covering over the ink effect
https://api.flutter.dev/flutter/material/InkWell/InkWell.html
This code seems to work
body: new Center(
child: new Container(
child: new Material(
child: new InkWell(
onTap: (){print("tapped");},
child: new Container(
width: 100.0,
height: 100.0,
),
),
color: Colors.transparent,
),
color: Colors.orange,
),
),
just click the middle square.
Edit: I found the bug report. https://github.com/flutter/flutter/issues/3782
This is actually as expected, though we should update the docs to make it clearer.
What's going on is that the Material spec says that the splashes are actually ink on the Material. So when we splash, what we do is we literally have the Material widget do the splash. If you have something on top of the Material, we splash under it, and you can't see it.
I have wanted to add a "MaterialImage" widget that conceptually prints its image into the Material as well so that then the splashes would be over the image. We could have a MaterialDecoration which does something similar for a Decoration. Or we could have Material itself take a decoration. Right now it takes a color, but we could extend that to taking a whole decoration. It's not clear whether it's really material-spec-compatible to have a material with a gradient, though, so I'm not sure whether we should do that.
In the short run, if you just need a workaround, you can put a Material on top of the container, with the material set to use the "transparency" type, and then put the ink well inside that.
--hixie
Update: Hixie merged a new Ink solution last year. The Ink provides a convenient way to splash over images.
testWidgets('Does the Ink widget render anything', (WidgetTester tester) async {
await tester.pumpWidget(
new Material(
child: new Center(
child: new Ink(
color: Colors.blue,
width: 200.0,
height: 200.0,
child: new InkWell(
splashColor: Colors.green,
onTap: () { },
),
),
),
),
);
Material(
color: Colors.grey[800],
child: Center(
child: Ink.image(
image: AssetImage('cat.jpeg'),
fit: BoxFit.cover,
width: 300.0,
height: 200.0,
child: InkWell(
onTap: () { /* ... */ },
child: Align(
alignment: Alignment.topLeft,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Text('KITTEN', style: TextStyle(fontWeight: FontWeight.w900, color: Colors.white)),
),
)
),
),
),
)
Please Note: I did not test the new Ink Widget. I coped the code from ink_paint_test.dart and the Ink class docs
https://github.com/Hixie/flutter/blob/1f6531984984f52328e66c0cd500a8d517964564/packages/flutter/test/material/ink_paint_test.dart
https://github.com/flutter/flutter/pull/13900
https://api.flutter.dev/flutter/material/Ink-class.html
Screenshot:
Use Ink widget wrapped in an InkWell.
InkWell(
onTap: () {}, // Handle your onTap
child: Ink(
width: 200,
height: 200,
color: Colors.blue,
),
)
The InkWell widget must have a Material widget as an ancestor otherwise it can't show effects. E.g.:
Material(
child : InkWell(
child : .....
you have to add onTap method to see the actual effects as like
Buttons {RaisedButton,FlatButton etc}.
e.g -> Material(
child : InkWell(
onTap : (){}
child : .....
Let's come to the main points see some examples below and try to understand
the actual concepts of InkWell.
In Below example Material is parent of InkWell with onTap but it still not working. Please try to understand the concept of it. You should provide some margin to container or other widget to show the effects. Actually below code working fine but we can't see because we did not provide any margin or align so it has no space to show the effects.
Widget build(BuildContext context) {
return Center(
child: Material(
child: new InkWell(
onTap: () {
print("tapped");
},
child: new Container(
width: 100.0,
height: 100.0,
color: Colors.orange,
),
),
),
);
}
Below example show InkWell effects only to upwards because we provide space {margin}.
Widget build(BuildContext context) {
return Center(
child: Material(
child: new InkWell(
onTap: () {
print("tapped");
},
child: new Container(
margin: EdgeInsets.only(top: 100.0),
width: 100.0,
height: 100.0,
color: Colors.orange,
),
),
),
);
}
Below exp. show the effects in all the page because center create margin from all the side. Centre align it's child widget from top, left, right and bottom.
Widget build(BuildContext context) {
return Center(
child: Material(
child: new InkWell(
onTap: () {
print("tapped");
},
child: Center(
child: new Container(
width: 100.0,
height: 100.0,
color: Colors.orange,
),
),
),
),
);
}
InkWell() will never show the ripple effect until you add the
onTap : () {}
or any of the callbacks like onDoubleTap, onLongPress etc.
parameter inside the InkWell as it starts listening to your taps only when you specify this parameter.
I have found this solution for me. I think it can help you:
Material(
color: Theme.of(context).primaryColor,
child: InkWell(
splashColor: Theme.of(context).primaryColorLight,
child: Container(
height: 100,
),
onTap: () {},
),
)
Color is given to Material widget. It is the default color of your widget.
You can adjust color of ripple effect using splashColor property of Inkwell.
A better way is to use the Ink widget instead of any other widget.
Instead of defining color inside container you can define it in Ink widget itself.
Below code will work.
Ink(
color: Colors.orange,
child: InkWell(
child: Container(
width: 100,
height: 100,
),
onTap: () {},
),
)
Do not forget to add a onTap: () {} in the InkWell else it will
not show the ripple effect too.
If you want a Ripple Effect on any widget just:
1- Wrap widget with Material and Inkwell.
2- set color to widget from Material.
3- Never set color to the widget you want it to ripple.
Material (
color: const Color(0xcffFF8906),
child: InkWell(
ontap:() { },
child: Container(
color: Colors.transparent,
height: 80,
width: 80,
)
Quick solution in easy words:
This solution works in any place of the widget tree.
Just add additional Material before InkWell, like so:
return Container(
.......code.......
),
child: Material(
type: MaterialType.transparency,
child: InkWell(
onTap: () {},
child: Container(
.......code.......
),
),
),
);
Reference:
https://api.flutter.dev/flutter/material/InkWell-class.html
Section called "The ink splashes aren't visible!"
Wrap the InkWell with Material and use there the color that you need:
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: Material(
color: Colors.orange,
child: new InkWell(
onTap: (){ print("tapped"); },
child: new Container(
width: 100.0,
height: 100.0,
),
),
),
),
);
}
}
This is the best way I found and use it always. You can try it.
Wrap your Widget with InkWell
Wrap InkWell with Material
Set the opacity 0% anyhow. e.g.: color: Colors.white.withOpacity(0.0),
Material(
color: Colors.white.withOpacity(0.0),
child: InkWell(
child: Container(width: 100, height: 100),
onTap: (){print("Wow! Ripple");},
),
)
This is working for me:
Material(
color: Colors.white.withOpacity(0.0),
child: InkWell(
splashColor: Colors.orange,
child: Text('Hello'), // actually here it's a Container wrapping an image
onTap: () {
print('Click');
},
));
After trying many answers here, it was a combination of:
Setting splashColor
Wrapping InkWell in Material(color: Colors.white.withOpacity(0.0), ..)
Thanks to the answers here that make those 2 points
child: Material(
color: Colors.transparent
child: InkWell(
onTap: (){ print("this Ripple!";},
splashColor: Colors.greenAccent,
child: Container(
height:100.0,
color: Colors.white,
),
),
),
Add Material before InkWell and set color to Colors.transparent.
I ran into this same problem trying to create an alternating color of InkWell's in a ListView. In that particular case, there's a simple solution: wrap the alternates in a Container that uses a mostly transparent tint/brightness change -- the InkWell touch animation will still be visible beneath it. No Material needed. Note there are other issues when trying to work around this with a Materal -- e.g., it will override a DefaultTextStyle you're using with the default (it installs an AnimatedDefaultTextStyle) which is a huge pain.
After you added onTap:(){} listener, ripple effect should work fine. It doesn't work if you use BoxShadow() with in the InkWell() widget.
I was able to make my situation work by using a Stack.
Stack(
children: [
MyCustomWidget(), // <--- Put this on bottom
Material(
color: Colors.transparent,
child: InkWell(
onTap: () {},
child: Ink(
width: 100,
height: 100,
),
),
),
],
),
The reason that the other answers on this page weren't working for me is that my custom widget hid the ink effect and I didn't have a plain image (so I couldn't use Ink.image).
Edit:
You still may be able to use Ink.image if you convert the image to the right format.
now using MaterialButton, in newer version flutter
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: MaterialButton(
child: Text(
labelText,
style: TextStyle(fontSize: 22),
),
onPressed: () {},
color: backgroundColor,
height: 45,
minWidth: double.infinity,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(16),
),
),
),
);
}
For handling ripple effect you should use the below rules:
onTap shouldn't be null (or empty).
InkWell must be inside any of the Material Widgets
Single Line:
Use splashColor widget in an InkWell.
InkWell(
onTap: () {}, // Handle your onTap
splashColor: Colors.grey,
child: Container(
width: 200,
height: 200,
),
)
You might have set the properties in theme or parent of the material
ThemeData(
...
splashFactory: NoSplash.splashFactory,
...
)
if you don't want splash effect for specific use it explicitly
InkWell(
splashFactory: NoSplash.splashFactory,
...
child: Row(
...
)
);
Make sure you have not added the below properties in the material theme:
(In my case this was the issue)
splashColor: Colors.transparent
splashFactory: NoSplash.splashFactory
eg:-
MaterialApp(
home:......,
theme: ThemeData(
splashFactory: NoSplash.splashFactory, // Remove this
splashColor: AppColors.transparent, // Remove this OR Change the color
......
),
)
Create a widget that supports tap.
Wrap it in an InkWell widget to manage tap callbacks and ripple
animations.
that's point 2 mentioned in documentation
example:
InkWell(
child: TextButton(
onPressed: () {},
child: Text('Tapped'),
),
),
),
I was hit by a similar problem adding an Inkwell to an existing complex Widget with a Container wrapping a BoxDecoration with a color. By adding the Material and Inkwell in the way suggested the Inkwell was still obscured by the BoxDecoration so I just made the BoxDecoration's color slightly opaque which allowed the Inkwell to be seen
The solution to circular widgets, do like below:
Material(
color: Colors.transparent,
child: Container(
alignment: Alignment.center,
height: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
IconButton(
enableFeedback: true,
iconSize: 40,
icon: Icon(
Icons.skip_previous,
color: Colors.white,
size: 40,
),
onPressed: () {}),
IconButton(
iconSize: 100,
enableFeedback: true,
splashColor: Colors.grey,
icon: Icon(Icons.play_circle_filled, color: Colors.white, size: 100),
padding: EdgeInsets.all(0),
onPressed: () {},
),
IconButton(
enableFeedback: true,
iconSize: 40,
icon: Icon(
Icons.skip_next,
color: Colors.white,
size: 40,
),
onPressed: () {}),
],
),
),
)
For me it was another problem. InkWell was not showing ripple effect when i had onTap function as parameter of my widget defined as below.
Function(Result) onTapItem;
...
onTap: onTapItem(result),
I don't know what's the difference, but next code is working well.
onTap: (){ onTapItem(result); },
Adding transparent color to Material worked in my case:
child: new Material(
color: Colors.transparent
child: new InkWell(
onTap: (){},
child: new Container(
width: 100.0,
height: 100.0,
color: Colors.amber,
),
),
),
Instead of providing the color for the Container widget, provide color for the Material widget.
Material(
color: Colors.orange,
child: InkWell(
onTap: () {
print("tapped");
},
child: Container(
width: 100.0,
height: 100.0,
),
),
)