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

时间:2020-05-11 12:27:55

标签: flutter flutter-layout

我正在尝试列出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

2 个答案:

答案 0 :(得分:2)

解决方案:

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));
}

提示:您也可以使用ListView.builder()代替SingleChildScrollViewColumn

答案 1 :(得分:1)

您必须 displayVideos 方法删除容器

module.exports = {
  chainWebpack: (config) => {
    config.resolve.symlinks(false)
   }
}

displayVideo方法删除弹性

相关问题