I have seen that I can\'t set the width of a RaisedButton
in Flutter. If I have well understood, I should put the RaisedButton
into a SizedBox
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"),
),
);
Wrap RaisedButton inside Container and give width to Container Widget.
e.g
Container(
width : 200,
child : RaisedButton(
child :YourWidget ,
onPressed(){}
),
)
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),
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: () {},
),
),
),
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),
),
),
)
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(...)
)