简体   繁体   English

Flutter:为什么我的 ListView/ListBuilder 小部件构建时在我的小部件之前有一个空白区域?

[英]Flutter: Why does my ListView/ ListBuilder widget build with an empty space before my widgets?

请注意 ListView 中第一张卡片上方的区域。我希望我的小部件从它所在的位置开始,而不是在它下面

As you can see above my ListView.builder is placing a margin prior to the actual widgets within the list.正如您在上方看到的那样,我的 ListView.builder 在列表中的实际小部件之前放置了一个边距。 Is there any way to avoid this?有什么办法可以避免这种情况吗?

I've looked throughout the widget tree for all of my alignment, padding and margins and I don't believe that it is that.我在整个小部件树中查看了我所有的 alignment、填充和边距,但我不相信是这样。 Additionally, I made a new test ListView to see if it is a problem with that Widget itself and I feel that it is.另外,我做了一个新的测试 ListView 看它是否是那个 Widget 本身的问题,我觉得是的。

注意文本小部件上方的区域

This is the code for the above image.这是上图的代码。

Container(
                margin: const EdgeInsets.all(60),
                decoration: BoxDecoration(
                    border: Border.all(color: Colors.black, width: 5)),
                height: 20,
                width: 20,
                child: ListView(
                  children: const [
                    Text("Test"),
                    Text("Test"),
                    Text("Test"),
                  ],
                )),

How do I remove or ignore this empty space?如何删除或忽略此空白区域?

Try to remove the padding of your ListView.尝试删除 ListView 的填充。 Set this inside your ListView.在您的 ListView 中设置它。

  padding: EdgeInsets.zero,

By default, ListView will automatically pad the list's scrollable extremities to avoid partial obstructions indicated by MediaQuery's padding.默认情况下,ListView 将自动填充列表的可滚动末端,以避免 MediaQuery 的填充指示的部分障碍。 To avoid this behavior, override with a zero padding property.要避免此行为,请使用零填充属性覆盖。

if you are using card widget for this card widget is giving 4 margin at all sides buy default you can eliminate this by giving it explicit zero margin如果您正在使用卡片小部件,因为此卡片小部件在所有方面都提供 4 个保证金购买默认值,您可以通过给它明确的零保证金来消除它

Card(
   margin: EdgeInsets.zero,
   child: ///Your widget
 )

You can try to use padding: EdgeInsets.zero on your ListView and if you want to also shrink the ListView's height to match it's children inside, use also shrinkWrap: true .您可以尝试在 ListView 上使用padding: EdgeInsets.zero ,如果您还想缩小 ListView 的高度以匹配其内部的子级,也可以使用shrinkWrap: true

ListView(
    shrinkWrap: true,
    padding: EdgeInsets.zero,
    children: const [
      Text("Test"),
      Text("Test"),
      Text("Test"),
    ],
  )

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

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