Flutter “RenderFlex 子项具有非零弹性,但传入的高度限制是无限的。”使用 Staggered Grid View 时~ @_@

Flutter “RenderFlex children have non-zero flex but incoming height constraints are unbounded.” when using Staggered Grid View~ @_@

                                   @_@

我正在考虑让 GridView 中的小部件根据它们的动态内容高度具有不同的高度。

小部件如下所示:

Container(
      decoration: BoxDecoration(
        color: someColor,
        borderRadius: BorderRadius.circular(17),
      ),
      child: Column(
          children: [
            ClipRRect(
              borderRadius: BorderRadius.circular(17),
              child: AspectRatio(
                aspectRatio: 5 / 3,
                child: Image.network(someImageUrl, fit: BoxFit.cover),
              ),
            ),
            SizedBox(height: 10),
            Text('Group name'),
            Text('Group desc'),
            SizedBox(height: 10),
            Expanded(
              child: Row(
                children: List.generate(
                  _group.popIds.length,
                  (_) => SizedBox(width: 50, height: 50),
                ),
              ),
            ),
            Text('Hello')
          ],
        ),
    );

这是放置这些小部件的屏幕:

Scaffold(
          body: Padding(
            padding:
                const EdgeInsets.only(left: 17.0, right: 17.0, top: 50),
            child: StaggeredGridView.countBuilder(
              crossAxisCount: 2,
              mainAxisSpacing: 10,
              crossAxisSpacing: 20,
              itemCount: 50,
              itemBuilder: (context, index) => GroupItem(),
              staggeredTileBuilder: (index) => StaggeredTile.fit(1),
            ),
          ),
        );

这是错误消息:

** RenderFlex 子级具有非零弹性,但传入的高度约束是无界的。 & RenderBox 未布局:RenderFlex#f808c relayoutBoundary=up6 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 'package:flutter/src/rendering/box.dart': 断言失败:第 1940 行第 12 行:'hasSize'

**

有没有高手可以帮我解决这个问题,我折腾了一天!~ @_@

您可以复制粘贴运行下面的完整代码
第 1 步:Column 使用 mainAxisSize: MainAxisSize.min
第 2 步:将 Expanded 更改为 Flexible
第 3 步:将 Row 更改为 Wrap
代码片段

child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                ClipRRect(
                  borderRadius: BorderRadius.circular(17),
                  child: AspectRatio(
                    aspectRatio: 5 / 3,
                    child: Image.network("https://picsum.photos/250?image=9",
                        fit: BoxFit.cover),
                  ),
                ),
                SizedBox(height: 10),
                Text('Group name'),
                Text('Group desc'),
                SizedBox(height: 10),
                Flexible(
                  child: Wrap(
                    children: List.generate(
                      Random().nextInt(5) + 1,
                      (_) => SizedBox(width: 50, height: 50),
                    ),
                  ),
                ),
                Text('Hello')
              ],
            ),

工作演示

完整代码

import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'dart:math';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.only(left: 17.0, right: 17.0, top: 50),
        child: StaggeredGridView.countBuilder(
          crossAxisCount: 2,
          mainAxisSpacing: 10,
          crossAxisSpacing: 20,
          itemCount: 50,
          itemBuilder: (context, index) => Container(
            decoration: BoxDecoration(
              color: Colors.blue,
              borderRadius: BorderRadius.circular(17),
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                ClipRRect(
                  borderRadius: BorderRadius.circular(17),
                  child: AspectRatio(
                    aspectRatio: 5 / 3,
                    child: Image.network("https://picsum.photos/250?image=9",
                        fit: BoxFit.cover),
                  ),
                ),
                SizedBox(height: 10),
                Text('Group name'),
                Text('Group desc'),
                SizedBox(height: 10),
                Flexible(
                  child: Wrap(
                    children: List.generate(
                      Random().nextInt(5) + 1,
                      (_) => SizedBox(width: 50, height: 50),
                    ),
                  ),
                ),
                Text('Hello')
              ],
            ),
          ),
          staggeredTileBuilder: (index) => StaggeredTile.fit(1),
        ),
      ),
    );
    ;
  }
}