【问题标题】:How to place an image below a listview in flutter?如何在颤动的列表视图下方放置图像?
【发布时间】:2023-04-11 02:39:01
【问题描述】:

使用这个简单的设计,如何在列表视图下显示第二张图片?实际上,该列表将从 Firebase 中获取,其中每个项目都是一个 ExpansionTile,因此列表视图的高度无法固定。

该列应该是可滚动的,以便在向下滚动列表下方时可以看到完整的图像。

import 'package:flutter/material.dart';

List<Widget> list = <Widget>[
  ListTile(
    title: Text('CineArts at the Empire',
        style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
    subtitle: Text('85 W Portal Ave'),
    leading: Icon(
      Icons.theaters,
      color: Colors.blue[500],
    ),
  ),
  ListTile(
    title: Text('The Castro Theater',
        style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
    subtitle: Text('429 Castro St'),
    leading: Icon(
      Icons.theaters,
      color: Colors.blue[500],
    ),
  ),
];

class CartWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Column(children: <Widget>[
      Image.network("https://via.placeholder.com/350x150"),
      Expanded(
        child: ListView(
          children: list,
        ),
      ),
      Image.network("https://via.placeholder.com/350x500"), // error: hides above widget
    ]));
  }
}

【问题讨论】:

    标签: dart flutter flutter-layout


    【解决方案1】:

    我理解您的问题的方式是,您希望底部图像出现在列表视图内而不是在其下方,就好像它只是另一个项目。解决方案:让它成为另一个项目!

    更具体地说,这是您使用图像丰富列表的辅助函数的实现方式可能如下所示:

    List<Widget> _buildListWithFooterImage(List<Widget> items) {
      return items.followedBy([
        Image.network("https://via.placeholder.com/350x500")
      ]);
    }
    

    然后,您可以在构建过程中使用该功能:

    Widget build(BuildContext context) {
      return SafeArea(
        child: Column(
          children: <Widget>[
            Image.network("https://via.placeholder.com/350x150"),
            Expanded(
              child: ListView(
                children: _buildListWithFooterImage(list)
             )
            ),
          ]
        )
      );
    }
    

    另外,我相信您的问题类似于this one

    【讨论】:

    • 谢谢......虽然将图像放在列表视图中似乎是一种黑客行为。您的解决方案不会隐藏列表视图之后的任何列子项吗?
    • 一点也不!在布局期间,Column 按以下顺序布置其子级:首先是不灵活的(如顶部图像或ListView 下方的任何自定义),然后才是灵活的(如Expanded 小部件)。所以ListView 总是占用剩余的可用空间,不多也不少。
    • 我猜列表视图不打算放在列中?在我的情况下,列表是使用 .toList() 生成的,所以 .followedBy 不起作用,但我使用 .toList() + [Image.network(...)] 让它工作
    • 太棒了,我不知道Lists 会覆盖+ 运算符...很高兴知道。是的,虽然您可以在 Flutter 中非常灵活地编写小部件,但 ListViews 并不打算用作 Columns 的直接子代。原因是ListViews 占用了他们可以使用的所有可用空间,Columns 为每个孩子提供了无限的高度限制,作为与他们交流的一种方式,使其尽可能小。欲了解更多信息,请查看this interesting Google Tech Talk about Flutter's rendering pipeline
    猜你喜欢
    • 2017-12-22
    • 2020-01-25
    • 1970-01-01
    • 2020-11-20
    • 2019-02-09
    • 2023-03-07
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    相关资源
    最近更新 更多