Methods
Control your animations programmatically using the DotLottieViewController.
Getting the Controller
Access the controller through the onViewCreated callback:
DotLottieViewController? _controller;
DotLottieView( source: 'assets/animation.lottie', sourceType: 'asset', onViewCreated: (controller) { _controller = controller; },)
// Later, use the controllerawait _controller?.play();Playback Control
Basic playback control methods.
| Method | Returns | Description |
|---|---|---|
play() | Future<bool?> | Starts playing the animation |
pause() | Future<bool?> | Pauses the animation |
stop() | Future<bool?> | Stops the animation and resets to the beginning |
Example
ElevatedButton( onPressed: () async { await _controller?.play(); }, child: const Text('Play'),)
ElevatedButton( onPressed: () async { await _controller?.pause(); }, child: const Text('Pause'),)
ElevatedButton( onPressed: () async { await _controller?.stop(); }, child: const Text('Stop'),)Animation State Getters
Query the current state of the animation.
| Method | Returns | Description |
|---|---|---|
isPlaying() | Future<bool?> | Returns whether the animation is currently playing |
isPaused() | Future<bool?> | Returns whether the animation is paused |
isStopped() | Future<bool?> | Returns whether the animation is stopped |
isLoaded() | Future<bool?> | Returns whether the animation has loaded |
currentFrame() | Future<double?> | Gets the current frame number |
totalFrames() | Future<double?> | Gets the total number of frames |
currentProgress() | Future<double?> | Gets the current progress (0.0 to 1.0) |
duration() | Future<double?> | Gets the animation duration in milliseconds |
loopCount() | Future<int?> | Gets the current loop count |
speed() | Future<double?> | Gets the current playback speed |
loop() | Future<bool?> | Gets the loop setting |
autoplay() | Future<bool?> | Gets the autoplay setting |
useFrameInterpolation() | Future<bool?> | Gets the frame interpolation setting |
segments() | Future<List<double>?> | Gets the active segment [start, end] |
mode() | Future<String?> | Gets the current playback mode |
Example
// Check if animation is playingfinal playing = await _controller?.isPlaying();if (playing == true) { print('Animation is playing');}
// Get current progressfinal progress = await _controller?.currentProgress();print('Progress: ${(progress ?? 0) * 100}%');
// Get animation infofinal frames = await _controller?.totalFrames();final duration = await _controller?.duration();print('$frames frames, $duration ms');Animation Control Setters
Modify animation behavior at runtime.
| Method | Returns | Description |
|---|---|---|
setSpeed(double speed) | Future<void> | Sets the playback speed |
setLoop(bool loop) | Future<void> | Sets whether the animation should loop |
setFrame(double frame) | Future<bool?> | Sets the current frame |
setProgress(double progress) | Future<bool?> | Sets the progress (0.0 to 1.0) |
setSegments(double start, double end) | Future<void> | Sets a segment of the animation to play |
setMarker(String marker) | Future<void> | Sets a marker for playback |
setMode(String mode) | Future<void> | Sets the playback mode |
setFrameInterpolation(bool useFrameInterpolation) | Future<void> | Enables or disables frame interpolation |
Example
// Change speed dynamicallySlider( value: _speed, min: 0.5, max: 3.0, onChanged: (value) { setState(() => _speed = value); _controller?.setSpeed(value); },)
// Seek to 50% progressawait _controller?.setProgress(0.5);
// Play a specific segmentawait _controller?.setSegments(30, 90);await _controller?.play();
// Change playback modeawait _controller?.setMode('bounce');Theme Methods
Apply and manage animation themes.
| Method | Returns | Description |
|---|---|---|
setTheme(String themeId) | Future<bool?> | Applies a theme by ID |
setThemeData(String themeData) | Future<bool?> | Applies a theme using JSON data |
resetTheme() | Future<bool?> | Resets to the default theme |
activeThemeId() | Future<String?> | Gets the currently active theme ID |
Example
// Apply a themeawait _controller?.setTheme('darkMode');
// Apply custom theme datafinal themeJson = '{"colors":{"primary":"#FF0000"}}';await _controller?.setThemeData(themeJson);
// Reset to defaultawait _controller?.resetTheme();
// Check active themefinal theme = await _controller?.activeThemeId();print('Active theme: $theme');Animation Loading
Load and manage multiple animations.
| Method | Returns | Description |
|---|---|---|
loadAnimation(String animationId) | Future<void> | Loads a specific animation by ID |
activeAnimationId() | Future<String?> | Gets the currently active animation ID |
markers() | Future<List<Map<String, dynamic>>?> | Gets all available markers |
Example
// Load a specific animationawait _controller?.loadAnimation('animation2');
// Get active animationfinal animId = await _controller?.activeAnimationId();print('Current animation: $animId');
// Get all markersfinal markers = await _controller?.markers();markers?.forEach((marker) { print('Marker: ${marker['name']} at frame ${marker['time']}');});Advanced Features
Advanced control methods.
| Method | Returns | Description |
|---|---|---|
setSlots(String slots) | Future<bool?> | Sets slot data for dynamic content |
resize(int width, int height) | Future<void> | Resizes the animation viewport |
getLayerBounds(String layerName) | Future<List<double>?> | Gets the bounds of a specific layer |
manifest() | Future<Map<String, dynamic>?> | Gets the animation manifest data |
Example
// Resize the animationawait _controller?.resize(800, 600);
// Get layer boundsfinal bounds = await _controller?.getLayerBounds('myLayer');print('Layer bounds: $bounds');
// Get manifestfinal manifest = await _controller?.manifest();print('Animations: ${manifest?['animations']}');State Machine Methods
Control state machines for interactive animations. See the State Machines Guide for detailed examples.
| Method | Returns | Description |
|---|---|---|
stateMachineLoad(String stateMachineId) | Future<bool?> | Loads a state machine by ID |
stateMachineLoadData(String data) | Future<bool?> | Loads a state machine from JSON data |
stateMachineStart() | Future<bool?> | Starts the loaded state machine |
stateMachineStop() | Future<bool?> | Stops the state machine |
stateMachineFire(String event) | Future<void> | Fires an event input in the state machine |
stateMachineSetNumericInput(String key, double value) | Future<bool?> | Sets a numeric input value |
stateMachineSetStringInput(String key, String value) | Future<bool?> | Sets a string input value |
stateMachineSetBooleanInput(String key, bool value) | Future<bool?> | Sets a boolean input value |
stateMachineGetNumericInput(String key) | Future<double?> | Gets a numeric input value |
stateMachineGetStringInput(String key) | Future<String?> | Gets a string input value |
stateMachineGetBooleanInput(String key) | Future<bool?> | Gets a boolean input value |
stateMachineGetInputs() | Future<Map<String, String>?> | Gets all input names and their types |
stateMachineCurrentState() | Future<String?> | Gets the current state name |
getStateMachine(String id) | Future<String?> | Gets state machine data by ID |
Example
// Load and start a state machineawait _controller?.stateMachineLoad('myStateMachine');await _controller?.stateMachineStart();
// Set inputsawait _controller?.stateMachineSetBooleanInput('isActive', true);await _controller?.stateMachineSetNumericInput('speed', 2.0);
// Fire an eventawait _controller?.stateMachineFire('buttonPressed');
// Get current statefinal state = await _controller?.stateMachineCurrentState();print('Current state: $state');Cleanup
| Method | Returns | Description |
|---|---|---|
dispose() | Future<void> | Disposes of the controller and releases resources |
Example
@overridevoid dispose() { _controller?.dispose(); super.dispose();}