Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to achieve accelerated playback? #287

Open
FengZi-lv opened this issue Jun 22, 2023 · 1 comment
Open

How to achieve accelerated playback? #287

FengZi-lv opened this issue Jun 22, 2023 · 1 comment

Comments

@FengZi-lv
Copy link

I have an animation, I want to speed up his playback when needed, is there any way to achieve it?
Thanks.

@xvrh
Copy link
Owner

xvrh commented Jun 26, 2023

By providing a custom AnimationController you can control precisely your playback.

Here is an example to speed up x5 (in the onLoaded callback, we get the duration of the animation and divide it by 5 for the custom AnimationController).

import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';

void main() => runApp(const MyApp());

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with TickerProviderStateMixin {
  late final AnimationController _controller;

  @override
  void initState() {
    super.initState();

    _controller = AnimationController(vsync: this);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: ListView(
          children: [
            Lottie.asset(
              'assets/AndroidWave.json',
              controller: _controller,
              frameRate: FrameRate.max,
              onLoaded: (composition) {
                // Configure the AnimationController with the duration of the
                // Lottie file and start the animation.
                _controller
                  ..duration = Duration(
                      milliseconds:
                          (composition.duration.inMilliseconds / 5).round())
                  ..repeat();
              },
            ),
          ],
        ),
      ),
    );
  }
}

Hope this helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants