颤振ListView在一些滚动后保持激活

颤振ListView在一些滚动后保持激活,listview,flutter,flutter-layout,flutter-sliver,Listview,Flutter,Flutter Layout,Flutter Sliver,我想keepAlive保存已在ListView中呈现的小部件。我尝试了addAutomaticePalies:true属性,这些属性由ListView类提供 这是我使用的示例代码。SliverList提供的SliverChildBuilderDelegate委托中存在相同问题 ListView.builder( itemBuilder: (context,index){ return Card( child: Container( chil

我想
keepAlive
保存已在
ListView
中呈现的小部件。我尝试了
addAutomaticePalies:true
属性,这些属性由
ListView
类提供

这是我使用的示例代码。
SliverList
提供的
SliverChildBuilderDelegate
委托中存在相同问题

ListView.builder(
    itemBuilder: (context,index){
      return Card(
        child: Container(
          child: Image.asset("images/${index+1}.jpg",fit: BoxFit.cover,),
          height: 250.0,
        ),
      );
    },
    addAutomaticKeepAlives: true,
    itemCount:40 ,
);

要使
automatickepalive
正常工作,需要保持活动状态的每个项目都必须发送特定的通知

触发此类通知的典型方法是使用

class Foo扩展StatefulWidget{
@凌驾
FooState createState(){
返回新的FooState();
}
}
类FooState使用AutomaticEpaLiveClientMixin扩展状态{
@凌驾
小部件构建(构建上下文){
返回容器(
);
}
@凌驾
bool get wantKeepAlive=>true;
}

您也可以尝试查看listview builder上的cacheExtent属性。将其设置为覆盖项目的值将使所有项目保持活动状态。
多亏了上面的雷米。我不知道在列表中使用它时需要保留的项目-这在以前的颤振文档中没有…

正如AutomatickePaLiveClientMixin和Remi的回答所述

子类必须实现wantKeepAlive,并且它们的构建方法必须 调用super.build(返回值将始终返回null,并且应该 被忽略)

因此,请将生成方法更改为:

class Foo extends StatefulWidget {
  @override
  FooState createState() {
    return new FooState();
  }
}

class FooState extends State<Foo> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(

    );
  }

  @override
  bool get wantKeepAlive => true;
}
class Foo扩展StatefulWidget{
@凌驾
FooState createState(){
返回新的FooState();
}
}
类FooState使用AutomaticEpaLiveClientMixin扩展状态{
@凌驾
小部件构建(构建上下文){
super.build(上下文);
返回容器(
);
}
@凌驾
bool get wantKeepAlive=>true;
}

如果您想保留一个片段列表(对于CustomScrollView),您需要做的就是使用“SliverChildListDelegate”而不是“SliverChildBuilderDelegate”

这是我的密码:

final List<Product> products;
return CustomScrollView(
  controller: _scrollController,
    slivers: [
      _mySliverAppBar(context, title),
      SliverList(
        delegate: SliverChildListDelegate(
          List.generate(products.length, (index) => _myProductCard(context,products[index]))
        )
        // SliverChildBuilderDelegate(
        //   (context, index) => _myProductCard(context, products[index]),
        //   childCount: products.length,
        // ),
      ),
   ],
);
最终产品清单;
返回自定义滚动视图(
控制器:\ u滚动控制器,
条子:[
_mySliverAppBar(上下文、标题),
银表(
委托:SliverChildListDelegate(
List.generate(products.length,(index)=>\u myProductCard(上下文,产品[索引])
)
//SliverChildBuilderDelegate(
//(上下文,索引)=>\u myProductCard(上下文,产品[索引]),
//childCount:products.length,
// ),
),
],
);

正如您在代码中所看到的,我以前使用SliverChildBuilderDelegate

这对图像有效吗?我试过了,但当我滚动时,图像仍然会重新渲染。如果滚动缓慢,则此时没有问题。但是在快速滚动图像小部件re-render.Hi中,您能提供帮助吗?我在CustomScrollView和SliverList中遇到了类似的问题。。。我有一个WebView,当页面滚动时,它会不断重新加载。。。我的CustomScrollView上有足够高的缓存范围。我还具备了所有的先决条件,例如:
super.build(context)
bool get wantKeepAlive
覆盖。。。我还设置了
automatickepaliveclientmixin
。。。不确定发生了什么,但WebView肯定不会像SingleChildScrollView那样重新加载。。所以我猜是CustomScrollView/SliverList破坏了它..
super.build(上下文)真的解决了我的问题。谢谢
final List<Product> products;
return CustomScrollView(
  controller: _scrollController,
    slivers: [
      _mySliverAppBar(context, title),
      SliverList(
        delegate: SliverChildListDelegate(
          List.generate(products.length, (index) => _myProductCard(context,products[index]))
        )
        // SliverChildBuilderDelegate(
        //   (context, index) => _myProductCard(context, products[index]),
        //   childCount: products.length,
        // ),
      ),
   ],
);