错误:RenderBox 未布局:NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE

Gbe*_*uga 35 dart flutter

我以SingleChildScrollView这种方式使用时遇到问题,我不知道出了什么问题,我不断收到此错误。

\n

如果添加 SingleChildScrollView,页面将为空白(不会显示所有小部件),但如果我删除 SingleChildScrollView,页面将显示。

\n
\n

RenderBox 未布局:RenderPadding#583c0 relayoutBoundary=up1\nNEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE\n'package:flutter/src/rendering/box.dart':断言失败:第 1929 行\npos 12:'hasSize

\n

导致错误的相关小部件是 Scaffold\nScaffold:file:///Users/mac/Documents/Uneleap-Platform-master/lib/screens/Pages/forum/forum.dart:25:12

\n
\n

这是代码

\n
SafeArea(\n                minimum: EdgeInsets.only(left: 25.0, right: 20.0, top: 10.0),\n                child: SingleChildScrollView(\n                  child: Column(\n                    crossAxisAlignment: CrossAxisAlignment.start,\n                    mainAxisSize: MainAxisSize.min,\n                    children: [\n                      WidgetForum(\n                        details: 'The Get Is Not Geting together ?',\n                        name: 'Nina Simon',\n                        url: 'assets/dashboard_pic.png',\n                      ),\n                      SizedBox(\n                        height: 5,\n                      ),\n                      WidgetForum(\n                        details:\n                            '''Notes is designed for whatever\xe2\x80\x99s on your mind.\\nJot down your thoughts. Download Notes.\\nThere is something wonderful in writing. \\nThe Get Is Not Getting Together?''',\n                        name: 'James Nugar',\n                        url: 'assets/dashboard_pic.png',\n                      ),\n                      Text(\n                        'Topics',\n                        style: TextStyle(\n                          fontSize: 35,\n                        ),\n                      ),\n                      Expanded(\n                        child: ListView.separated(\n                            scrollDirection: Axis.horizontal,\n                            separatorBuilder: (_, inedex) => SizedBox(\n                                  width: 20,\n                                ),\n                            itemCount: topics.length,\n                            itemBuilder: (context, index) {\n                              return Container(\n                                height: 50,\n                                width: 100,\n                                decoration: BoxDecoration(\n                                  color: Colors.red,\n                                  borderRadius: BorderRadius.circular(15),\n                                  image: DecorationImage(\n                                    image: AssetImage('assets/saved_2.png'),\n                                    fit: BoxFit.fill,\n                                  ),\n                                ),\n                                child: Column(\n                                  children: [\n                                    Padding(\n                                      padding: const EdgeInsets.all(8.0),\n                                      child: Align(\n                                        alignment: Alignment.bottomLeft,\n                                        child: Text(\n                                          topics[index].schoolNmae!,\n                                          style: TextStyle(\n                                              color: Colors.white,\n                                              fontWeight: FontWeight.bold),\n                                        ),\n                                      ),\n                                    )\n                                  ],\n                                ),\n                              );\n                            }),\n                      ),\n                      WidgetForum(\n                        details:\n                            '''Notes is designed for whatever\xe2\x80\x99s on your mind.\\nJot down your thoughts. Download Notes.\\nThere is something wonderful in writing. \\nThe Get Is Not Getting Together?''',\n                        name: 'Sam Ajayi',\n                        url: 'assets/dashboard_pic.png',\n                      ),\n                    ],\n                  ),\n                ),\n              ),\n              // bottomSheet:\n              floatingActionButton: FloatingActionButton(\n                onPressed: () {\n                  Scaffold.of(context).showBottomSheet<void>((BuildContext context) {\n                    return Container(\n                      height: 250,\n                      decoration: BoxDecoration(\n                        color: Colors.black,\n                        borderRadius: BorderRadius.only(\n                          topLeft: Radius.circular(10),\n                        ),\n                      ),\n                      child: Padding(\n                        padding: const EdgeInsets.symmetric(\n                          vertical: 50,\n                          horizontal: 15.0,\n                        ),\n                        child: Column(\n                          children: [\n                            Row(\n                              children: [\n                                Icon(\n                                  CustomIcons.answers_forum,\n                                  color: Colors.grey,\n                                ),\n                                SizedBox(\n                                  width: 10,\n                                ),\n                                Text(\n                                  'Post',\n                                  style: TextStyle(\n                                    fontSize: 25,\n                                    fontWeight: FontWeight.bold,\n                                    color: Colors.white,\n                                  ),\n                                )\n                              ],\n                            ),\n                            Padding(\n                              padding: const EdgeInsets.only(right: 120.0),\n                              child: Divider(\n                                thickness: 2,\n                                color: Colors.white,\n                              ),\n                            ),\n                            Row(\n                              children: [\n                                Icon(\n                                  CustomIcons.create_forum,\n                                  color: Colors.grey,\n                                ),\n                                SizedBox(\n                                  width: 10,\n                                ),\n                                Text(\n                                  'Create Forum',\n                                  style: TextStyle(\n                                    fontSize: 25,\n                                    fontWeight: FontWeight.bold,\n                                    color: Colors.white,\n                                  ),\n                                )\n                              ],\n                            ),\n                            Align(\n                              alignment: Alignment.bottomRight,\n                              child: GestureDetector(\n                                onTap: () {\n                                  Navigator.pop(context);\n                                },\n                                child: Container(\n                                  height: 70,\n                                  width: 70,\n                                  decoration: BoxDecoration(\n                                    shape: BoxShape.circle,\n                                    color: Colors.white,\n                                  ),\n                                  child: Center(\n                                    child: Icon(Icons.add),\n                                  ),\n                                ),\n                              ),\n                            )\n                          ],\n                        ),\n                      ),\n                    );\n                  });\n                },\n                child: Icon(Icons.add),\n              ),\n            );\n          }\n        }\n        \n        class WidgetForum extends StatefulWidget {\n          final String? name;\n          final String? details;\n          final String? url;\n        \n          WidgetForum(\n              {Key? key, required this.name, required this.details, required this.url})\n              : super(key: key);\n        \n          @override\n          _WidgetForumState createState() => _WidgetForumState();\n        }\n        \n        class _WidgetForumState extends State<WidgetForum> {\n          @override\n          Widget build(BuildContext context) {\n            return Column(\n              crossAxisAlignment: CrossAxisAlignment.start,\n              children: [\n                Row(\n                  mainAxisAlignment: MainAxisAlignment.spaceBetween,\n                  children: [\n                    Row(\n                      children: [\n                        Container(\n                          height: 50,\n                          width: 50,\n                          decoration: BoxDecoration(\n                            image: DecorationImage(\n                              image: AssetImage(widget.url!),\n                              fit: BoxFit.fill,\n                            ),\n                          ),\n                        ),\n                        SizedBox(\n                          width: 5,\n                        ),\n                        Text(\n                          widget.name!,\n                          style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25),\n                        )\n                      ],\n                    ),\n                    PopupMenuButton(\n                      color: Colors.black,\n                      offset: Offset(0, 40),\n                      itemBuilder: (_) => <PopupMenuItem<String>>[\n                        new PopupMenuItem<String>(\n                          child: Center(\n                            child: Text(\n                              'Fellow',\n                              style: TextStyle(\n                                color: Colors.white,\n                              ),\n                            ),\n                          ),\n                        ),\n                        new PopupMenuItem<String>(\n                          child: Center(\n                            child: Text(\n                              'Block',\n                              style: TextStyle(\n                                color: Colors.white,\n                              ),\n                            ),\n                          ),\n                        ),\n                        new PopupMenuItem<String>(\n                          child: Center(\n                            child: Text(\n                              'Report',\n                              style: TextStyle(\n                                color: Colors.white,\n                              ),\n                            ),\n                          ),\n                        ),\n                      ],\n                      child: Container(\n                        height: 20,\n                        width: 20,\n                        child: SvgPicture.asset('assets/library_pre.svg'),\n                      ),\n                    ),\n                  ],\n                ),\n                SizedBox(height: 18),\n                Text(\n                  widget.details!,\n                  style: TextStyle(fontSize: 15),\n                ),\n                SizedBox(height: 18),\n                Row(\n                  mainAxisAlignment: MainAxisAlignment.spaceBetween,\n                  children: [\n                    Row(\n                      children: [\n                        Icon(\n                          CustomIcons.icons8_up_2_11,\n                          color: Colors.red,\n                        ),\n                        Text('36'),\n                        Icon(\n                          CustomIcons.icons8_down,\n                          color: Colors.grey,\n                        ),\n                        Icon(\n                          CustomIcons.answers_forum,\n                          color: Colors.grey,\n                        ),\n                        Text('3')\n                      ],\n                    ),\n                    Text('Sep 2, 2020')\n                  ],\n                ),\n                Divider(\n                  thickness: 1,\n                )\n              ],\n            );\n          }\n        }\n
Run Code Online (Sandbox Code Playgroud)\n

