I have a problem. I want to create a recording. How could I create a blinking red circle like the code below. What is the best option to create a simple blinking red circle?
Example created in SVG
context
<svg>
<circle fill="#ff0000" stroke="none" cx="60" cy="60" r="12">
<animate attributeName="opacity" dur="1s" values="0;1;0" repeatCount="indefinite" begin="0.1" />
</circle>
</svg>
I tried something like, but nothing happend. There is no red circle/ container shown.
BlinkWidget(children: <Widget>[Container(color: Colors.red,)])
import 'package:flutter/cupertino.dart';
class BlinkWidget extends StatefulWidget {
final List<Widget> children;
final int interval;
const BlinkWidget({required this.children, this.interval = 500, Key? key}) : super(key: key);
@override
_BlinkWidgetState createState() => _BlinkWidgetState();
}
class _BlinkWidgetState extends State<BlinkWidget> with SingleTickerProviderStateMixin {
late AnimationController _controller;
int _currentWidget = 0;
@override
initState() {
super.initState();
_controller = AnimationController(
duration: Duration(milliseconds: widget.interval),
vsync: this
);
_controller.addStatusListener((status) {
if(status == AnimationStatus.completed) {
setState(() {
if(++_currentWidget == widget.children.length) {
_currentWidget = 0;
}
});
_controller.forward(from: 0.0);
}
});
_controller.forward();
}
@override
dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
child: widget.children[_currentWidget],
);
}
}
2
Answers
Here is a widget that adds the blinking animation to any widget passed to it:
You can call the BlinkingWidget passing the widget you want, in your case a red circle:
By having a widget that only takes care of adding the blinking animation, you can reuse it whenever and wherever you want, with different widgets as well.
by using this plug in you can do the same.
flutter_animate