从列表标题和标题之间删除填充

Jos*_*aza 6 dart flutter

我需要删除ListTile中领先的Widget和标题Widget之间的一些填充。它有太多的空格。有没有办法做到这一点?我为此使用的代码是这样的:

ListTile _getActionMenu(String text, IconData icon, Function() onTap) {
  return new ListTile(
    leading: new Icon(icon),
    title: new Text(text),
    onTap: onTap,
  );
}
Run Code Online (Sandbox Code Playgroud)

小智 45

对于那些现在正在寻找干净的一行答案的人。您可以使用

ListTile(
 horizontalTitleGap: desiredDoubleValue,
);
Run Code Online (Sandbox Code Playgroud)

  • 为什么这个答案没有获得更多赞成票?与“minLeadingWidth”一起使用就像一个魅力。 (6认同)

Rus*_*mov 32

编辑:Flutter 2.0 升级后

正如 Dinesh 在此处指出的那样,ListTile已收到一项minLeadingWidth财产。

默认值为40,因此要通过领导和标题之间减少空间xminLeadingWidth: 40 - x


Align 结果将取决于文本和平铺宽度。

使用Transform.translate了一致的结果。

ListTile(
  leading: Icon(icon),
  title: Transform.translate(
    offset: Offset(-16, 0),
    child: Text('Some text'),
  ),
);
Run Code Online (Sandbox Code Playgroud)


小智 24

您可以在ListTile上使用minLeadingWidth。默认值为 40,因此您可以使用较低的值使行距和标题更接近。

ListTile(
  leading : Icon(Icons.settings),
  title : Text("Settings"),
  minLeadingWidth : 10,
);
Run Code Online (Sandbox Code Playgroud)

  • 哇,我很高兴我滚动到本页底部找到了这个答案! (2认同)
  • 一天后,[`flutter Upgrade` 刚刚升级到 `2.0.0`](https://flutter.dev/docs/development/tools/sdk/releases)。如果我没看错的话,这似乎包括所有 Dev(和 Beta)通道更改(Dev 现在有更新的“2.1.0-10.0.pre”版本)。我仍处于稳定通道,并且此选项 **`minLeadingWidth` 现已可用**。 (2认同)

Jua*_*oza 15

我为此找到的一个丑陋的解决方法是在标题部分放置一个行小部件,然后将图标和文本放置在该行中,中间有一个 SizedBox。通过这种方式,您可以控制它们之间需要多少空间:

ListView(
  shrinkWrap: true,
  children: <Widget>[
    ListTile(
      title: Row(
        children: <Widget>[
          Icon(Icons.android),
          SizedBox(
            width: 5, // here put the desired space between the icon and the text
          ),
          Text("Title") // here we could use a column widget if we want to add a subtitle
        ],
      ),
    )
  ],
)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 这并没有那么难看,低于国际海事组织接受的答案。至少您可以精确控制填充,并且它不依赖于设备的宽高比。 (4认同)

wes*_*tdb 10

您可以使用Align并指定Alignment

ListTile _getActionMenu(String text, IconData icon, Function() onTap) {
  return new ListTile(
    leading: new Icon(icon),
    title: Align(
      child: new Text(text),
      alignment: Alignment(-1.2, 0),
    ),
    onTap: onTap,
  );
}
Run Code Online (Sandbox Code Playgroud)

  • 标题变得不对称 (9认同)

小智 8

将水平标题间隙设置为 0

 ListTile(
                          contentPadding: EdgeInsets.zero,
                          minLeadingWidth: 5,
                          ***horizontalTitleGap: 0,***
                          leading: widget,
                          title: widget ,
                         )
Run Code Online (Sandbox Code Playgroud)


Pla*_*yhi 7

我这几天也有同样的问题,最后我发布了一个可以解决这个问题的包。

该包可在https://pub.dev/packages/list_tile_more_customizable 获得,使用该包我们可以设置水平标题间隙,当它设置为 0.0(零)时,前导和标题之间的填充将变为零。

用法:

ListTileMoreCustomizable(
    title: Text("Title"),
    trailing: Icon(Icons.people),
    horizontalTitleGap: 0.0,
    onTap: (details){},
    onLongPress: (details){},
);
Run Code Online (Sandbox Code Playgroud)

编辑:
这种方法对我来说很好用,而且代码可以在https://github.com/Playhi/list_tile_more_customizable(原始代码太长)下获得 BSD 许可,我很难理解为什么一个用户会失败 -投票的答案没有提交任何问题。

  • 如果您将包重命名为 BetterListTile ,您可能会获得赞成票:P (2认同)
  • 似乎这些修改已经合并到 Flutter 主通道中:https://github.com/flutter/flutter/pull/64222 (2认同)

小智 6

使用水平TileGap:

ListTile(
        horizontalTitleGap: 0,
        leading: CircleAvatar(
          radius: 50,
          backgroundImage:
              NetworkImage("https://picsum.photos/250?image=1"),
        ),
        title: Text("Image 6"),
      ),
Run Code Online (Sandbox Code Playgroud)


小智 5

此代码是解决方案

...
    contentPadding:EdgeInsets.all(0),
...
Run Code Online (Sandbox Code Playgroud)