Skip to content

Commit d2fd77d

Browse files
authored
Merge pull request #7 from colonal/force_reload_indexes
Add `autoDisposeIndexes` to dispose unused IndexedStack children and rebuild them when needed
2 parents 82f1af7 + a3decc4 commit d2fd77d

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

README.md

+11-8
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@
33

44
# lazy_load_indexed_stack
55

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

88
## Motivation
99

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

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

14-
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.
14+
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.
1515

1616
## Features
1717
* **Lazy Loading**: The main feature of `LazyLoadIndexedStack` is to build children widgets only when they are needed, reducing initial load time.
1818
* **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.
19+
* **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.
1920

2021
## Usage
21-
You can use `LazyLoadIndexedStack` in the same way as `IndexedStack`.
22+
You can use `LazyLoadIndexedStack` in the same way as `IndexedStack`, with additional options for preloading and auto dispose.
2223

24+
### Basic Example
2325
```dart
2426
class MainPage extends StatefulWidget {
2527
@override
@@ -36,11 +38,12 @@ class _MainPageState extends State<MainPage> {
3638
body: LazyLoadIndexedStack(
3739
index: _index,
3840
preloadIndexes: const [3],
41+
autoDisposeIndexes: const [1, 2],
3942
children: [
4043
Page1(),
41-
Page2(),
42-
Page3(),
43-
Page4(), // index3 is preloaded
44+
Page2(), // index 1 will be auto dispose
45+
Page3(), // index 2 will also auto dispose
46+
Page4(), // index 3 is preloaded
4447
],
4548
),
4649
bottomNavigationBar: BottomNavigationBar(

lib/lazy_load_indexed_stack.dart

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
library lazy_load_indexed_stack;
2-
31
import 'package:flutter/widgets.dart';
42

53
/// An extended IndexedStack that builds the required widget only when it is needed, and returns the pre-built widget when it is needed again.
@@ -10,6 +8,9 @@ class LazyLoadIndexedStack extends StatefulWidget {
108
/// The indexes of children that should be preloaded.
119
final List<int> preloadIndexes;
1210

11+
/// The indexes of children that should be automatically disposed and rebuilt when accessed again.
12+
final List<int> autoDisposeIndexes;
13+
1314
/// Same as alignment attribute of original IndexedStack.
1415
final AlignmentGeometry alignment;
1516

@@ -33,6 +34,7 @@ class LazyLoadIndexedStack extends StatefulWidget {
3334
super.key,
3435
Widget? unloadWidget,
3536
this.preloadIndexes = const [],
37+
this.autoDisposeIndexes = const [],
3638
this.alignment = AlignmentDirectional.topStart,
3739
this.sizing = StackFit.loose,
3840
this.textDirection,
@@ -65,6 +67,8 @@ class LazyLoadIndexedStackState extends State<LazyLoadIndexedStack> {
6567
_children = _initialChildren();
6668
}
6769

70+
_children = _updateChildrenForAutoDispose();
71+
6872
_children[widget.index] = widget.children[widget.index];
6973
}
7074

@@ -92,4 +96,17 @@ class LazyLoadIndexedStackState extends State<LazyLoadIndexedStack> {
9296
}
9397
}).toList();
9498
}
99+
100+
List<Widget> _updateChildrenForAutoDispose() {
101+
return widget.children.asMap().entries.map((entry) {
102+
final index = entry.key;
103+
final childWidget = entry.value;
104+
105+
if (index != widget.index && widget.autoDisposeIndexes.contains(index)) {
106+
return widget.unloadWidget;
107+
} else {
108+
return childWidget;
109+
}
110+
}).toList();
111+
}
95112
}

0 commit comments

Comments
 (0)