Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

dart

an undo redo library for dart/flutter.

usage

create an changestack to store changes

import 'package:undo/undo.dart';

var changes = new changestack();

add new undo, redo commands using changestack.add(). when a change is added, it calls the change’s execute() method. use change.inline() for simple inline changes.

var count = 0;

changes.add(
  new change.inline(() => count++, () => count--);
  name: "increase"
);

use change.property() when changing a field on an object. this will store the field’s old value so it can be reverted.

var person = new person()
    ..firstname = "john"
    ..lastname = "doe";

changes.add(
  new change.property(
    person.firstname, 
    () => person.firstname = "jane",
    (oldvalue) = person.firstname = oldvalue
  )
)

undo a change with undo().

print(person.firstname); // jane
changes.undo();
print(person.firstname); // john

redo the change with redo().

changes.redo();
print(person.firstname); // jane

use group() to wrap many changes in a transaction. any new changes will be added to the group until commit() is invoked.

count = 0;

changes.group();

var increase = new change.inline(() => count++, () => count--);
changes.add(increase);
changes.add(increase);

changes.commit();
print(count); // 2

changes.undo();
print(count); // 0

calling discard() instead of commit() will unapply the group. this is useful for previewing changes.

count = 0;

changes.group();
changes.add(increase);		
changes.add(increase);
changes.discard();

print(count); // 0

extend change to create your own changes.

class increasechange extends change {

  int value;
  
  increasechange(this.value);
  
  void execute() => value++;
  void undo() => value--;

}

Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.