flutter 旋转控件

代码:

import 'package:flutter/cupertino.dart';

import 'package:flutter/material.dart';

class RotationWidget extends StatefulWidget {
  final Widget child;
  final bool isStartDefault;
  final int speedMilliseconds;

  const RotationWidget(
      {Key key,
      this.child,
      this.isStartDefault = true,
      this.speedMilliseconds = 500})
      : super(key: key);

  @override
  State createState() {
    return _RotationWidgetState();
  }
}

class _RotationWidgetState extends State
    with TickerProviderStateMixin {
  //动画控制器
  AnimationController controller;

  stop() {
    if (controller != null) {
      controller.stop();
    }
  }

  start() {
    if (controller != null) {
      controller.forward();
    }
  }

  @override
  void initState() {
    super.initState();
    controller = new AnimationController(
        duration: Duration(milliseconds: widget.speedMilliseconds),
        vsync: this);
    controller.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        controller.reset();
        controller.forward();
      }
    });
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (widget.isStartDefault) controller.forward();
    });
  }

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

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    return buildRotationTransition();
  }

  //旋转
  Widget buildRotationTransition() {
    return Center(
      child: RotationTransition(
        //设置动画的旋转中心
        alignment: Alignment.center,
        //动画控制器
        turns: controller,
        //将要执行动画的子view
        child: widget.child,
      ),
    );
  }

  @override
  void didUpdateWidget(RotationWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.isStartDefault) {
      if (controller != null) {
        controller.forward();
      }
    } else {
      if (controller != null) {
        controller.stop();
      }
    }
  }
}

使用:

                         RotationWidget(
                          isStartDefault:true,
                          speedMilliseconds: 2000,
                          child: Image.asset(
                            "images/progress_music.png",
                            fit: BoxFit.fill,
                          )),

 

你可能感兴趣的:(Flutter)