博客
关于我
Flutter——ListView源码分析之child-view的构建
阅读量:356 次
发布时间:2019-03-04

本文共 1206 字,大约阅读时间需要 4 分钟。

ListView.builder 构造流程分析

在Flutter中,ListView.builder 是一个强大的工具,用于动态生成列表项。以下是其构造流程的详细分析:

1. ListView.builder 结构

ListView.builder 是一个构造函数,主要参数包括:

  • itemCount:指定列表的项数。
  • itemBuilder:一个IndexedWidgetBuilder,用于生成每个列表项。

它内部使用 SliverList 来管理这些项,并通过 SliverChildBuilderDelegate 来处理 itemBuilder。

2. SliverList 与 SliverChildBuilderDelegate

SliverList 是一个 Sliver widget,用于管理多个孩子 widget。它通过 SliverChildBuilderDelegate 来处理 itemBuilder 的调用。

  • SliverChildBuilderDelegate 接收 itemBuilder 和 itemCount,用于生成和管理列表项。
  • 每次调用 itemBuilder.build 时,会生成一个新的 widget。

3. 构建过程概述

  • 调用 buildChildLayout: ListView.builder 调用 buildChildLayout 方法,返回一个 SliverList。
  • SliverList 构造: SliverList 接收 childrenDelegate(由 SliverChildBuilderDelegate 实现),并生成多个项。
  • Scrollable widget 包裹: SliverList 被 Scrollable widget 包裹,支持滚动交互。
  • Viewport widget: Scrollable widget 内部使用 Viewport widget 管理视窗,构造最终的滚动列表。
  • 4. performRebuild 方法

    • performRebuild 方法由 SliverMultiBoxAdaptorElement 调用,用于触发 widget 的重建。
    • 它会调用 widget 的 build 方法,生成新的 widget。

    5. 列表项生成

    • 每次生成项时,itemBuilder 的 build 方法会被调用,传入当前 BuildContext 和索引。
    • 生成的 widget 会被添加到列表中,由 SliverList 管理。

    6. 总结

    ListView.builder 通过 SliverList 和 SliverChildBuilderDelegate 高效地管理列表项构建。其背后的机制涉及多个 Flutter widget,确保列表能够流畅滚动和高效绘制。理解这些内部机制有助于优化列表性能和用户体验。

    转载地址:http://tqnr.baihongyu.com/

    你可能感兴趣的文章
    NMF(非负矩阵分解)
    查看>>
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>