-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathanimation_full_control.dart
111 lines (102 loc) · 3.08 KB
/
animation_full_control.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';
/// This example show how to play the Lottie animation in various way:
/// - Start and stop the animation on event callback
/// - Play the animation forward and backward
/// - Loop between two specific frames
///
/// This works by creating an AnimationController instance and passing it
/// to the Lottie widget.
/// The AnimationController class has a rich API to run the animation in various ways.
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)
..value = 0.5
..addListener(() {
setState(() {
// Rebuild the widget at each frame to update the "progress" label.
});
});
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Animation control'),
),
body: Column(
children: <Widget>[
const SizedBox(height: 20),
Lottie.asset(
'assets/LottieLogo1.json',
controller: _controller,
height: 300,
onLoaded: (composition) {
setState(() {
_controller.duration = composition.duration;
});
},
),
Text(_controller.value.toStringAsFixed(2)),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Play backward
IconButton(
icon: const Icon(Icons.arrow_left),
onPressed: () {
_controller.reverse();
},
),
// Pause
IconButton(
icon: const Icon(Icons.pause),
onPressed: () {
_controller.stop();
},
),
// Play forward
IconButton(
icon: const Icon(Icons.arrow_right),
onPressed: () {
_controller.forward();
},
),
],
),
const SizedBox(height: 30),
ElevatedButton(
onPressed: () {
// Loop between 2 specifics frames
var start = 0.1;
var stop = 0.5;
_controller.repeat(
min: start,
max: stop,
reverse: true,
period: _controller.duration! * (stop - start),
);
},
child: const Text('Loop between frames'),
),
],
),
),
);
}
}