当前位置:   article > 正文

flutter 旋转指定角度的动画组件_flutter 旋转角度

flutter 旋转角度
  1. import 'package:flutter/material.dart';
  2. /// 旋转动画,旋转指定角度 动画 + Transform.rotate() 实现
  3. class RotateContainer extends StatefulWidget{
  4. final double endAngle; // 旋转角度
  5. final bool rotated; //是否旋转
  6. final Widget child;
  7. final int duration; //旋转时间
  8. RotateContainer({this.endAngle,this.duration, this.rotated = false, this.child});
  9. @override
  10. State<StatefulWidget> createState() {
  11. // TODO: implement createState
  12. return _RotateContainer();
  13. }
  14. }
  15. class _RotateContainer extends State<RotateContainer> with SingleTickerProviderStateMixin{
  16. AnimationController _controller;
  17. Animation<double> _animation;
  18. double angle = 0;
  19. @override
  20. void initState() {
  21. // TODO: implement initState
  22. _controller = AnimationController(vsync: this,duration: Duration(milliseconds: widget.duration));
  23. _animation = Tween(begin: 0.0,end: widget.endAngle).animate(_controller)..addListener((){
  24. setState(() {
  25. angle = _animation.value;
  26. });
  27. });
  28. super.initState();
  29. }
  30. @override
  31. void didUpdateWidget(RotateContainer oldWidget) {
  32. // TODO: implement didUpdateWidget
  33. if(oldWidget.rotated == widget.rotated){
  34. return; //减少绘制
  35. }
  36. if(!widget.rotated){
  37. _controller.reverse();//反向旋转
  38. }else{
  39. _controller.forward();//正向旋转
  40. }
  41. super.didUpdateWidget(oldWidget);
  42. }
  43. @override
  44. void dispose() {
  45. // TODO: implement dispose
  46. _controller.dispose();
  47. super.dispose();
  48. }
  49. @override
  50. Widget build(BuildContext context) {
  51. // TODO: implement build
  52. return Transform.rotate(angle: angle,child: widget.child,);
  53. }
  54. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/341741
推荐阅读
相关标签
  

闽ICP备14008679号