如何使按钮宽度与父级匹配? [英] How to make button width match parent?

查看:17
本文介绍了如何使按钮宽度与父级匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何设置宽度以匹配父级布局宽度

I want to know that how can I set a width to match parent layout width

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

I know about little bit on Expanded widget but Expanded expands view to both direction, i dont know how to do it.

推荐答案

更新:

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

With Flutter 2.0 RaisedButton is deprecated and replaced by ElevatedButton. you can use minimumSize like this:

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 的旧答案:


Old answer for Flutter less than 2.0:

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

The correct solution would be to use the SizedBox.expand widget, which enforces its child to match its parent's size.

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

有很多选择,可以或多或少地进行定制:

There are many alternatives, which allows for more or less customization:

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

或使用 ConstrainedBox

ConstrainedBox(
    constraints: const BoxConstraints(minWidth: double.infinity),
    child: RaisedButton(...),
)

这篇关于如何使按钮宽度与父级匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