Last active
May 3, 2025 14:23
-
-
Save BorisKest/e28f7d9efef9371092a52ba4805d2c17 to your computer and use it in GitHub Desktop.
Content list controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class ContentListController { | |
ContentListController({ | |
this.onPressed, | |
this.onLoadMore, | |
this.onRefresh, | |
this.onSearch, | |
this.onFilter, | |
this.onSort, | |
}); | |
final List<Widget> _items = const []; | |
final VoidCallback? onLoadMore; | |
final Function<T>(T item)? onPressed; | |
final Function()? onRefresh; | |
final Function<T>(T query)? onSearch; | |
final Function<T>(T filters)? onFilter; | |
final Function<T>(T sort)? onSort; | |
final ValueNotifier<List<Widget>> itemsNotifier = ValueNotifier<List<Widget>>( | |
[], | |
); | |
List<Widget> get items => _items; | |
void updateItems(List<Widget> items) { | |
itemsNotifier.value = items; | |
} | |
void addItems(List<Widget> items) { | |
final updated = Set<Widget>.from(itemsNotifier.value)..addAll(items); | |
itemsNotifier.value = updated.toList(); | |
} | |
void removeItems(List<Widget> itemsToRemove) { | |
final updated = | |
itemsNotifier.value | |
.where((item) => !itemsToRemove.contains(item)) | |
.toList(); | |
itemsNotifier.value = updated; | |
} | |
void clearItems() { | |
itemsNotifier.value = []; | |
} | |
void dispose() => itemsNotifier.dispose(); | |
void refresh() => onRefresh?.call(); | |
void loadMore() => onLoadMore?.call(); | |
void search<T>(T query) => onSearch?.call(query); | |
void filter<T>(T filters) => onFilter?.call(filters); | |
void sort<T>(T sort) => onSort?.call(sort); | |
void onItemPressed<T>(T item) => onPressed?.call(item); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment