How to set the width of a RaisedButton in Flutter?

后端 未结 20 1905
傲寒
傲寒 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:25

    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"),
      ),
    );
    
    0 讨论(0)
  • 2021-01-30 06:25

    Wrap RaisedButton inside Container and give width to Container Widget.

    e.g

     Container(
     width : 200,
     child : RaisedButton(
             child :YourWidget ,
             onPressed(){}
          ),
        )
    
    0 讨论(0)
  • 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),
    
    0 讨论(0)
  • 2021-01-30 06:29

    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: () {},
        ),
      ),
    ),
    
    0 讨论(0)
  • 2021-01-30 06:29

    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),
                          ),
                        ),
                      )
    
    0 讨论(0)
  • 2021-01-30 06:30

    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(...)
    )
    
    0 讨论(0)
提交回复
热议问题