History Value Notifier

Powered by Mason melos coverage

Easy undo/redo for ValueNotifiers

Installation ðŸ’ŧ

❗ In order to start using History Value Notifier you must have the dart_install_link installed on your machine.

Install via dart pub add:

dart pub add history_value_notifier

Features

  • â†Đïļ Add undo() and redo() to ValueNotifier
  • 🕐 Limit the size of your history
  • 💕 Offers both a mixin that can be added to your existing ValueNotifiers and a class that you can extend
  • ðŸŠķ No dependencies on any other packages and super lightweight.
  • 🔎 Choose which states get stored to the history
  • 🔄 Transform states before applying them from the history

Usage

Getting started is easy! There are three main ways in which you can add HistoryValueNotifier to your project:

Use it as-is

If you don't need any extra functionality, you can use HistoryValueNotifier as-is.

import 'package:history_value_notifier/history_value_notifier.dart';

final notifier = HistoryValueNotifier<int>(0);
notifier.value = 1;
notifier.undo(); // 0
notifier.redo(); // 1

Upgrade an existing ValueNotifier

class CounterNotifier extends ValueNotifier<int>
    with HistoryValueNotifierMixin<int> {
  CounterNotifier() : super(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 HistoryValueNotifier

If you prefer to create a HistoryValueNotifier directly, you can do this instead:

class CounterNotifier extends HistoryValueNotifier<int> {
  // ... Same as above
}

Use It!

You can now use the full functionality of the HistoryValueNotifier!

// Obtain a reference however you wish (Provider, GetIt, etc.)
final CounterNotifier notifier = context.read(counterNotifier);

notifier.increment(); // 1
notifier.undo(); // 0
notifier.redo(); // 1

notifier.decrement(); // 0
notifier.undo(); // 1
notifier.canRedo // true
notifier.increment // 2
notifier.canRedo // false

// ...

Libraries

history_value_notifier
Easy undo/redo for ValueNotifiers