@@ -448,12 +448,20 @@ class CollectionView<T> extends StatefulWidget {
448448 paginatedData = data,
449449 this .data = null ;
450450
451+ /// Returns a [CollectionViewStateActions] instance for the given [stateName] .
452+ static CollectionViewStateActions stateActions (String stateName) =>
453+ CollectionViewStateActions (stateName);
454+
451455 /// Resets the state.
456+ @Deprecated ('Use CollectionView.stateActions(stateName).reset() instead' )
452457 static void stateReset (String stateName) {
453458 updateState (stateName, data: {"action" : "reset" , "data" : {}});
454459 }
455460
456461 /// Removes an item from the list at the given index.
462+ @Deprecated (
463+ 'Use CollectionView.stateActions(stateName).removeFromIndex(index) instead' ,
464+ )
457465 static void removeFromIndex (String stateName, int index) {
458466 updateState (
459467 stateName,
@@ -465,17 +473,60 @@ class CollectionView<T> extends StatefulWidget {
465473 }
466474}
467475
476+ /// State actions for [CollectionView] .
477+ ///
478+ /// Example usage:
479+ /// ```dart
480+ /// CollectionView.stateActions("my_list").reset();
481+ /// CollectionView.stateActions("my_list").addItem(newItem);
482+ /// CollectionView.stateActions("my_list").removeFromIndex(2);
483+ /// ```
484+ class CollectionViewStateActions extends StateActions {
485+ CollectionViewStateActions (super .state);
486+
487+ /// Resets the CollectionView, clearing data and re-fetching.
488+ void reset () {
489+ action ("reset" );
490+ }
491+
492+ /// Removes the item at [index] from the list.
493+ void removeFromIndex (int index) {
494+ action ("removeFromIndex" , data: {"index" : index});
495+ }
496+
497+ /// Re-fetches data from the data callback.
498+ void refreshData () {
499+ action ("refreshData" );
500+ }
501+
502+ /// Appends [item] to the end of the list.
503+ void addItem (dynamic item) {
504+ action ("addItem" , data: {"item" : item});
505+ }
506+
507+ /// Inserts [item] at [index] in the list.
508+ void insertItem (int index, dynamic item) {
509+ action ("insertItem" , data: {"index" : index, "item" : item});
510+ }
511+
512+ /// Replaces the item at [index] with [item] .
513+ void updateItemAtIndex (int index, dynamic item) {
514+ action ("updateItemAtIndex" , data: {"index" : index, "item" : item});
515+ }
516+ }
517+
468518class _CollectionViewState <T > extends NyState <CollectionView <T >> {
469519 List <T > _data = [];
520+ bool _syncDataInitialized = false ;
470521 int _iteration = 1 ;
471522 final RefreshController _refreshController = RefreshController (
472523 initialRefresh: false ,
473524 );
474525
475526 @override
476527 void initState () {
477- super .initState ();
478528 stateName = widget.stateName;
529+ super .initState ();
479530 }
480531
481532 @override
@@ -487,6 +538,7 @@ class _CollectionViewState<T> extends NyState<CollectionView<T>> {
487538 @override
488539 get init => () {
489540 _iteration = 1 ;
541+ _syncDataInitialized = false ;
490542
491543 if (widget.isPullable) {
492544 // Pullable mode - use paginatedData
@@ -562,6 +614,34 @@ class _CollectionViewState<T> extends NyState<CollectionView<T>> {
562614 _data.removeAt (index);
563615 setState (() {});
564616 },
617+ 'refreshData' : (_) async {
618+ _data = [];
619+ await reboot ();
620+ },
621+ 'addItem' : (data) {
622+ if (data is ! Map || ! data.containsKey ('item' )) return ;
623+ _data.add (data['item' ]);
624+ setState (() {});
625+ },
626+ 'insertItem' : (data) {
627+ if (data is ! Map ||
628+ ! data.containsKey ('index' ) ||
629+ ! data.containsKey ('item' ))
630+ return ;
631+ int index = (data['index' ] as int ).clamp (0 , _data.length);
632+ _data.insert (index, data['item' ]);
633+ setState (() {});
634+ },
635+ 'updateItemAtIndex' : (data) {
636+ if (data is ! Map ||
637+ ! data.containsKey ('index' ) ||
638+ ! data.containsKey ('item' ))
639+ return ;
640+ int index = data['index' ];
641+ if (index < 0 || index >= _data.length) return ;
642+ _data[index] = data['item' ];
643+ setState (() {});
644+ },
565645 };
566646
567647 /// Refresh the list (for pullable mode)
@@ -720,7 +800,10 @@ class _CollectionViewState<T> extends NyState<CollectionView<T>> {
720800 /// Build regular (non-pullable) view
721801 Widget _buildRegularView (Widget loadingWidget) {
722802 if (widget.data is ! Future Function ()) {
723- _data = widget.data !() ?? [];
803+ if (! _syncDataInitialized) {
804+ _data = widget.data !() ?? [];
805+ _syncDataInitialized = true ;
806+ }
724807 if (_data.isEmpty) {
725808 return _buildEmptyWidget ();
726809 }
0 commit comments