history_state_notifier 0.0.6+1 copy "history_state_notifier: ^0.0.6+1" to clipboard
history_state_notifier: ^0.0.6+1 copied to clipboard

Extends state_notifier to add a history feature that allows for undo/redo operations.

example/lib/main.dart

import 'package:state_notifier/state_notifier.dart';
import 'package:history_state_notifier/history_state_notifier.dart';

void main() {
  final notifier = CounterNotifier();
  notifier.increment();
  print(notifier.state);
  notifier.undo();
  print(notifier.state);
  print(notifier.canRedo);
  notifier.increment();
  print(notifier.state);
  print(notifier.canRedo);
}

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;
  }
}
16
likes
130
pub points
26%
popularity

Publisher

verified publishertimcreated.it

Extends state_notifier to add a history feature that allows for undo/redo operations.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

meta, state_notifier

More

Packages that depend on history_state_notifier