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

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

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

当前回答

使用媒体查询明智地使用宽度为您的解决方案,这将运行相同的小屏幕和大屏幕

  Container(
            width: MediaQuery.of(context).size.width * 0.5, // Will take 50% of screen space
            child: RaisedButton(
              child: Text('Go to screen two'),
              onPressed: () => null
            ),
          )

您也可以将类似的解决方案应用于SizeBox。

其他回答

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

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

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

这是因为颤振与大小无关。这是关于约束的。

通常我们有两个用例:

小部件的子部件定义约束。父大小本身就是基于该信息的。填充,它接受子约束并增加它。 父节点对子节点强制执行约束。例如:sizebox,但也在拉伸模式列,…

RaisedButton是第一个例子。这意味着按钮定义了它自己的高度/宽度。并且,根据材质规则,凸起的纽扣尺寸是固定的。

您不想要这种行为,因此可以使用第二种类型的小部件来覆盖按钮约束。


无论如何,如果你非常需要这个功能,可以考虑创建一个新的小部件来完成这项工作。或者使用MaterialButton,它拥有一个height属性。

这对我很管用。Container提供高度,FractionallySizedBox提供RaisedButton的宽度。

Container(
  height: 50.0, //Provides height for the RaisedButton
  child: FractionallySizedBox(
    widthFactor: 0.7, ////Provides 70% width for the RaisedButton
    child: RaisedButton(
      onPressed: () {},
    ),
  ),
),

我建议使用MaterialButton,你可以这样做:

MaterialButton( 
 height: 40.0, 
 minWidth: 70.0, 
 color: Theme.of(context).primaryColor, 
 textColor: Colors.white, 
 child: new Text("push"), 
 onPressed: () => {}, 
 splashColor: Colors.redAccent,
)

你可以创建全局方法,比如在整个应用程序中使用按钮。它会根据容器内的文本长度调整大小。FittedBox小部件用于使小部件适合它里面的子部件。

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();
   },
  ),
 );
}

如果你想要按钮的特定宽度和高度,你可以使用RawMaterialButton的约束属性来给出按钮的最小最大宽度和高度

constraints: BoxConstraints(minHeight: 45.0,maxHeight:60.0,minWidth:20.0,maxWidth:150.0),