IndexedStack을 확장하여 자식 요소의 지연 로딩을 가능하게 하는 플러터 패키지입니다. 현재 보이는 위젯만 빌드하고 렌더링되도록 보장하여 여러 페이지나 무거운 위젯이 있는 앱의 성능을 향상시킵니다. 각 탭에 복잡한 콘텐츠가 포함된 탭 기반 인터페이스 또는 네비게이션에 적합합니다. 필요한 시점까지 위젯 생성을 미루어 메모리 사용을 효율적으로 관리하고 초기 로드 시간 및 리소스 사용량을 줄입니다.
사용 사례
많은 콘텐츠를 포함한 탭 인터페이스
다중 페이지 레이아웃에서의 성능 최적화
초기 앱 로드 시간 줄이기
동적 콘텐츠 전환
메모리 효율적인 탐색
복잡한 위젯의 지연 렌더링
주요 기능
IndexedStack 확장
자식 요소의 지연 로딩
요청에 따라 위젯 생성
성능 개선
메모리 효율성
간단한 통합
적합한 대상
앱 성능 최적화하는 개발자
여러 탭 또는 화면을 가진 앱
효율적인 렌더링을 필요로 하는 복잡한 UI
IndexedStack를 사용하는 프로젝트
자원 관리에 집중하는 팀
고려 사항
IndexedStack 내의 자식 위젯으로 제한
신중한 상태 관리가 필요할 수 있습니다
즉시 렌더링 요구 사항에 적합하지 않습니다
예측 가능한 탐색 패턴과 함께 사용할 때 가장 효과적입니다
위젯 라이프사이클에 대한 이해가 필요합니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
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
classMainPageextendsStatefulWidget{
@overrideState<StatefulWidget> createState() => _MainPageState();
}
class_MainPageStateextendsState<MainPage> {
int _index = 0;
@overrideWidgetbuild(finalBuildContext context) {
returnMaterialApp(
home: Scaffold(
body: LazyLoadIndexedStack(
index: _index,
preloadIndexes: [1, 2],
autoDisposeIndexes: [2, 3],
children: [
Page1(), // Load by initial indexPage2(), // Preloaded initiallyPage3(), // Preloaded initially but disposed when other index is selectedPage4(), // Not preloaded and disposed when other index is selected
],
),
bottomNavigationBar: BottomNavigationBar(
onTap: (index) {
setState(() => _index = index);
},
currentIndex: _index,
items: [
BottomNavigationBarItem1(),
BottomNavigationBarItem2(),
BottomNavigationBarItem3(),
BottomNavigationBarItem4(),
],
),
),
);
}
}