搜索插件、代码仓与 APP 分类

插件详情v1.2.1

lazy_load_indexed_stack

一个扩展 IndexedStack 以支持延迟加载的包。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.3万
喜欢数
101
Pub 分数
160

安装插件

flutter pub add lazy_load_indexed_stack

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个扩展 IndexedStack 的 Flutter 包,可实现其子项的延迟加载。它确保仅构建和渲染当前可见的 widget,从而提升具有多个页面或复杂 widget 的应用性能。适用于包含复杂内容的标签页界面或导航场景。该包通过推迟 widget 创建直到需要时才进行,高效管理内存,减少初始加载时间和资源消耗。

适合场景

  • 内容丰富的标签式界面
  • 多页面布局中的性能优化
  • 减少应用初始加载时间
  • 动态内容切换
  • 内存高效的导航
  • 复杂控件的延迟渲染

主要特点

  • 扩展 IndexedStack
  • 子项的惰性加载
  • 按需创建小部件
  • 性能提升
  • 内存效率
  • 简单集成

适合对象

  • 开发者优化应用性能
  • 具有多个标签页或屏幕的应用程序
  • 需要高效渲染的复杂UI
  • 使用 IndexedStack 的项目
  • 专注于资源管理的团队

使用建议

  • 限制为 IndexedStack 内的子部件
  • 可能需要仔细的状态管理
  • 不适用于即时渲染需求
  • 最好与可预测的导航模式一起使用
  • 需要理解部件生命周期

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

CI Pub

lazy_load_indexed_stack

A package that extends IndexedStack to allow for lazy loading and provides enhanced control for reloading specific child widgets.

Motivation

If you use the IndexedStack with bottom navigation, all the widgets specified in the children of the IndexedStack will be built.

Moreover, if the widget requires API requests or database access, or has a complex UI, the IndexedStack build time will be significant.

Therefore, we created an extended IndexedStack that builds the required widget only when it is needed and returns the pre-built widget when it is needed again.

Features

  • Lazy Loading: The main feature of LazyLoadIndexedStack is to build children widgets only when they are needed, reducing initial load time.
  • Preloading: With the preloadIndexes parameter, you can specify indexes of children that should be built in advance, even if they are not currently visible. This is useful for preloading widgets that are likely to be needed soon.
  • Auto Disposal: The autoDisposeIndexes parameter allows specific children to be automatically disposed of when they are no longer visible. When these children are accessed again, they will be rebuilt from scratch. This is useful for cases where widgets hold significant state or require resetting when revisited.

Usage

You can use LazyLoadIndexedStack in the same way as IndexedStack, with additional options for preloading and auto dispose. If an index is included in both preloadIndexes and autoDisposeIndexes, it will be preloaded initially but disposed when it becomes invisible and rebuilt when accessed again.

Basic Example
class MainPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  int _index = 0;

  @override
  Widget build(final BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: LazyLoadIndexedStack(
          index: _index,
          preloadIndexes: [1, 2],
          autoDisposeIndexes: [2, 3],
          children: [
            Page1(), // Load by initial index
            Page2(), // Preloaded initially
            Page3(), // Preloaded initially but disposed when other index is selected
            Page4(), // Not preloaded and disposed when other index is selected
          ],
        ),
        bottomNavigationBar: BottomNavigationBar(
          onTap: (index) {
            setState(() => _index = index);
          },
          currentIndex: _index,
          items: [
            BottomNavigationBarItem1(),
            BottomNavigationBarItem2(),
            BottomNavigationBarItem3(),
            BottomNavigationBarItem4(),
          ],
        ),
      ),
    );
  }
}

See more details in Example or API reference!

所属分类

源码仓库

使用此插件的项目