如何在Flutter中制作方形凸起的按钮?

时间:2019-05-17 05:56:43

标签: android button dart flutter flutter-layout

我在Wrap中有一大堆RaisedButton,因此它们可以占用所需的任意多行。但是我确实想将按钮扩展为方形按钮。我可以通过将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),
          ),
        ],
      ),
    );
  }

Current result

用2作为crossAxisCount代替GridView可以做到这一点,这与我需要的非常接近(理想情况下,文本将更大且换行-如果没有给出正确的空间,似乎会溢出,但是我假设我可以解决这个问题)

Gridview almost there

但是,当我进入横向模式时,例如,轻松地将3个按钮排成一行,GridView就会变成“ eh,every”,并使按钮变大:

GridView bad

2 个答案:

答案 0 :(得分: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),
            ),
          ],
        );
      },
    ),
  );
}

答案 1 :(得分:0)

试试AspectRatio

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