flutter 实现闪烁动画

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
import 'dart:async';

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

class TaskBlinkingButton extends StatefulWidget {
final Widget child;
Function onClick;
bool isAnimating ;

TaskBlinkingButton({Key? key, required this.child, required this.onClick, required this.isAnimating}): super(key: key);

@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _taskblinkState();
}

}

class _taskblinkState extends State<TaskBlinkingButton>
with
SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _opacityAnimation;

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

_controller = AnimationController(
duration: Duration(milliseconds: 300),
vsync: this,
)..repeat(reverse: true);
_opacityAnimation = Tween<double>(begin: 0.2, end: 1.0).animate(
CurvedAnimation(
parent: _controller,
curve: Curves.easeInOut,
),
);
}

@override
Widget build(BuildContext context) {
return AnimatedBuilder(animation: _controller, builder: (BuildContext context, Widget? child) {
return AnimatedOpacity(
opacity: widget.isAnimating ? _opacityAnimation.value : 1,
duration: Duration(milliseconds: 500),
child: CupertinoButton(
onPressed: () {
widget.onClick();
},
child: widget.child,
),
);
},);


}

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