1- library lazy_load_indexed_stack;
2-
31import '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 forcibly reloaded.
12+ final List <int > forceReloadIndexes;
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 .forceReloadIndexes = 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 = _updateChildrenForReload ();
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 > _updateChildrenForReload () {
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.forceReloadIndexes.contains (index)) {
106+ return widget.unloadWidget;
107+ } else {
108+ return childWidget;
109+ }
110+ }).toList ();
111+ }
95112}
0 commit comments