Git Product home page Git Product logo

history_manager's Introduction

history_manager

An action manager for saving executed changes that allows redoing and undoing of changes occured.

169703219-eca4c6b6-83d8-413f-9d89-a16817beb9aa

Installation

dependencies:
    history_manager: 0.1.0

dependency_overrides:
    history_manager:
        git: https://github.com/rynssnjn/history_manager.git

Usage

Initializing the controller.

import 'package:history_manager/history_manager.dart';

HistoryController controller = new HistoryController();

Creating a simple model by extending to ExecutedChange wherein it accepts different types.

class IncrementChange extends ExecutedChange<int> {
  IncrementChange({
    this.oldValue,
    this.onExecute,
    this.onUndo,
  });
  @override
  final int oldValue;

  @override
  final VoidCallback onExecute;

  @override
  final OnUndoChange<int> onUndo;
}

Adding a new executed change.

var counter = 0;
controller.add(IncrementChange(
  oldValue: counter,
  onExecute: () => counter++,
  onUndo: (c) => counter = c,
));

Checking if there is an undoable or a redoable changes.

controller.canUndo; // checks if there is an undoable change
controller.canRedo; // checks if there is a redoable change.

Undoing the change from the last executed change.

print(counter); // 1
controller.undo();
print(counter); // 0

Redoing the last undone change.

print(counter); // 0
controller.redo();
print(counter); // 1

Example

class IncrementNumbers extends StatefulWidget {
  const IncrementNumbers({
    Key key,
    @required this.title,
  }) : super(key: key);

  final String title;

  @override
  State<IncrementNumbers> createState() => _IncrementNumbersState();
}

class _IncrementNumbersState extends State<IncrementNumbers> {
  int _counter = 0;
  HistoryController controller;

  void _incrementCounter() {
    controller.add(IncrementChange(
      oldValue: _counter,
      onExecute: () => setState(() => _counter++),
      onUndo: (lastValue) => setState(() => _counter = lastValue),
    ));
  }

  @override
  void initState() {
    controller = HistoryController();
    super.initState();
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text('Current count:'),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
            CommonHistoryTools(
              onUndo: controller.undo,
              onRedo: controller.redo,
              canUndo: controller.canUndo,
              canRedo: controller.canRedo,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

history_manager's People

Contributors

ffuf-vince avatar raelsj avatar rynssnjn avatar

Watchers

 avatar

Forkers

sunlightbro

history_manager's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.