首页 > 解决方案 > 将容器添加到 Sliver

问题描述

我刚开始学习 slivers,它们似乎不适用于任何其他 Flutter 小部件,除了以“Sliver”开头的小部件。我尝试添加一个Container,这样我就可以添加BorderRadius来装饰列表。但我得到了这个错误:

RenderSliv​​erFillRemaining 需要 RenderBox 类型的子代,但收到了 RenderSliv​​erToBoxAdapter 类型的子代。

到目前为止,这是我的代码:

...
SliverFillRemaining(
  child: SliverToBoxAdapter(
    child: Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(36),
          topRight: Radius.circular(36),
        ),
      ),
      child: SliverPadding(
        padding: const EdgeInsets.all(16),
        sliver: SliverFixedExtentList(
          itemExtent: 50.0,
          delegate: SliverChildBuilderDelegate(
            (BuildContext context, int index) {
              return Container(
                alignment: Alignment.center,
                color: Colors.lightBlue[100 * (index % 9)],
                child: Text('List Item $index'),
              );
            },
          ),
        ),
      ),
    ),
  ),
),
...

标签: flutterflutter-sliver

解决方案


我通过使用 aNestedScrollView而不是 a解决了我的问题CustomScrollView

return Scaffold(
  body: NestedScrollView(
    headerSliverBuilder: (context, innerBoxIsScrolled) {
      return <Widget>[
        SliverAppBar(
          floating: false,
          pinned: true,
          snap: false,
          elevation: 0,
          expandedHeight: 150.0,
          brightness: DynamicTheme.of(context).brightness,
          flexibleSpace: FlexibleSpaceBar(
            title: Text(assignment.name),
          ),
        ),
      ];
    },
    body: Container(
      decoration: BoxDecoration(
        color: Colors.red,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(36),
          topRight: Radius.circular(36),
        ),
      ),
      child: ListView.builder(
        itemExtent: 50.0,
        itemBuilder: (context, index) {
          return Container(
            alignment: Alignment.center,
            color: Colors.lightBlue[100 * (index % 9)],
            child: Text('List Item $index'),
          );
        },
      ),
    ),
  ),
);

推荐阅读