A frame-accurate state machine for managing Lottie animations in Flutter using Enums.
Add to pubspec.yaml:
dependencies:
lottie_state_machine:
git:
url: https://github.com/Dzmi3y/LottieStateMachine.git
ref: mainUsage
Run Command: Open the terminal in your project folder and run:
flutter pub getQuick Start
- Define states
enum CatState { idle, walk, jump }- Configure animation
final catAnimation = LottieAnimationData<CatState>(
src: 'assets/cat.json',
states: {
CatState.idle: LottieAnimationState(
id: CatState.idle,
startFrame: 0,
endFrame: 29,
isLoop: true,
),
CatState.walk: LottieAnimationState(
id: CatState.walk,
startFrame: 30,
endFrame: 59,
nextStateId: CatState.idle,
),
},
);- Use widget
LottieStateMachineWidget<CatState>(
data: catAnimation,
currentStateId: _currentState,
onAnimationFinished: (state) {
print('$state completed');
},
)- Change state
setState(() => _currentState = CatState.walk);API LottieAnimationState
id - State identifier
startFrame/endFrame - Frame range
nextStateId - Next state (optional)
isLoop - Loop animation (default: false)
speed - Playback speed (default: 1.0)
Requirements
Flutter >= 3.0.0
Dart >= 3.0.0
lottie: ^3.1.0