如何使用 flutter 的 NestedScrollView?

How to use flutter NestedScrollView?

我实现了一些 NestedScrollView 如下。 它会导致错误 "must be called with a context that contains a NestedScrollView"。 但是我使用 Builder 来构建小部件,这在 flutter docs 的手册中。我应该怎么办?

return NestedScrollView(
      headerSliverBuilder: (context, isInScroll) {
        /* something*/
      },
      body: Hero(
        tag: widget.folderInfo.title + 'body',
        child: Container(
          padding: EdgeInsets.fromLTRB(16.0, 20.0, 16.0, 16.0),
          child: Builder(
            builder: (context) {
              print("built body builder");
               // below line causes error "must be called with a context that contains a NestedScrollView"
              var handle =
                  NestedScrollView.sliverOverlapAbsorberHandleFor(context);
              );
            },
          ),
        ),
      ),
    );

Builder 函数需要一个 return 语句,您提供的代码中似乎缺少该语句。

下面的代码可以工作并在屏幕上打印 test

return NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
              SliverAppBar()
            ];
          },
          body: Hero(
            tag: 'test',
            child: Container(
              padding: EdgeInsets.fromLTRB(16.0, 20.0, 16.0, 16.0),
              child: Builder(builder: (context) {
                var handle = NestedScrollView.sliverOverlapAbsorberHandleFor(context);
                print('test');
                return Container( . // whatever you want to return here
                 child: Text('test'),
                );
                print('test');

              })
            ),
          ),
    );