【问题标题】:How to make square raised buttons in Flutter?如何在 Flutter 中制作方形凸起按钮?
【发布时间】:2019-05-17 05:56:43
【问题描述】:

我在 Wrap 中有一大堆 RaisedButtons,这样它们就可以根据需要占用尽可能多的行。但我确实想将按钮扩展为方形按钮。我可以通过将Wrap 替换为GridView.count 并使用crossAxisCount 来做到这一点,但是当有更多可用空间时,按钮会变得不必要地大。基本上,我希望它们都是相同大小的正方形,都只有它们所容纳的内容那么大。它们不是动态加载的或其他任何东西,因此无需担心性能。但是我们正在研究相当小的屏幕的可能性,因此滚动也需要成为可能。有没有办法让所有这些事情都正确?

这是当前代码及其产生的结果:

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Wrap(
        direction: Axis.horizontal,
        children: <Widget>[
          RaisedButton.icon(
            onPressed: () {}
            label: Text('Park In', style: TextStyle(fontSize: 15.0)),
            icon: Icon(Icons.add),
          ),
          RaisedButton.icon(
            onPressed: () {}
            label: Text('Park Out', style: TextStyle(fontSize: 15.0)),
            icon: Icon(Icons.eject),
          ),
          RaisedButton.icon(
            onPressed: () {}
            label: Text('Maintainence In', style: TextStyle(fontSize: 15.0)),
            icon: Icon(Icons.vertical_align_bottom),
          ),
          RaisedButton.icon(
            onPressed: () {}
            label: Text('Maintainence Out', style: TextStyle(fontSize: 15.0)),
            icon: Icon(Icons.vertical_align_top),
          ),
          RaisedButton.icon(
            onPressed: null,
            label: Text('Move', style: TextStyle(fontSize: 15.0)),
            icon: Icon(Icons.open_with),
          ),
        ],
      ),
    );
  }

用 2 替换 GridView 作为crossAxisCount 给出了这个,这非常接近我需要的(理想情况下,文本会更大并且换行 - 如果没有给出正确的空间,它似乎会溢出,但我想我可以解决这个问题):

但是当我进入例如横向模式时,可以轻松地将 3 个按钮放在一行中,GridView 只是“嗯,随便”,并使按钮变得巨大:

【问题讨论】:

    标签: android button dart flutter flutter-layout


    【解决方案1】:

    您可以为此使用OrientationBuilder。它将为您处理方向更改:

    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text("word"),
        ),
        body: OrientationBuilder(
          builder: (context, orientation) {
            int count = 2;
            if(orientation == Orientation.landscape){
              count = 3;
            }
            return GridView.count(
              crossAxisCount: count,
              children: <Widget>[
                RaisedButton.icon(
                  onPressed: () {},
                  label: Text('Park In', style: TextStyle(fontSize: 15.0)),
                  icon: Icon(Icons.add),
                ),
                RaisedButton.icon(
                  onPressed: () {},
                  label: Text('Park Out', style: TextStyle(fontSize: 15.0)),
                  icon: Icon(Icons.eject),
                ),
                RaisedButton.icon(
                  onPressed: () {},
                  label:
                      Text('Maintainence In', style: TextStyle(fontSize: 15.0)),
                  icon: Icon(Icons.vertical_align_bottom),
                ),
                RaisedButton.icon(
                  onPressed: () {},
                  label:
                      Text('Maintainence Out', style: TextStyle(fontSize: 15.0)),
                  icon: Icon(Icons.vertical_align_top),
                ),
                RaisedButton.icon(
                  onPressed: null,
                  label: Text('Move', style: TextStyle(fontSize: 15.0)),
                  icon: Icon(Icons.open_with),
                ),
              ],
            );
          },
        ),
      );
    }
    

    【讨论】:

    • 不是说能适应的按钮数量会因为屏幕大小而变化吗?虽然我想然后用宽度和高度动态计算它可以工作。很公平,谢谢!
    【解决方案2】:

    试试AspectRatio

    AspectRatio(
      aspectRatio: 1,
      child: RaisedButton(
        onPressed: () {},
        child: Text('hi'),
      ),
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-21
      • 2020-04-10
      • 2023-01-09
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      • 2017-05-10
      相关资源
      最近更新 更多