如何减少ListView.builder中RaisedButton的宽度?

时间:2019-04-08 18:58:25

标签: dart flutter

我似乎无法弄清楚如何减小RaisedButtonListView.builder的宽度。

ListView.builder(
          itemCount: streetList.length,
          itemBuilder: (context, index) {
            bool first = 0 == (index);
            bool last = streetList.length - 1 == (index);
            EdgeInsets itemEdges = EdgeInsets.only(bottom: 20);

            if (first) {
              itemEdges = EdgeInsets.only(top: 50, bottom: 20);
            } else if (last) {
              itemEdges = EdgeInsets.only(bottom: 50);
            }

            return Container(
              margin: EdgeInsets.symmetric(horizontal: 30),
              padding: itemEdges,
              child: SizedBox(
                // height: 50,
                child: RaisedButton(
                  child: Text(streetList[index]),
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => StreetNumberList(
                            widget.peopleList, (streetList[index])),
                      ),
                    );
                  },
                ),
              ),
            );
          }),

我明白了:

enter image description here

我正在尝试减小RaisedButton的宽度,但似乎ListView.builder项始终设置为使用最大宽度。我该如何覆盖?

谢谢!

1 个答案:

答案 0 :(得分:1)

如果您想使用RaisedButton的默认大小,只需将Align小部件添加为父级

    return Container(
                  margin: EdgeInsets.symmetric(horizontal: 30),
                  padding: itemEdges,
                  child: Align(
                      child: RaisedButton(
                        child: Text("index: $index"),
                        onPressed: () {},

                    ),
                  ),
                );

如果要更改大小,请在SizedBox内使用Align

    return Container(
                  margin: EdgeInsets.symmetric(horizontal: 30),
                  padding: itemEdges,
                  child: Align(
                    child: SizedBox(
                      width: 250,
                      child: RaisedButton(
                        child: Text("index: $index"),
                        onPressed: () {},
                      ),
                    ),
                  ),
                );