I have two buttons in the bottom of the screen in a single row. i want to keep first button in center and second button in end. how can i achieve that?
Here is my Code
new Container
(
child:Row(
children:<Widget>[
new RaisedButton(
color: Colors.red,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
onPressed: takescrshot,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Next',
style:
TextStyle(fontSize: 23, color: Colors.white),
),
),
),
new FloatingActionButton.extended(
onPressed: () {
// Add your onPressed code here!
},
label: Text('options'),
icon: Icon(Icons.menu),
backgroundColor: Colors.pink,
),
])
)
Can anyone guide me?
You need to understand how Column and Row work to position its children and how to properly use the Flexible widgets to make every child position as you want.
You have a Row there with two buttons as a children (one RaisedButton and a FloatingActionButton) but they aren't being restricted to any position, so they're both lay down on the start of the Row aligned at the left.
First, let's make it so both fit all the space available horizontally. For that, you'll need to wrap each button with an Expanded.
Right now, you'll probably have two stretched buttons using 50/50 of the space but looking ugly. If so, you'll probably want to wrap each button with a Center so they are centered within the space they have from each Expanded. If not, skip this step. Looking better now, right?
Now, we have 2 buttons evenly centered on the screen with all the space available, but we want to make sure that one is centered and the other on the right, regardless of the screen size. For that, we add a const Spacer() as the first child of the Row so it actually share "invisible" space with all the other 2 siblings;
Right now, you should have what you are looking for, and working flawlessly for all screen sizes.
TL;DR:
Container(
child: Row(
children: <Widget>[
const Spacer(),
Expanded(
child: Center(
child: RaisedButton(
color: Colors.red,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
onPressed: takescrshot,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Next',
style: TextStyle(fontSize: 23, color: Colors.white),
),
),
),
),
),
Expanded(
child: Center(
child: FloatingActionButton.extended(
onPressed: () {
// Add your onPressed code here!
},
label: Text('options'),
icon: Icon(Icons.menu),
backgroundColor: Colors.pink,
),
),
),
],
));
use Spacer widget like this:
new Container
(
child:Row(
children:<Widget>[
Spacer(),
new RaisedButton(
color: Colors.red,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
onPressed: takescrshot,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Next',
style:
TextStyle(fontSize: 23, color: Colors.white),
),
),
),
Spacer(),
new FloatingActionButton.extended(
onPressed: () {
// Add your onPressed code here!
},
label: Text('options'),
icon: Icon(Icons.menu),
backgroundColor: Colors.pink,
),
])
)
You can do it by faking a same child like the Options button at the start of the Row and making it invisible. Also need to set the Row's maninAxisAlignment to spaceBetween like this..
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Opacity(
opacity: 0,
child: FloatingActionButton.extended(
onPressed: null,
label: Text('options'),
icon: Icon(Icons.menu),
backgroundColor: Colors.pink,
),
),
new RaisedButton(
color: Colors.red,
shape:RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
onPressed: () {},
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Next',
style: TextStyle(fontSize: 23, color: Colors.white),
),
),
),
new FloatingActionButton.extended(
onPressed: () {
// Add your onPressed code here
},
label: Text('options'),
icon: Icon(Icons.menu),
backgroundColor: Colors.pink,
),
],
),
)
Related
We need to move the icons to the end of the Card and with a distance of 17px between them. I tried in different ways and align, but for some reason I can not move the icons to the end. I would be grateful if you tell me how to do it right.
Scrollbar(
isAlwaysShown: true,
thickness: 2,
scrollbarOrientation: ScrollbarOrientation.left,
child: ListView.builder(
itemCount: close.length,
itemBuilder: (context, index) => Padding(
padding: const EdgeInsets.only(left: 9),
child: SizedBox(
height: 47,
child: Card(
color: constants.Colors.greyMiddle,
margin: const EdgeInsets.only(
bottom: 4,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8)),
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 9, vertical: 5),
child: Row(
// mainAxisAlignment: MainAxisAlignment.start,
children: [
const CircleAvatar(
backgroundColor: constants.Colors.white,
),
const SizedBox(
width: 11,
),
Text(
close[index],
style: constants.Styles.smallerLtStdTextStyleWhite,
),
IconButton(
onPressed: () {},
icon: SvgPicture.asset(constants.Assets.barPoynts)),
IconButton(
onPressed: () {},
icon: SvgPicture.asset(constants.Assets.crypto)),
IconButton(
onPressed: () {},
icon: SvgPicture.asset(
constants.Assets.barFavourites)),
],
),
),
),
),
),
),
),
You should wrap your Text widget with the Expanded widget, Which allocates the max-width to your Text so if your text gets bigger in length it will wrap it in a new line.
Expanded(
child: Text(
close[index],
style: constants.Styles.smallerLtStdTextStyleWhite,
),
),
Output:
If you use only the Spacer widget as per the other answer (which will do the work) but if your text length got bigger it will result in an overflow error.
Like below:
Also Do not use Spacer and Expanded together, as Expanded's default flex and Spacer's default flex are set to 1, flutter will allocate 'em equal amount of space and which will again cause an issue for you.
for example:
You can try putting a Spacer() before the IconButtons. So like
children: [
const CircleAvatar(
backgroundColor: constants.Colors.white,
),
const SizedBox(
width: 11,
),
Text(
close[index],
style: constants.Styles.smallerLtStdTextStyleWhite,
),
const Spacer(),
IconButton(
onPressed: () {},
icon: SvgPicture.asset(constants.Assets.barPoynts)),
IconButton(
onPressed: () {},
icon: SvgPicture.asset(constants.Assets.crypto)),
IconButton(
onPressed: () {},
icon: SvgPicture.asset(
constants.Assets.barFavourites)),
],
Spacers always take as much space as they can
{Text(close[index],
style: constants.Styles.smallerLtStdTextStyleWhite,
),}
After this, Write
Expanded(child:Container()),
And then icons. It will make space of container with empty area in Row.
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.
I'm having issues where I want to make a button (any kind to achieve this) that has an icon that is always in the same position at the start of the button, and that the text of the button is always centered compared to the text below it and always centered in the button itself, even if the text is dynamic. This is what I want to achieve:
The button text is centered according to the text below(that stays the same) and it is centered with the above button, but if the button text changes dynamically, then I have these issues:
Or like this one:
Any ideas how to solve this problem?
I tried with the Expanded widget and adding the flex properties but failed. I tried some other stuff too but failed also... Here is the button code:
TextButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.blue)),
onPressed: () {},
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Expanded(
flex: 1,
child: Icon(
Icons.keyboard_arrow_right_sharp,
color: Colors.white,
),
),
Expanded(
flex: 4,
child: Center(
child: Text(
"SOME TEXT",
style: TextStyle(color: Colors.white),
),
),
)
],
),
),
Thanks in advance for your help!
There are a couple of options to do that. I'm going to assume the size of the text could change so you want a flexible solution.
The first option is the most obvious of the two, but will break if the text is too long. The trick is to add an empty sizedBox at the end of the button, so the text can be centered in an expanded widget.
TextButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
),
onPressed: () {},
child: Row(
children: [
Icon(
Icons.keyboard_arrow_right_sharp,
color: Colors.white,
size: 24,
),
Expanded(
child: Center(
child: Text(
"SOME TEXT",
style: TextStyle(
color:Colors.white,
),
),
),
),
SizedBox(
width: 24, // width of the icon
),
],
),
);
The other option is using a stack widget. This way, if the text is too long it will overlap with the icon but it's less probable that it will overflow the button.
TextButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
),
onPressed: () {},
child: Stack(
children: [
Positioned(
left: 0,
child: Icon(
Icons.keyboard_arrow_right_sharp,
color: Colors.white,
size: 24,
),
),
Center(
child: Text(
"SOME TEXT",
style: TextStyle(
color:Colors.white,
),
),
),
],
),
);
Let's discuss about you code :
using row with a mainaxisalignment from start make the element start from the available area. then i suggest to use the SizedBox to make some of spacing between the elements as you want and play with the width of it.
TextButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.blue)),
onPressed: () {},
child: Row(
mainAxisAlignment: MainAxisAlignment.start, // here the issue
children: [
Expanded(
flex: 1,
child: Icon(
Icons.keyboard_arrow_right_sharp,
color: Colors.white,
),
),
SizedBox(width: MediaQuery.of(context).size.width * 0.08, // change this value as you want 0.08 or 0.1 ...etc
),
Expanded(
flex: 4,
child: Center(
child: Text(
"SOME TEXT",
style: TextStyle(color: Colors.white),
),
),
)
],
),
),
for more read this documentation
How to add icon to RaisedButton at the right side
Padding(
padding: const EdgeInsets.only(top: 15.0),
child: new SizedBox(
width: double.infinity,
child: new RaisedButton(
textColor: Colors.white,
color: coloraccent,
onPressed: () {},
child: const Text('UPADATE'),
)),
),
You can just wrap your RaisedButton.icon to the Directionality widget:
Directionality(
textDirection: TextDirection.rtl,
child: RaisedButton.icon(
onPressed: () {},
color: Colors.deepPurple,
icon: Icon(
Icons.arrow_drop_down,
color: Colors.white,
),
label: Text(
"Category",
style: TextStyle(color: Colors.white),
),
),
)
You can try this out, it works fine for me.
child: RaisedButton(
onPressed: () => navigateToLogin(context),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Continue",style: TextStyle(fontSize: 20)),
Icon(Icons.navigate_next)
],
),
use RaisedButton.icon constructor
Padding(
padding: const EdgeInsets.only(top: 15.0),
child: SizedBox(
width: double.infinity,
child: RaisedButton.icon(
icon: Icon(Icons.translate),
textColor: Colors.white,
color: Colors.lightBlue,
label: const Text('UPADATE'),
onPressed: () {},
)),
),
give a child to raised button like that
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween
children: <Widget>[
Text('some text'),
Icon(Icons.home),
],
),
I tried to use RaisedButton.Icon but it was not enough for me, because I usually customize the buttons with many design styles, so I used some techniques for placing text and Icon with RaisedButton.
You can try this, It worked for me. Let me explain in the code.
// Create a Raised Button.
RaisedButton(
color: Colors.pinkAccent, // Color pinkAccent
child: Row( // Add a Row Widget for placing objects.
mainAxisAlignment: MainAxisAlignment.center, // Center the Widgets.
mainAxisSize: MainAxisSize.max, // Use all of width in RaisedButton.
children: <Widget>[
Padding(
padding: EdgeInsets.all(5.0), // Give to the text some space.
child: Text(
"Enviar",
style: TextStyle(
fontSize: 18, // 18pt in font.
color: Colors.white, // You can ommit it if you use textColor in RaisedButton.
),
),
),
Icon(
Icons.send, // Send Icon. (Papper Plane Icon)
color: Colors.white, // White Color. You can ommit it too if you use textColor property on RaisedButton.
size: 18, // 18 pt, same as text.
),
],
),
onPressed: () {}, /// For enabling the button
),
Use RaisedButton.icon:
Doc says: Create a filled button from a pair of widgets that serve as
the button's icon and label.
The icon and label are arranged in a row and padded by 12 logical
pixels at the start, and 16 at the end, with an 8 pixel gap in
between.
RaisedButton.icon(
icon: Icon(Icons. ...), // <-- Icon you want.
textColor: Colors.white,
color: Colors.lightBlue,
label: const Text('UPADATE'), // <-- Your text.
onPressed: () {},
)),
use this code. you can add your required width as per text length
Container(
height: 35.0,
width: 95,
child: RaisedButton(
elevation: 10,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(15.0),
),
),
color: Colors.white,
child: Row(
children: <Widget>[
Text('Call', style: TextStyle(color: Colors.black)),
SizedBox(width: 10),
Icon(Icons.call),
],
),
onPressed: () {},
),
);
If you still resist using RaisedButton.Icon instead of using the normal RaisedButton with Row widget as a child
Then you can swap between label and icon attributes because they both take a widget.
Like this:
RaisedButton.icon(
icon: const Text('UPADATE'),
label: Icon(Icons.translate),
textColor: Colors.white,
color: Colors.lightBlue,
onPressed: () {},
)
Here is my two buttons, Back and Next
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
RaisedButton(
color: const Color(0xFFFFB822), // Color pinkAccent
child: Row(
// Add a Row Widget for placing objects.
mainAxisAlignment: MainAxisAlignment.center, // Center the Widgets.
mainAxisSize: MainAxisSize.max, // Use all of width in RaisedButton.
children: <Widget>[
SizedBox(height: 10, width: 10),
Icon(
Icons.arrow_back_ios, // Send Icon. (Papper Plane Icon)
color: Colors.black, // White Color. You can ommit it too if you use textColor property on RaisedButton.
size: 18, // 18 pt, same as text.
),
Padding(
padding: EdgeInsets.all(5.0), // Give to the text some space.
child: Text(
"Back",
style: TextStyle(
fontWeight: FontWeight.w900,
color: Colors.black, // You can ommit it if you use textColor in RaisedButton.
),
),
),
],
),
onPressed: () {},
/// For enabling the button
),
RaisedButton(
color: const Color(0xFFFFB822), // Color pinkAccent
child: Row(
// Add a Row Widget for placing objects.
mainAxisAlignment: MainAxisAlignment.center, // Center the Widgets.
mainAxisSize: MainAxisSize.max, // Use all of width in RaisedButton.
children: <Widget>[
Padding(
padding: EdgeInsets.all(5.0), // Give to the text some space.
child: Text(
"Next",
style: TextStyle(
fontWeight: FontWeight.w900,
color: Colors.black, // You can ommit it if you use textColor in RaisedButton.
),
),
),
Icon(
Icons.arrow_forward_ios, // Send Icon. (Papper Plane Icon)
color: Colors.black, // White Color. You can ommit it too if you use textColor property on RaisedButton.
size: 18, // 18 pt, same as text.
),
],
),
onPressed: () {},
/// For enabling the button
),
],
),
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}"),
),
],
),
),