dart Lottie Animation只在点击时工作一次

c9qzyr3d  于 2022-12-27  发布在  其他
关注(0)|答案(2)|浏览(168)

我正在使用洛蒂动画,并希望它的动画每次我点击它,为此我正在使用GestureDetector,但它只工作的第一次,然后由于某种原因,它不会再工作
下面是代码

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,
              ),
            ),
          ),
        ),
      ),
    );
  }
}
cig3rfwq

cig3rfwq1#

添加侦听器以在动画完成时重置动画,如下所示:

@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();
      }
    });
  }
blmhpbnm

blmhpbnm2#

@Ante Bule thnx,会接受你的答案,这似乎也起作用。

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

相关问题