简体   繁体   English

水平listview.builder中的间距

[英]spacing in horizontal listview.builder

How to do auto spacing in listview.builder().For example i need something like MainAxisAlignment.spaceEvenly in column, but i need it in list view.builder().I will have two or three not scrollable horizontal items.So i need spacing between items for different sceen sizes.Could you help me.如何在 listview.builder() 中进行自动间距。例如,我需要在列中使用MainAxisAlignment.spaceEvenly东西,但我在列表 view.builder() 中需要它。我将有两个或三个不可滚动的水平项目。所以我需要不同屏幕尺寸的项目之间的间距。你能帮我吗。

ListView.builder(
              scrollDirection: Axis.horizontal,
              itemCount: 3,//list.length expected two or three
              itemBuilder: (BuildContext context, int index) {
                return Container(
                  height: 100,
                  width: 100,
                  decoration: BoxDecoration(
                    borderRadius: const BorderRadius.all(Radius.circular(10.0)),
                    color: Colors.amber,
                    border: Border.fromBorderSide(
                      BorderSide(
                        color: Color(0xFF8D4AE9),
                        width: 1.0,
                      ),
                    ),
                  ),
                );
              },
            ),

For example, picture enter image description here例如,图片在这里输入图片描述

add margin and padding as much you want.根据需要添加边距和填充。

ListView.builder(
        scrollDirection: Axis.horizontal,
        itemCount: 7,//list.length expected two or three
        itemBuilder: (BuildContext context, int index) {
          return Container(
            height: 100,
            width: 100,
            margin: EdgeInsets.all(10),// add margin 
            padding: EdgeInsets.all(10),// add padding
            decoration: BoxDecoration(
              borderRadius: const BorderRadius.all(Radius.circular(10.0)),
              color: Colors.amber,
              border: Border.fromBorderSide(
                BorderSide(
                  color: Color(0xFF8D4AE9),
                  width: 1.0,
                ),
              ),
            ),
          );
        },
      )

You can try adding a SizedBox with fixed height: or width: or even both between the content you want.您可以尝试在您想要的内容之间添加一个具有固定height:width:或什至两者的SizedBox Another solution is adding Padding with equal values.另一种解决方案是添加具有相等值的Padding

First you have to wrap your list view with the container or card or sized box if you want the horizontal Scroll of the list.首先,如果你想要列表的水平滚动,你必须用容器或卡片或大小的盒子包装你的列表视图。 Next how you just wrap your return width with the padding to the padding to get the padding.接下来如何将返回宽度与填充一起包装到填充以获得填充。

Try this code:试试这个代码:

Container(
      height: 100,width: MediaQuery.of(context).size.width,
      child:
      ListView.builder(
        scrollDirection: Axis.horizontal,
        itemCount: 13,
        itemBuilder: (context, i) {
          return Padding(
            padding: const EdgeInsets.all(8.0),
            child: Container(
              width: 100,
              decoration: BoxDecoration(
                color: Colors.black,
                ),
              ),
            );
        },
      ),
    ),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM