给定一个在 Stream 事件上“摇晃”的小部件
class Shaker extends StatefulWidget {
final Widget child;
final Stream<void> stream;
final Duration duration;
final int loops;
const Shaker({
required this.child,
required this.stream,
this.duration = const Duration(milliseconds: 100),
this.loops = 2,
Key? key,
}) : super(key: key);
@override
State<Shaker> createState() => _ShakerState();
}
class _ShakerState extends State<Shaker> with SingleTickerProviderStateMixin {
late final AnimationController _controller;
late final StreamSubscription _subscription;
late final Animation<Offset> _offsetAnimation;
@override
void initState() {
_controller = AnimationController(
vsync: this,
duration: widget.duration,
);
_subscription = widget.stream.listen((event) async {
for (var i = 0; i < widget.loops; ++i) {
await _controller.forward();
await _controller.reverse();
}
});
_offsetAnimation = Tween<Offset>(
begin: Offset.zero,
end: const Offset(.02, 0.0),
).animate(CurvedAnimation(
parent: _controller,
curve: Curves.elasticIn,
));
super.initState();
}
@override
void dispose() {
_controller.dispose();
_subscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return SlideTransition(
position: _offsetAnimation,
child: widget.child,
);
}
}
什么是一种优雅的方法来测试收到的事件是否实际发生了“抖动”?
谢谢你