【问题标题】:Can't change the height of a button in flutter无法在颤动中更改按钮的高度
【发布时间】:2020-07-25 09:57:40
【问题描述】:

我是新来的颤振。我正在尝试在“AppBar”中插入一个带有文本的简单“包含按钮”。 (例如材料设计“包含按钮”here

问题是,无论我在构造函数中插入什么高度,按钮仍然会填满 AppBar 的整个高度。

我可以像在下面的示例中那样通过设置填充明显地解决问题,但让我感到沮丧的是,我不明白为什么我不能更改按钮本身的高度。 我还尝试用容器或 sizedBox 包装它,如答案here 中所示,但它没有做出任何可见的变化(按钮仍然填充了整个 appBar 的高度)

如果有人能向我解释为什么代码会这样,我将不胜感激。

      appBar: AppBar(
      automaticallyImplyLeading: false,
      title: Text(widget.title),
      actions: <Widget>[
        Padding(
            padding: EdgeInsets.only(top: 7.0, bottom: 7),
            child: Container(
              width: 80,
              child: FlatButton(
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(5.0),
                  ),
                  color: Color.fromRGBO(58, 90, 128, 1),
                  onPressed: () {},
                  child: Text('Button')
              ),
            )
        ),
        ]
  )

【问题讨论】:

    标签: flutter button dart resize materialbutton


    【解决方案1】:

    我认为AppBar() 正在关注material design guidelines for AppBar

    这也与 Material Scaffold() 小部件有关。

    你可以看看这个文档

    在这种特殊情况中,我认为控制高度的最佳方法是使用Padding()。您可以在代码中擦除容器。

    appBar: AppBar(
        automaticallyImplyLeading: false,
        title: Text(widget.title),
        actions: <Widget>[
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: FlatButton(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(5.0),
                ),
                color: Color.fromRGBO(58, 90, 128, 1),
                onPressed: () {},
                child: Text('Button')),
          ),
        ]),
    

    您可以使用PreferredSize() 控制整个 AppBar 的大小,但这与按钮的高度无关。

    【讨论】:

      猜你喜欢
      • 2021-12-01
      • 2021-07-03
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 2016-01-23
      • 2020-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多