https://pub.dev/packages/statemachine
// Creating machine
final machine = machine<String>();
// Define State
final startState = machine.newState(‘start’);
final activityState = machine.newState(‘active’);
// State Callback
someState.onEntry(() { … });
someState.onExit(() { … });
// Machine Start & Stop
machine.start();
machine.stop();
// 상태 수동 전환
someState.enter();
// 시간 기반 전환
someState.onTimeout(Duration({seconds : 1}), () => anotherState.enter());
실제 사용 예제 코드
// Creating machine
var _machine = wmState.Machine();
get machine => _machine;
// Define state
final STEP_STUDY_PRETEST_START = machine.newState('STEP_STUDY_PRETEST_START');
// State Callback
STEP_STUDY_PRETEST_START.onEntry(() async {
if (mSentenceIndex == 0 && retryCount == 0) {
…
}});
// Machine Start
machine.start();
// 상태 수동 전환
btnNestState.onEntry((){
speechBubble.add(btnNext(stepID:selectLevel);
notifyListener();
})
// 시간 기반 전환
aiInterestSelect.onTimeout(const Duration(seconds : 3), () {
btnNextState.enter();
});
machine정의 할 때, statemachine패키지를 impor하면 Flutter의 statefulwidget과 이름 중복이 발생하여 as키워드로 이름을 변경해 준다.
