パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v3.4.0

statemachine

FlutterおよびWebアプリをサポートする、シンプルで強力なDart用のステートマシンフレームワークです。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
1.5万
いいね数
49
Pub ポイント
160

パッケージをインストール

flutter pub add statemachine

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

Dart向けの軽量で柔軟な状態機械フレームワークで、FlutterおよびWebアプリケーションにおける状態管理を簡素化することを目的としています。状態、遷移、アクションを定義する明確な構造を提供し、予測可能で保守しやすいアプリケーションロジックを実現します。同期および非同期の遷移をサポートしており、フォーム処理、認証フロー、UIナビゲーションなどの複雑なワークフローに適しています。シンプルさとパフォーマンスを意識して設計されており、重い依存関係なしに既存プロジェクトにスムーズに統合できます。

用途

  • 複雑なUIワークフローの管理
  • マルチステップフォームの処理
  • 認証フローの実装
  • アプリのナビゲーションを制御する
  • ゲームまたはプロセスの状態をモデル化する

主な機能

  • 状態と遷移を定義するためのシンプルな API
  • 同期および非同期アクションのサポート
  • 組み込みのイベント駆動型アーキテクチャ
  • カスタムガードおよびリスナーによって拡張可能
  • 軽量で依存関係なし

適している対象

  • 構造的な状態管理を求める開発者
  • 予測可能な状態遷移を必要とするプロジェクト
  • 複雑なユーザーインタラクションを持つアプリ
  • 最小限のボイラープレートを望むチーム

注意事項

  • 単純な状態変更には適していない
  • 状態機械の概念を理解する必要がある
  • 制限された組み込みデバッグツール
  • 明確な状態境界を持つアプリに最適

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

State Machine

Pub Package Build Status Code Coverage GitHub Issues GitHub Forks GitHub Stars GitHub License

A simple, yet powerful state machine framework for Dart supporting Flutter and web apps.

This library is open source, stable and well tested. Development happens on GitHub. Feel free to report issues or create a pull-request there. General questions are best asked on StackOverflow.

The package is hosted on dart packages. Up-to-date class documentation is created with every release.

Tutorial

Installation

Follow the installation instructions on dart packages.

Import the package into your Dart code using:

import 'package:statemachine/statemachine.dart';
Creating a machine

To create a new state machine instantiate Machine:

final machine = Machine<String>();

The type of the state machine is used to uniquely identify states of the machine. In this simple example we use a String; but typically you would use an enum, a Symbol, or an arbitrary other identifying object.

Defining states

To create states call Machine.newState and store them in variables. Each state needs an identifying key of the declared type, as described above we use a String that helps debugging.

final startState = machine.newState('start');
final activeState = machine.newState('active');

By default, the first state created is also the start state of the machine. It is possible to explicitly create start and stop states of the machine using Machine.newStartState and Machine.newStopState.

Callbacks on states

States support callbacks whenever a state is entered or left.

someState.onEntry(() => print('activated'));
someState.onExit(() => print('deactivate'));
Starting and stopping a machine

To start a state machine and set its state to its starting state call Machine.start:

machine.start();

Similarly, you can stop a machine by calling Machine.stop.

Transitioning between states

There are various ways in which your machine can switch states.

Manually triggered transition

From anywhere within your code you can enter a specific state by calling State.enter.

someState.enter();

Depending on context, it might be easier to set the current state using the accessor on the state machine itself:

machine.current = someState;

Alternatively, you can also use the identifying object to set the active state:

machine.current = 'active';
Event triggered transition

You can define transitions between states that are triggered by events using State.onStream. The example below registers for click events when the inactive state is entered. In case of a click event the callback is executed, and the state machine transitions into the new state:

someState.onStream(element.onClick, (value) => anotherState.enter());
Future completion transition

Also, transitions can be triggered by the completion of a future using State.onFuture. Since futures cannot be suspended or cancelled, the future continues to run even if the owning state is deactivated. Should the state be activated, and the future value is already present, then the value is immediately supplied into the callback. Further activations have no effect.

someState.onFuture(computation, (value) => anotherState.enter());
Time based transition

Also, you can automatically trigger callbacks after a timeout using State.onTimeout. The following snippet calls the callback 1 second after the active state is entered and falls back to another state:

someState.onTimeout(Duration({seconds: 1}), () => anotherState.enter());

Callbacks often contain code to check for additional constraints and update other objects or UI element before entering a different state. See the tooltip example directory for a more complete illustration of the functionality provided by this library.

Nested machines

Machines can be nested. Simply add another machine that gets started when the state is entered, and stopped when the state is left.

someState.addNested(anotherMachine);

Misc

Resources
License

The MIT License, see LICENSE.

カテゴリー

依存関係

依存関係一覧 1 件

ソースリポジトリ

このパッケージを使用しているプロジェクト