How to set the width of a RaisedButton in Flutter?

后端 未结 20 1915
傲寒
傲寒 2021-01-30 06:01

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

20条回答
  •  粉色の甜心
    2021-01-30 06:26

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

提交回复
热议问题