flutter 错误:RenderBox未布局:需求-油漆需求-合成-钻头-更新

laximzn5  于 2022-11-25  发布在  Flutter
关注(0)|答案(5)|浏览(120)

我有问题使用SingleChildScrollView在这种方式下,我不知道什么是错的,我不断得到这个错误。
如果添加SingleChildScrollView,页面将为空白(不会显示所有小部件),但如果删除SingleChildScrollView,页面将显示。
未配置RenderBox:渲染填充#583c0重新布局边界= up 1需要-油漆需要-合成-位-更新'软件包:flutter/src/rendering/box. dart':失败的Assert:行1929位置12:'具有大小
相关的导致错误的小部件是支架支架:file:///Users/mac/Documents/Uneleap-Platform-master/lib/screens/Pages/forum/forum.dart:25:12
下面是代码

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

xzv2uavs1#

如果Column具有父SingleChildScrollView,则不能在Column中使用Expanded,因为当您使用列时,它会尝试位于屏幕高度内,而当在内部使用展开时,列会将剩余空间分配给展开小部件的子小部件,现在如果您使用SingleChildScrollView,它会尝试展开(按方向,在您的情况下为垂直方向)尽可能长的时间,但由于您使用的是Expanded,它试图占用剩余空间,因此它会无限扩展,从而引发错误,
因此要么删除SingleChildScrollView并使用Column和Expanded,要么删除Expanded并使用SingleChildScrollView,同时确保ListView中的ShrinkWrap为true。

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'),
              );
            },
          ),
        )
      ],
    );

或者滚动所有小部件您可以使用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'),
                );
              },
            ),
        ],
      ),
    );
rekjcdws

rekjcdws2#

把它 Package 在一个容器里,并增加一个高度。为我修正了它。

jljoyd4f

jljoyd4f3#

我通过将Column小部件包含在Container小部件中来解决这个问题,该小部件包含一组widthheight

ffscu2ro

ffscu2ro4#

如果列具有父SingleChildScrollView,则不能使用“在列中展开”,因为当您使用列时,它会尝试处于屏幕高度,而当在内部使用展开时,该列会将剩余空间分配给展开小部件的子小部件。现在,如果您使用SingleChildScrollView,它会尝试展开(按方向,在您的情况下为垂直方向)尽可能长的时间,但由于您使用的是Expanded,它试图占用剩余空间,因此它会无限扩展,从而引发错误,
因此要么删除SingleChildScrollView并使用Column和Expanded,要么删除Expanded并使用SingleChildScrollView也要确保在ListView中ShrinkWrap为true。

tquggr8v

tquggr8v5#

我发现了同样的问题。但在我的例子中,我试图为listTile类的尾随属性提供一行小部件。我通过用容器 Package 该行并设置其宽度来解决它。

相关问题