flutter 防止NestedScrollView滚动

aydmsdu9  于 2023-03-31  发布在  Flutter
关注(0)|答案(2)|浏览(245)

我有一个小部件,是一个NestedScrollView和一些文本在身体。有情况下,我想“锁定”用户,不允许他滚动。我已经尝试使用物理属性,并将其设置为NeverScrollableScrollPhysics(),因为文档似乎表明这是它的用途:https://api.flutter.dev/flutter/widgets/NeverScrollableScrollPhysics-class.html然而,这似乎不起作用,屏幕仍然滚动。
有办法阻止卷轴吗?

@override
  Widget build(BuildContext context) {
    return DefaultTabController(
        length: 2,
        child: NestedScrollView(
          physics: NeverScrollableScrollPhysics(),
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
              SliverAppBar(
                expandedHeight: 150.0,
                floating: false,
                pinned: true,
                forceElevated: true,
                flexibleSpace: FlexibleSpaceBar(
                  centerTitle: true,
                  title: Text('I am a header'),
                ),
              )
            ];
          },
          body: Text(' I am scrolling'),
        ));
  }
txu3uszq

txu3uszq1#

有一个名为AbsorbPointer的小部件。将其缠绕在NestedScrollView上。

AbsorbPointer( 
   isAbsorbing: true,
   child: NestedScrollView(...))
nbewdwxp

nbewdwxp2#

将此Widget用作SliverAppBar的父级

SliverOverlapAbsorber(  
         handle:NestedScrollView.sliverOverlapAbsorberHandleFor(context),
         sliver: Child(),
         )

样本代码

NestedScrollView(
      headerSliverBuilder: (BuildContext context, bool  
         innerBoxIsScrolled) { 
        return <Widget>[
          SliverOverlapAbsorber( 
         handle:NestedScrollView.sliverOverlapAbsorberHandleFor(context),
            sliver: SliverAppBar(
              expandedHeight: 150.0,
              floating: false,
              pinned: true,
              forceElevated: true,
              flexibleSpace: FlexibleSpaceBar(
              centerTitle: true,
              title: Text('I am a header'),
            ),
          ),
        ];
      },
      body: const TabBarView(
        children: [],
      ),
    ),

相关问题