Dart용 가볍고 유연한 상태 기계 프레임워크로, Flutter 및 웹 애플리케이션에서 상태 관리를 간소화하도록 설계되었습니다. 상태, 전이, 동작을 정의하기 위한 명확한 구조를 제공하여 예측 가능하고 유지보수 가능한 애플리케이션 로직을 가능하게 합니다. 동기 및 비동기 전이를 모두 지원하여 폼 처리, 인증 흐름, UI 탐색과 같은 복잡한 워크플로우에 적합합니다. 단순성과 성능을 고려해 제작되었으며, 무거운 종속성 없이 기존 프로젝트에 원활하게 통합됩니다.
사용 사례
복잡한 UI 워크플로우 관리
다단계 양식 처리
인증 흐름 구현
앱 탐색 제어
게임 또는 프로세스 상태 모델링
주요 기능
상태와 전이를 정의하기 위한 간단한 API
동기 및 비동기 작업 지원
내장된 이벤트 기반 아키텍처
사용자 정의 가드 및 리스너를 통해 확장 가능
경량이며 종속성 없음
적합한 대상
구조적 상태 관리를 원하는 개발자
예측 가능한 상태 전이가 필요한 프로젝트
복잡한 사용자 상호작용을 가진 앱
최소한의 보일러플레이트를 원하는 팀
고려 사항
간단한 상태 변경에 적합하지 않음
상태 기계 개념에 대한 이해가 필요합니다
제한된 내장 디버깅 도구
명확한 상태 경계를 가진 앱에 적합
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
State Machine
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.
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.
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:
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.
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:
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.