2022年6月10日星期五

play lottie after loaded the lottie widget

 import 'package:flutter/material.dart';

import 'lottie/lottie.dart';

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Benny'),
debugShowCheckedModeBanner: false,
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);

final String title;

@override
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
late AnimationController _lottieAnimationController;

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

_lottieAnimationController = AnimationController(vsync: this);

_lottieAnimationController.addStatusListener((status) {
if(status == AnimationStatus.completed) {

}


});
}




@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(

body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [Lottie.asset('assets/bennytext.json',
controller: _lottieAnimationController,
onLoaded: (composition) {
_lottieAnimationController.duration = composition.duration;
Future.delayed(const Duration(milliseconds: 100), () {

setState(() {
_lottieAnimationController.forward();
});
});

}
)
],
),
),
);
}
}

沒有留言:

發佈留言