CustomScrollView实践 2023-08-16 周三

2023-08-16  本文已影响0人  勇往直前888

简介

今天有反馈说有两个页面比较卡,需要做一下性能优化。
在首页和商品详情页已经做过实践,这直接依样画葫芦,直接用CustomScrollView进行改造

列表

性能问题基本上离不开列表,SliverList一般要用到。记得以前这个组件看起来还是比较难用的,需要一个delegate之类的,看上去不伦不类。不过这次看了一下SliverList已经和ListView差不多了,非常方便。

  SliverList.list({
    super.key,
    required List<Widget> children,
    bool addAutomaticKeepAlives = true,
    bool addRepaintBoundaries = true,
    bool addSemanticIndexes = true,
  })
  SliverList.builder({
    super.key,
    required NullableIndexedWidgetBuilder itemBuilder,
    ChildIndexGetter? findChildIndexCallback,
    int? itemCount,
    bool addAutomaticKeepAlives = true,
    bool addRepaintBoundaries = true,
    bool addSemanticIndexes = true,
  })
  SliverList.separated({
    super.key,
    required NullableIndexedWidgetBuilder itemBuilder,
    ChildIndexGetter? findChildIndexCallback,
    required NullableIndexedWidgetBuilder separatorBuilder,
    int? itemCount,
    bool addAutomaticKeepAlives = true,
    bool addRepaintBoundaries = true,
    bool addSemanticIndexes = true,
  })

SliverGrid

这个也降低使用难度了,和普通的GridView差不多了。大多数情况下直接使用下面这个

  SliverGrid.count({
    super.key,
    required int crossAxisCount,
    double mainAxisSpacing = 0.0,
    double crossAxisSpacing = 0.0,
    double childAspectRatio = 1.0,
    List<Widget> children = const <Widget>[],
  })

其他组件

嵌套滑动

参考文章

干货 | Flutter控件CustomScrollView原理解析及应用实践

flutter性能分析

让Flutter 应用程序性能提高 10 倍的 10 个技巧

上一篇 下一篇

猜你喜欢

热点阅读