简体   繁体   English

Flutter:ListView.Separated 设置高度

[英]Flutter: ListView.Separated set height

How to set the height of each ListTile item in a ListView.Separated?如何在 ListView.Separated 中设置每个 ListTile 项目的高度?

I use ListView.Seperated and ListTile item as in the following code:我使用 ListView.Seperated 和 ListTile 项目,如下面的代码所示:

  Widget _LocationItem(LocationModel location) => Container(
        child: Center(
          child: ListTile(
            title: Text(location.name),
            trailing: Icon(Icons.arrow_right),
          ),
        ),
      );

  // *** BUILD WIDGET
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          leading: Icon(Icons.navigate_before),
          title: Text("Location"),
        ),
        body: FutureBuilder<List<LocationModel>>(
          future: _locationModel,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return ListView.separated(
                  itemCount: snapshot.data!.length,
                  separatorBuilder: (_, __) => const Divider(),
                  itemBuilder: (context, index) {
                    var location = snapshot.data![index];

                    return _LocationItem(location);
                  });
            } else
              return Center(child: CircularProgressIndicator());
          },
        ));
  }

This is the result of my code:这是我的代码的结果:

在此处输入图片说明

As you can see, there is quite a lot of extra space at top and bottom of each text.如您所见,每个文本的顶部和底部都有相当多的额外空间。 How to remove the top and bottom extra space?如何去除顶部和底部的额外空间?

I've tried to set the height of the container, but it actually just remove the bottom space and it doesn't look good.我试图设置容器的高度,但它实际上只是删除了底部空间,看起来不太好。

You will need to edit your ListTile try adding properties to it您将需要编辑ListTile尝试ListTile添加属性

try this :试试这个 :


    Widget _LocationItem(LocationModel location) => Container(
        child: Center(
          child: ListTile(
            title: Text(location.name),
            trailing: Icon(Icons.arrow_right),
            contentPadding: , // add padding here
          ),
        ),
      );

Thanks all for the help, just realize that Divider has height property.感谢大家的帮助,只是意识到 Divider 具有高度属性。 For the meantime I just set the height to 0, and the result is better:与此同时,我只是将高度设置为 0,结果更好:

      return ListView.separated(
          itemCount: snapshot.data!.length,
          separatorBuilder: (_, __) => const Divider(
                height: 0,
              ),
          itemBuilder: (context, index) {
            var location = snapshot.data![index];

            return _LocationItem(location);
          });

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

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