小编典典

按钮宽度匹配父项

flutter

我想知道如何设置宽度以 匹配父 版面宽度

new Container(
  width: 200.0,
  padding: const EdgeInsets.only(top: 16.0),
  child: new RaisedButton(
    child: new Text(
        "Submit",
        style: new TextStyle(
          color: Colors.white,
        )
    ),
    colorBrightness: Brightness.dark,
    onPressed: () {
      _loginAttempt(context);
    },
    color: Colors.blue,
  ),
),

我对Expanded窗口小部件了解得很少,但是Expanded将视图扩展到两个方向,我不知道该怎么做。


阅读 446

收藏
2020-08-13

共1个答案

小编典典

正确的解决方案是使用SizedBox.expand窗口小部件,该窗口小部件将强制其child匹配其父级的大小。

SizedBox.expand(
  child: RaisedButton(...),
)

有很多选择,它们或多或少地允许自定义:

SizedBox(
  width: double.infinity,
  // height: double.infinity,
  child: RaisedButton(...),
)

或使用 ConstrainedBox

ConstrainedBox(
    constraints: const BoxConstraints(minWidth: double.infinity),
    child: RaisedButton(...),
)
2020-08-13