【问题标题】:SingleChildScrollView with Column of Flexible Cards: "A RenderFlex Overflowed by x pixels on bottom", How to make card expand?带有灵活卡片列的 SingleChildScrollView:“A RenderFlex 在底部溢出 x 像素”,如何使卡片展开?
【发布时间】:2020-08-26 23:51:08
【问题描述】:

我正在尝试制作一个显示在 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.

卡片的样子:

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

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    解决方案:

    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

    【讨论】:

    • 谢谢库马尔!我解决了我的问题!
    【解决方案2】:

    您必须从 displayVideos 方法中删除 Container

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

    并从 displayVideo 方法移除 Flexible

    【讨论】:

    • 谢谢维伦!我很感激这个解决方案!
    猜你喜欢
    • 2021-06-21
    • 2020-02-18
    • 2021-10-12
    • 2021-09-05
    • 2021-05-23
    • 1970-01-01
    • 2020-09-30
    • 2020-12-29
    • 2020-11-22
    相关资源
    最近更新 更多