undo 1.0.1+3

  • Readme
  • Changelog
  • Example
  • Installing
  • 79

undo #

An undo redo library for Dart/Flutter. Forked from here and updated for Flutter. Demo can be viewed here.

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--;

}

1.0.1 #

  • Adding Stream API

1.0.0 #

  • Forked abandoned project and updated with latest Flutter

example/lib/main.dart

import 'package:flutter/material.dart';

import 'package:undo/undo.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.light(),
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  int count = 0;
  var changes = ChangeStack();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Undo/Redo Example'),
      ),
      body: Center(
        child: Text('Count: $count'),
      ),
      bottomNavigationBar: BottomAppBar(
        child: Row(
          children: <Widget>[
            IconButton(
              icon: Icon(Icons.arrow_back),
              onPressed: !changes.canUndo
                  ? null
                  : () {
                      if (mounted)
                        setState(() {
                          changes.undo();
                        });
                    },
            ),
            IconButton(
              icon: Icon(Icons.arrow_forward),
              onPressed: !changes.canRedo
                  ? null
                  : () {
                      if (mounted)
                        setState(() {
                          changes.redo();
                        });
                    },
            ),
          ],
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
      floatingActionButton: FloatingActionButton(
        heroTag: ValueKey('add_button'),
        child: Icon(Icons.add),
        onPressed: () {
          if (mounted)
            setState(() {
              changes.add(Change.inline(
                () => count++,
                () => count--,
              ));
              changes.commit();
            });
        },
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  undo: ^1.0.1+3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:undo/undo.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
67
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
79
Learn more about scoring.

We analyzed this package on Apr 4, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health issues and suggestions

Document public APIs. (-1 points)

31 out of 31 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Format lib/src/undo_stack.dart.

Run flutter format to format lib/src/undo_stack.dart.

Format lib/undo.dart.

Run flutter format to format lib/undo.dart.

Maintenance suggestions

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
mockito any