skip to Main Content

I am using Lottie Animation and want it to animate everytime I click on it , To this I am using GestureDetector However it only works the first time then for some reason it wont work again

Here is the code

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

void main() async {
  runApp(const App());
}

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

  @override
  State<App> createState() {
    return _AppState();
  }
}

class _AppState extends State<App> with SingleTickerProviderStateMixin {
  late final AnimationController my_location_controller;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    my_location_controller =
        AnimationController(vsync: this, duration: const Duration(seconds: 5));
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      color: Colors.lightBlue,
      home: Scaffold(
        backgroundColor: Colors.lightBlue,
        body: Center(
          child: SizedBox(
            width: 300,
            height: 300,
            child: GestureDetector(
              onTap: () {
                my_location_controller.forward();
              },
              child: Lottie.asset(
                'assets/my_location.json',
                controller: my_location_controller,
                animate: true,
                repeat: true,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

2

Answers


  1. Chosen as BEST ANSWER

    @Ante Bule thnx, will accept your answer and this seems to work too ..

     child: GestureDetector(
                  onTap: () {
                    my_location_controller.reset();
                    my_location_controller.forward();
                  },
                  child: Lottie.asset(
                    'assets/my_location.json',
                    controller: my_location_controller,
                   
                  ),
    

  2. Add a listener to reset your animation when it gets completed, like this:

    @override
      void initState() {
        super.initState();
        my_location_controller =
            AnimationController(vsync: this, duration: const Duration(seconds: 5));
        my_location_controller.addStatusListener((status) {
          if (status == AnimationStatus.completed) {
            my_location_controller.reset();
          }
        });
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search