【问题标题】:Flutter: ListView.Separated set heightFlutter:ListView.Separated 设置高度
【发布时间】:2021-10-01 04:10:15
【问题描述】:

如何设置 ListView.Separated 中每个 ListTile 项的高度?

我在下面的代码中使用 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());
          },
        ));
  }

这是我的代码的结果:

如您所见,每个文本的顶部和底部都有很多额外的空间。如何去除顶部和底部多余的空间?

我试过设置容器的高度,但实际上只是去掉了底部的空间,看起来不太好。

【问题讨论】:

  • 有一个separatorBuilder: (_, _) =&gt; hereCanDefineHeight,,可以用SizedBox(), Container()

标签: flutter listview height


【解决方案1】:

您需要编辑您的 ListTile 尝试向其添加属性

试试这个:


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

【讨论】:

    【解决方案2】:

    感谢大家的帮助,刚刚意识到 Divider 有 height 属性。同时我只是将高度设置为0,结果更好:

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

    【讨论】:

      猜你喜欢
      • 2015-03-16
      • 2015-04-14
      • 2021-02-06
      • 2013-09-29
      • 2012-05-01
      • 2012-07-03
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多