dart - Flutter 又抛出一个异常 : RenderBox was not laid out: RenderRepaintBoundary#eaea6 NEEDS-LAYOUT NEEDS-PAINT

标签 dart flutter

我正在使用 SingleChildScrollView 和 Column 来显示滑动条和 gridview。

如果我在我的专栏中使用一些其他小部件,如文本、图像,应用程序显示正常。 但是我的swiper和gridview无法显示,报错:

I/flutter ( 4687): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 4687): The following assertion was thrown during performLayout():
I/flutter ( 4687): RenderFlex children have non-zero flex but incoming height constraints are unbounded.
I/flutter ( 4687): When a column is in a parent that does not provide a finite height constraint, for example if it is
I/flutter ( 4687): in a vertical scrollable, it will try to shrink-wrap its children along the vertical axis. Setting a
I/flutter ( 4687): flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining
I/flutter ( 4687): space in the vertical direction.
I/flutter ( 4687): These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child
I/flutter ( 4687): cannot simultaneously expand to fit its parent.
I/flutter ( 4687): Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits for the flexible
I/flutter ( 4687): children (using Flexible rather than Expanded). This will allow the flexible children to size
I/flutter ( 4687): themselves to less than the infinite remaining space they would otherwise be forced to take, and
I/flutter ( 4687): then will cause the RenderFlex to shrink-wrap the children rather than expanding to fit the maximum
I/flutter ( 4687): constraints provided by the parent.
I/flutter ( 4687): The affected RenderFlex is:
I/flutter ( 4687):   RenderFlex#1f4de relayoutBoundary=up14 NEEDS-LAYOUT NEEDS-PAINT
I/flutter ( 4687): The creator information is set to:
I/flutter ( 4687):   Column ← _SingleChildViewport ← IgnorePointer-[GlobalKey#ae070] ← Semantics ← Listener ←
I/flutter ( 4687):   _GestureSemantics ← RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#e4f77] ←
I/flutter ( 4687):   _ScrollableScope ← _ScrollSemantics-[GlobalKey#4bb86] ← RepaintBoundary ← CustomPaint ←
I/flutter ( 4687):   RepaintBoundary ← ⋯

我曾经是一个 uwp 开发者,刚开始接触 flutter。 谁能帮忙? 这是代码:

class _HomePageState extends State<HomePage> {
  final List<ListItem> _children;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body:SingleChildScrollView(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            Expanded(
                child: Container(
              height: 200,
              child: Swiper(
                itemBuilder: (BuildContext context, int index) {
                  return Image.network(
                    "http://via.placeholder.com/350x150",
                    fit: BoxFit.fitHeight,
                  );
                },
                itemCount: 5,
                pagination: SwiperPagination(),
                control: SwiperControl(),
              ),
            )),
            Expanded(
              child: GridView.builder(
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 4, childAspectRatio: 1.0),
                itemBuilder: (BuildContext context, int index) {
                  return ListItemWidget(_children[index]);
                },
                itemCount: _children.length,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

最佳答案

SingleChildScrollView 中不能有 Expanded 小部件,它具有无限的垂直尺寸。

class _HomePageState extends State<HomePage> {
  final List<ListItem> _children;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            Expanded(
                child: Container(
              height: 200,
              child: Swiper(
                itemBuilder: (BuildContext context, int index) {
                  return Image.network(
                    "http://via.placeholder.com/350x150",
                    fit: BoxFit.fitHeight,
                  );
                },
                itemCount: 5,
                pagination: SwiperPagination(),
                control: SwiperControl(),
              ),
            )),
            Expanded(
              child: GridView.builder(
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 4, childAspectRatio: 1.0),
                itemBuilder: (BuildContext context, int index) {
                  return ListItemWidget(_children[index]);
                },
                itemCount: _children.length,
              ),
            ),
          ],
      ),
    );
  }
}

关于dart - Flutter 又抛出一个异常 : RenderBox was not laid out: RenderRepaintBoundary#eaea6 NEEDS-LAYOUT NEEDS-PAINT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55440315/

相关文章:

dart - 在 Dart 的 Cloud Firestore 中添加文档后如何获取文档 ID

html - 在 AngularDart 中的 Material 选项卡之间切换会破坏子元素

firebase - 使用 Mockito 为 Firebase 用户身份验证设置单元测试

flutter - 如何知道 flutter app 是否处于生产模式?

flutter - TabBar设置tabviews的设置状态

flutter - 如何修复Flutter Web TextFormField不接受也不响应输入?

flutter - 在 Flutter 中将 CircularProgressIndicator 显示在前面

java - 如何在 Flutter/Dart 中从音频文件中获取字节数据(必须是有符号的 int/bytes),就像 AudioInputStream 在 java 中为您提供的那样

android - 如何解决在直接将图像从 android 库共享到我的 flutter 应用程序时收到的奇怪和不完整的 Uri 和路径

flutter - 了解如何听 : false works when used with Provider<SomeType>. of(context, listen: false)