简体   繁体   English

删除对话框中列内两个 ListView 之间的空格 - Flutter

[英]Remove whitespace between two ListView inside Column in Dialog - Flutter

I need to show two ListViews inside a dialog;我需要在一个对话框中显示两个 ListView; I am using the showDialog function and in the "content" attribute I added a "SizedBox" without any value for the "height" attribute, and the lists are inside the Column object. Here is the code:我正在使用 showDialog function 并在“content”属性中添加了一个“SizedBox”,而“height”属性没有任何值,列表位于列 object 内。这是代码:

showDialog(
      context: context,
      builder: (context) => AlertDialog(
        content: SizedBox(
          width: double.maxFinite,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: [
              Text('Text1'),
              Expanded(
                child: ListView.builder(
                  shrinkWrap: true,
                  scrollDirection: Axis.vertical,
                  itemCount: listOne.length,
                  itemBuilder: (context, index) {
                      return Text(listOne.elementAt(index).name);
                  },
                ),
              ),
              Text('Text 2'),
              Expanded(
                child: ListView.builder(
                  shrinkWrap: true,
                  scrollDirection: Axis.vertical,
                  itemCount: listTwo.length,
                  itemBuilder: (context, index) {
                      return Text(listTwo.elementAt(index).name);
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );

I have already checked that the item count for both lists is correct.我已经检查过两个列表的项目计数是否正确。 Here is the result:这是结果: 在此处输入图像描述

I want to remove the big whitespace between the two lists if it possibile.如果可能的话,我想删除两个列表之间的大空格。 Hope someone can help me.希望可以有人帮帮我。

Expanded : A widget that expands a child of a Row, Column, or Flex so that the child fills the available space. Expanded :一个小部件,用于扩展 Row、Column 或 Flex 的子级,以便子级填充可用空间。

so by default it will expand fills the full height of screen.所以默认情况下它会扩展到屏幕的整个高度。

Workaround:解决方法:

change with Flexible widget:使用Flexible的小部件进行更改:

unlike Expanded , Flexible does not require the child to fill the available space.Expanded不同, Flexible不需要孩子填充可用空间。

children: [
  Text('Text1'),
  Flexible(
    child: ListView.builder(
      shrinkWrap: true,
      scrollDirection: Axis.vertical,
      itemCount: 3,
      itemBuilder: (context, index) {
          return Text('.name');
      },
    ),
  ),
  Text('Text 2'),
  Flexible(
    child: ListView.builder(
      shrinkWrap: true,
      scrollDirection: Axis.vertical,
      itemCount: 4,
      itemBuilder: (context, index) {
          return Text('.name');
      },
    ),
  ),
],

在此处输入图像描述

The white space is coming for the Expanded widget trying to expand to full size extent of the screen for both lists. Expanded小部件试图扩展到两个列表的屏幕全尺寸范围时出现空白。 if you change the Expanded widget with Sizedbox and give it a height for both list it will remove the white space in between and after.如果您使用Sizedbox更改Expanded小部件并为其两个列表指定高度,它将删除之间和之后的空白。

I hope this helps我希望这有帮助

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

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