我发现无法在扑动中设置提升按钮的宽度。如果我已经很好地理解了,我应该把提升按钮放入大小框中。然后,我将能够设置框的宽度或高度。正确吗?还有别的办法吗?

在每个按钮周围创建一个大小框有点乏味,所以我想知道为什么他们选择这样做。我很确定他们这么做有一个很好的理由,但我不这么认为。 对于初学者来说,脚手架很难阅读和构建。

new SizedBox(
  width: 200.0,
  height: 100.0,
  child: ElevatedButton(
    child: Text('Blabla blablablablablablabla bla bla bla'),
    onPressed: _onButtonPressed,
  ),
),

当前回答

您不需要使用其他小部件来设置大小。你可以为ElevatedButton使用minimumSize

ElevatedButton(
              style: ElevatedButton.styleFrom(
                      minimumSize: const Size(200, 50),
                      elevation: 0,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(50), // <-- Radius
                      ),
                    ),
                    onPressed: (){},
                    child: const Text("Text", style: TextStyle(fontSize: 20, fontWeight: FontWeight.w500),),
                  ),

其他回答

SizedBox(
  width: double.infinity,
  child: ElevatedButton(
    child: Text("FULL WIDTH"),
    onPressed: () {},
  ),
),

使用ElevatedButton,因为RaisedButton已弃用

在Flutter 2.0中,RaisedButton已弃用,并被ElevatedButton取代。

考虑到这一点,为ElevatedButton提供自定义大小的更清晰的方法是ElevatedButton小部件的minimumSize属性。

输出

完整代码

          ElevatedButton(
            style: ElevatedButton.styleFrom(
              primary: Colors.green,
              onPrimary: Colors.white,
              shadowColor: Colors.greenAccent,
              elevation: 3,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(32.0)),
              minimumSize: Size(100, 40), //////// HERE
            ),
            onPressed: () {},
            child: Text('Hey bro'),
          )

注意:也要记住,同样的方法可以用在新的小部件,如TextButton和OutlinedButton使用TextButton。styleFrom(minimumSize: Size(100,40))和outline button。styleFrom(minimumSize: Size(100,40))分别。

我们也可以使用ElevatedButton Widget,它有fixedSize属性。最新Flutter版本

 ElevatedButton(
          onPressed: () {},
          style: ElevatedButton.styleFrom( 
              fixedSize: Size(120, 34), // specify width, height
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(
                20,
              ))),
          child: Text("Search"),
        )

预览

我努力解决这个问题,并发现我的问题是:我在一个ListView中定义了我所有的按钮。不管我做了什么,按钮的宽度总是屏幕的宽度。我用列替换ListView,瞧,突然我可以控制按钮宽度。

已批准的答案现在已弃用,请参阅文档。

要更新从主题到所有按钮的代码大小,您可以这样做:

ThemeData(
    elevatedButtonTheme: ElevatedButtonThemeData(
      style: ButtonStyle(
        fixedSize:MaterialStateProperty.all<Size?>(Size(200.0, 75.0)),
        textStyle:MaterialStateProperty.all<TextStyle?>(TextStyle(fontSize:30))),
    ),
  )