Moh*_*hri 44

如果它有父级,则不能使用Expandedin ,因为当您使用列时,它会尝试处于屏幕高度,并且当在内部使用扩展时,该列会将剩余空间分配给扩展小部件的子级,现在,如果您使用它将尝试扩展(按方向,在您的情况下垂直)尽可能长,但当您使用 Expanded 时,它会尝试占用剩余空间,因此它会变得无限,从而引发该错误,ColumnSingleChildScrollViewSingleChildScrollView

因此,要么删除SingleChildScrollView并使用 Column 并展开,要么删除Expanded并使用SingleChildScrollView,并确保ShrinkWrapListViewtrue。

Column(
      children: [
        Text(
          'Hello, World!',
          style: Theme.of(context).textTheme.headline4,
        ),
        Container(
          height: 50,
          width: 200,
          color: Colors.amber,
          child: const Text('Random widget'),
        ),
        Expanded(
          child: ListView.separated(
            shrinkWrap: true,
            itemCount: 20,
            separatorBuilder: (_, __) => const Divider(),
            itemBuilder: (context, int index) {
              return ListTile(
                title: Text('Item at $index'),
              );
            },
          ),
        )
      ],
    );
Run Code Online (Sandbox Code Playgroud)

或者滚动所有小部件,您可以使用SingleChildScrollView

删除Expanded此处的小部件。

SingleChildScrollView(
      child: Column(
        children: [
          Text(
            'Hello, World!',
            style: Theme.of(context).textTheme.headline4,
          ),
          Container(
            height: 50,
            width: 200,
            color: Colors.amber,
            child: const Text('Random widget'),
          ),
          ListView.separated(
              shrinkWrap: true,
              itemCount: 20,
              separatorBuilder: (_, __) => const Divider(),
              itemBuilder: (context, int index) {
                return ListTile(
                  title: Text('Item at $index'),
                );
              },
            ),
        ],
      ),
    );
Run Code Online (Sandbox Code Playgroud)


Dav*_*zza 12

将其包裹在 a 中Container并为其添加高度。为我修好了。

  • 或者更好的是,一个有高度的 SizedBox。 (2认同)