首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >颤振: SliverAppBar in LayoutBuilder

颤振: SliverAppBar in LayoutBuilder
EN

Stack Overflow用户
提问于 2019-10-16 01:08:24
回答 2查看 1.6K关注 0票数 0

我想用SliverAppBar使用Layout Builder来测量高度,然后在得到SliverAppBar高度之后,使用ScrollController创建这个高度的条件。

我有这样的代码:

代码语言:javascript
复制
CustomScrollView(
          controller: _scrollController,
          slivers: <Widget>[
            LayoutBuilder(
              builder: (context, constraint) {
                message = constraint.biggest.height.toString();
                return SliverAppBar(
                  floating: false,
                  pinned: true,
                  expandedHeight: ScreenUtil.getInstance().setHeight(250),
                  leading: Container(
                    color: Colors.blue.withOpacity(.5),
                    child: IconButton(
                      icon: Icon(Icons.arrow_back),
                      onPressed: () => Navigator.pop(context),
                    ),
                  ),
                  flexibleSpace: FlexibleSpaceBar(
                    // titlePadding: EdgeInsets.zero,
                    title: Container(
                      width: ScreenUtil.getInstance().setWidth(250),
                      child: Text(
                        message ?? "Null",
                        style: ResponsiveUI.textNamaWartawanStylWhite,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    background: Container(
                      child: CachedNetworkImage(
                        fit: BoxFit.cover,
                        imageUrl:
                            "${Urls.BASE_API_IMAGE}/wartawan/${widget.gambarWartawan}",
                      ),
                    ),
                  ),
                  actions: <Widget>[
                    Container(
                      color: Colors.blue.withOpacity(.5),
                      child: IconButton(
                        icon: Icon(Icons.more_horiz),
                        onPressed: () => "",
                      ),
                    ),
                  ],
                );
              },
            ),
          ],
        ),

但我知道这个错误:

代码语言:javascript
复制
The following assertion was thrown building Listener:
A RenderViewport expected a child of type RenderSliver but received a child of type _RenderLayoutBuilder.

RenderObjects expect specific types of children because they coordinate with their children during layout and paint. For example, a RenderSliver cannot be the child of a RenderBox because a RenderSliver does not understand the RenderBox layout protocol.
The RenderViewport that expected a RenderSliver child was created by: Viewport ← IgnorePointer-[GlobalKey#4f27a] ← Semantics ← _PointerListener ← Listener ← _GestureSemantics ← RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#ed3fa] ← _PointerListener ← Listener ← _ScrollableScope ← _ScrollSemantics-[GlobalKey#c7130] ← RepaintBoundary ← ⋯
The _RenderLayoutBuilder that did not match the expected child type was created by: LayoutBuilder ← Viewport ← IgnorePointer-[GlobalKey#4f27a] ← Semantics ← _PointerListener ← Listener ← _GestureSemantics ← RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#ed3fa] ← _PointerListener ← Listener ← _ScrollableScope ← _ScrollSemantics-[GlobalKey#c7130] ← ⋯
User-created ancestor of the error-causing widget was
    CustomScrollView 
When the exception was thrown, this was the stack
#0      ContainerRenderObjectMixin.debugValidateChild.<anonymous closure> 
#1      ContainerRenderObjectMixin.debugValidateChild

你能帮我办案吗?谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-16 01:12:38

你不能在CustomScrollView里放一个非条状的。您可能需要将布局构建器放置在FlexibleSpace的背景中。

票数 1
EN

Stack Overflow用户

发布于 2021-03-29 05:53:06

试试SliverLayoutBuilder小部件。

解释:

您不能将非条小部件(如LayoutBuilder )放置在希望条作为子部件(如CustomScrollView )的小部件中。

例如,当您想要将填充添加到一个小块时,您应该使用SliverPadding小部件,而不是普通的Padding小部件。类似地,也有一个LayoutBuilder的小版本,叫做SliverLayoutBuilder

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58404408

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档