首页 > 解决方案 > Flutter - remove space between items in ListView

问题描述

I am using ListView.builder function to create a list of items. However, the space between each item in iOS is huge (screenshot). Do you know how to remove item? It seems it is by default, because I do not adding it.

code:

ListView:

return Scaffold(
    body: ListView.builder(
          itemCount: data.length,
          itemBuilder: (context, index) {
            final model = data[index];
            if (model.path.isEmpty)
              return Divider(color: Colors.grey[500], indent: 40.0);
            else
              return ItemMenu(model.path, model.name);
          }),

);

Item:

return Row(
  crossAxisAlignment: CrossAxisAlignment.center,
  mainAxisAlignment: MainAxisAlignment.start,
  children: <Widget>[
    Image.asset(path, width: 100, height: 100,color: Colors.grey[500]),
    Text(name, style: MyTextTheme().getLightSmallGrey().copyWith(fontSize: 20.0, fontWeight: FontWeight.w700))
  ],
);

iOS Simulator screenshot

标签: androidiosflutterpadding

解决方案


如果您使用 ListTile 小部件作为您的 ListView 项目,则可以添加 dense = true 选项:

ListView(
  children: [
    ListTile(
      dense: true,
      ...
    ),
    ListTile(
      dense: true,
      ...
    ),
  ]
),

推荐阅读