Flutter, how to remove spaces around text in a TextButton? - flutter

As titled, padding: EdgeInsets.zero doesn't seem to work.
Container(
height: 30,
width: 60,
padding: EdgeInsets.zero,
decoration: BoxDecoration(
color: Colors.green, borderRadius: BorderRadius.circular(6)),
child: Padding(
padding: EdgeInsets.zero,
child: TextButton(
child: Text('Login', style: TextStyle(fontSize: 14, color: Colors.white)),
onPressed: () {},
),
),
),

From your comment:
The text Login doesn’t fully shown, I would like to maintain the size of the green box, while making the size of the text inside as big as possible.
You can do that like this. No need for Containers
ElevatedButton(
onPressed: () {},
child: Text('Login', style: TextStyle(fontSize: 18)),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.green),
padding: MaterialStateProperty.all(EdgeInsets.zero),
),
),
You can also change the minimum size of the widget by adding minimumSize to the ButtonStyle. For example:
minimumSize: MaterialStateProperty.all(Size(10, 10)),
This will make the minimum size 10x10, if your text size is small enough.
But keep in mind that you need to be able to easily press the button too.
Also check this:
https://api.flutter.dev/flutter/material/ButtonStyle-class.html

Try this: Wrap your container in this
MediaQuery.removePadding(
context:context,
removeTop:true,
removeBottom:true,
child: Container(....)
)

Related

how to remove the space between the lines(rows) of a wrap widget (flutter)?

This is my code for wrap widget which is having multiple material button representing tags, i don't know why there is a vertical space between each new row in the wrap widget
Container(
width: double.infinity,
child: Wrap(
children: [
MaterialButton(
color: Colors.black,
minWidth: 1,
height: 25,
padding: EdgeInsets.zero,
onPressed: () {},
child: Text(
"#software",
style: TextStyle(color: defaultColor),
),
),
MaterialButton(
minWidth: 1,
height: 25,
padding: EdgeInsets.zero,
onPressed: () {},
child: Text(
"#software",
style: TextStyle(color: defaultColor),
),
),
MaterialButton(
minWidth: 1,
height: 25,
padding: EdgeInsets.zero,
onPressed: () {},
child: Text(
"#software",
style: TextStyle(color: defaultColor),
),
),
MaterialButton(
minWidth: 1,
height: 25,
padding: EdgeInsets.zero,
onPressed: () {},
child: Text(
"#software",
style: TextStyle(color: defaultColor),
),
),
MaterialButton(
minWidth: 1,
height: 25,
padding: EdgeInsets.zero,
onPressed: () {},
child: Text(
"#software",
style: TextStyle(color: defaultColor),
),
),
MaterialButton(
minWidth: 1,
height: 25,
padding: EdgeInsets.zero,
onPressed: () {},
child: Text(
"#software",
style: TextStyle(color: defaultColor),
),
),
],
),
),
this is a picture describing the vertical space between the line , i reached a solution by set the running space to negative values and that worked for me, but i feel like its a bad practiceenter image description here
Don't use MaterialButton directly. Use one of its descendants: ElevatedButton, TextButton or OutlinedButton.
In your case, TextButton is the closet to what you want to achieve, so you can use that as a starting point and then further customize its style.
Or, if you just want to allow clicking on the blue text, use GestureDetector or InkWell instead of using buttons.
you can use two property here for controlling the space
spacing
runSpacing
please test it and share with us about the result.
In your case I would use more common buttons such as
https://www.notion.so/BUTTONS-IN-FLUTTER-f596d17ba17f418381b30ed140cd9239
and I would create a generalized button sending as parameters what I need from each button, so as not to generate more unnecessary code, you could also use column or row with mainAxisAlignment: MainAxisAlignment.spaceEvenly,
to align your buttons.

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

How can i make flat button size as it's child size

i want to make button size like it's text child size
i will attach example photo , and i'll attach the code that i write
this is my code :
Expanded(
child: FlatButton(
minWidth: double.infinity,
height: 20,
child: FlatButton(
onPressed: (){},
color: Colors.grey,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10) ,
),
child: Text(
'SHOP' ,
style: TextStyle(
color: Colors.blueAccent,
fontWeight: FontWeight.bold ,
fontSize: 16 ,
),
and this is the photo
photo
How about this?
RawMaterialButton(
constraints: BoxConstraints(),
padding: EdgeInsets.fromLTRB(14, 4, 14, 4),
elevation: 0,
onPressed: () {},
fillColor: Color(0xFFF2F2F2),
shape: StadiumBorder(),
child: Text(
'SHOP',
style: TextStyle(
color: Colors.blueAccent,
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
),
Your question is confusing to me, I am not sure whether you want your button as big as the parent's child (as you stated in the title), or if you want it as big as the Text child (as you stated in the description of your question).
If what you want is the button to be as big as the parent, consider using LayoutBuilder and SizedBox to make the button as big as the available surrounding space. You can do something like this:
LayoutBuilder(
builder: (context, constraints) {
return SizedBox(
height: constraints.maxHeight,
width: constraints.maxWidth,
child: FlatButton(
...
)
)
}
)
If you want the button to be as big as its child, I believe you don't need to worry about anything other than providing the proper Text widget. FlatButton will figure out its size accordingly.
GestureDetector{
onTap:(){},
child:Container(
child:Material(
shape:RoundRectangleBorder()
child:Center(
child:'Text')))
}
You can use properties of Material widget this way. If you don't provide height to container it will adjust to the child size. i.e. text size
Try this for button function:
Expanded(
//Modify below factor as your requirement
flex: 1,
child: InkWell(
onTap: () {},
child: Container(
alignment: Alignment.center,
//Modify width,height as your requirement
height: double.infinity,
width:double.infinity,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: Colors.blueAccent.withAlpha(30),
),
child: Text('SHOP',
style: TextStyle(
color: Colors.blue,
fontWeight: FontWeight.bold,
fontSize: 20)),
),
)),

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