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 将 View 扩展到两个方向,我不知道该怎么做。

最佳答案

更新:

在 Flutter 2.0 中,RaisedButton 已被弃用,取而代之的是 ElevatedButton。您可以像这样使用 minimumSize:

ElevatedButton(
        style: ElevatedButton.styleFrom(
          minimumSize: Size.fromHeight(40), // fromHeight use double.infinity as width and 40 is the height
        ),
        onPressed: () {},
        child: Text('Text Of Button'),
      )

Flutter 低于 2.0 的旧答案:

正确的解决方案是使用 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(...),
)

https://stackoverflow.com/questions/50014342/

相关文章:

Flutter - 在溢出时换行,例如插入省略号或淡入淡出

flutter - 如何在 Flutter 中制作圆角图像

flutter - 如何在 Flutter 小部件中创建超链接?

flutter - 如何在 Flutter 中创建数字输入字段?

flutter - Flutter中文本小部件下的黄线?

flutter - 如何在 flutter 中更改包名称?

flutter - 如何更改 CircularProgressIndicator 的颜色

flutter - 如何关闭屏幕键盘?

flutter - 以编程方式滚动到 ListView 的末尾

flutter - 如何处理不需要的小部件构建?