1
                                   @_@

我正在考虑根据动态内容高度使 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 行 pos 12:'hasSize'

**

有没有大神能帮我解决这个问题,我已经纠结了一天了!~@_@

4

1 回答 1

3

您可以在下面复制粘贴运行完整代码
第 1 步:Column使用mainAxisSize: MainAxisSize.min
第 2 步:更改ExpandedFlexible
第 3 步:更改RowWrap
代码片段

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),
        ),
      ),
    );
    ;
  }
}
于 2021-03-25T01:09:14.380 回答