history_state_notifier 0.0.6+1 history_state_notifier: ^0.0.6+1 copied to clipboard
Extends state_notifier to add a history feature that allows for undo/redo operations.
DISCONTINUED #
Development of this package has been discontinued due to the sunsetting of state_notifier
in riverpod. If you are looking for a similar package, please check out value_notifier_tools
which works with ValueNotifier
instead, has no external dependencies and will be maintained actively.
Thank you for your support!
Old README: #
This package offers an extension on state_notifier to add a full history support with undo/redo functionality.
Features #
- âĐïļ Add
undo()
andredo()
to StateNotifier - ð Limit the size of your history
- ð Offers both a mixin that can be added to your existing StateNotifiers and an abstract class that you can extend
- ðŠķ No dependencies on flutter or any other packages and super lightweight.
- ð Choose which states get stored to the history
- ð Transform states before applying them from the history
Getting started #
This package is designed to work with/like StateNotifier. If you're not using StateNotifier and don't plan to use it you won't get much value out of this.
Usage #
Upgrade an existing StateNotifier #
class CounterNotifier extends StateNotifier<int>
with HistoryStateNotifierMixin<int> {
CounterNotifier() : super(0) {
// If you want the initial state to be added to the history, do it like this
state = 0;
// This is how you limit the size of your history.
// Set it to null to keep all state (default)
maxHistoryLength = 30;
}
void increment() => ++state;
void decrement() => --state;
// By using temporaryState setter, the change won't be stored in history
void reset() => temporaryState = 0;
// You can override this function to apply a transformation to a state
// from the history before it gets applied.
@override
int transformHistoryState(int newState, int currentState) {
return newState;
}
}
Create a HistoryStateNotifier #
If you prefer to create a HistoryStateNotifier directly, you can do this instead:
class CounterNotifier extends HistoryStateNotifier<int> {
// ... Same as above
}
Use It! #
You can now use the full functionality of the HistoryStateNotifier!
// Obtain a reference however you wish
final CounterNotifier notifier = watch(counterProvider.notifier);
notifier.increment(); // 1
notifier.undo(); // 0
notifier.redo(); // 1
notifier.decrement(); // 0
notifier.undo(); // 1
notifier.canRedo // true
notifier.increment // 2
notifier.canRedo // false
// ...