具有柔性卡列的SingleChildScrollView:“ RenderFlex在底部被x像素溢出”,如何使卡扩展?

我正在尝试制作在SingleChildScrollView中显示的卡片列表。 但是我不知道如何使卡片适合它们的内容。

我已附上一张有关问题的图片,以及我希望卡片的外观。

我试着将它们放在一个扩展中,这似乎没有什么不同。

我也尝试过在卡片容器上设置手动高度,由于某种原因,该高度只能显示两张卡片,而不能让我滚动。

我以为我需要在卡片上设置最小高度,但是我似乎无法弄清楚应该在哪个高度上添加卡片。

Widget displayVideo(item) {
    return Flexible(
      child: Container(
        child: Card(
          child: Column(
            children: <Widget>[
              ListTile(
                leading: CircleAvatar(
                  backgroundImage:
                      NetworkImage(item.fromChannel.channelThumbnail),
                ),
                title: Text(item.fromChannel.channelTitle),
                subtitle: Text(item.publishAt),
              ),
              Container(
                child: new AspectRatio(
                  aspectRatio: 16 / 10,
                  child: new Container(
                    decoration: new BoxDecoration(
                        image: new DecorationImage(
                      fit: BoxFit.fitWidth,
                      alignment: FractionalOffset.center,
                      image: new NetworkImage(item.thumbnailUrl),
                    )),
                  ),
                ),
              ),
              Container(
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(
                    item.title,
                    style: TextStyle(color: Colors.black, fontSize: 16),
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  Widget displayVideos(items) {
    List<Widget> lines = [];
    print(items);
    items.videos.forEach((element) => lines.add(displayVideo(element)));

    return SingleChildScrollView(
        child: Container(
            height: MediaQuery.of(context).size.height,
            child: Column(children: lines)));
  }

  @override
  Widget build(BuildContext context) {
    final ExplorePlaylistArguments args =
        ModalRoute.of(context).settings.arguments;
    playlist = fetchPlaylist(args.playlistId);
    return Scaffold(
        appBar: AppBar(
          title: Text(args.playlistName),
        ),
        body: Container(
          child: FutureBuilder<Playlist>(
              future: playlist,
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  return displayVideos(snapshot.data);
                } else if (snapshot.hasError) {
                  return Text("${snapshot.error}");
                }
                // By default, show a loading spinner.
                return Center(child: CircularProgressIndicator());
              }),
        ));
  }

════════ Exception caught by rendering library ═════════════════════════════════════════════════════
The following assertion was thrown during layout:
A RenderFlex overflowed by 303 pixels on the bottom.

The relevant error-causing widget was: 
  Column file:///C:/Users/Jonathan/AndroidStudioProjects/klp_app/lib/screens/explore_playlist.dart:29:18
The overflowing RenderFlex has an orientation of Axis.vertical.
The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and black striped pattern. This is usually caused by the contents being too big for the RenderFlex.

Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the RenderFlex to fit within the available space instead of being sized to their natural size.
This is considered an error condition because it indicates that there is content that cannot be seen. If the content is legitimately bigger than the available space, consider clipping it with a ClipRect widget before putting it in the flex, or using a scrollable container rather than a Flex, like a ListView.

The specific RenderFlex in question is: RenderFlex#42b06 relayoutBoundary=up19 OVERFLOWING
...  parentData: <none> (can use size)
...  constraints: BoxConstraints(0.0<=w<=384.7, 0.0<=h<=67.9)
...  size: Size(384.7, 67.9)
...  direction: vertical
...  mainAxisAlignment: start
...  mainAxisSize: max
...  crossAxisAlignment: center
...  verticalDirection: down
◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════════════════════════
A RenderFlex overflowed by 303 pixels on the bottom.

卡的外观:

How it's looks like

我希望每张卡片的外观如何:

How I want the cards to look like

评论
  • 魂淡
    魂淡 回复

    解:

    Widget displayVideo(item) {
    return Card(
      child: Column(
        children: <Widget>[
          ListTile(
            leading: CircleAvatar(
              backgroundImage:
                  NetworkImage(item.fromChannel.channelThumbnail),
            ),
            title: Text(item.fromChannel.channelTitle),
            subtitle: Text(item.publishAt),
          ),
          new AspectRatio(
            aspectRatio: 16 / 9,
            child: Image.network(item.thumbnailUrl, fit:BoxFit.fitWidth),
              ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Text(
             item.title,
              style: TextStyle(color: Colors.black, fontSize: 16),
            ),
          )
        ],
      ),
    );
    }
    
    Widget displayVideos(items) {
       List<Widget> lines = [];
       items.forEach((element) => lines.add(displayVideo(element)));
    
       return SingleChildScrollView(
           child: Column(children: lines));
    }
    

    Tip: You can also use ListView.builder() instead of SingleChildScrollView and Column

  • 薄荷凉
    薄荷凉 回复

    您必须从displayVideos方法中删除Container。

    return SingleChildScrollView(child: Column(children: lines)); // container remove
    

    然后从displayVideo方法中删除Flexible。