Flutter---设备搜索动画效果(3)

介绍:扇形不断绕着圆形旋转

效果图

核心代码

Dart 复制代码
 // 旋转的扇形
            Container(
              width: 245,
              height: 245,
              child: CustomPaint(
                painter: SectorPainter(
                  startAngle: _animation.value, //开始角度设置为动画值,使扇形一直变化旋转
                  sweepAngle: pi / 4,
                  color: const Color(0xFF21A2EF).withOpacity(0.3),  // 纯蓝色
                ),
              ),
            )

实现步骤

1.定义相关变量

Dart 复制代码
double startAngle = 0;
late AnimationController _controller;
late Animation<double> _animation;

2.初始化动画控制器和动画

Dart 复制代码
  @override
  void initState() {
    super.initState();

    //动画控制器
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 3),
    );


    //动画
    _animation = TweenSequence<double>([
      TweenSequenceItem(
        tween: Tween<double>(begin: 0, end: pi / 2), //四分之一圆
        weight: 1.0,
      ),
      TweenSequenceItem(
        tween: Tween<double>(begin: pi / 2, end: pi), //半圆
        weight: 1.0,
      ),
      TweenSequenceItem(
        tween: Tween<double>(begin: pi, end: pi * 1.5), //四分之三圆
        weight: 1.0,
      ),
      TweenSequenceItem(
        tween: Tween<double>(begin: pi * 1.5, end: pi * 2), //圆
        weight: 1.0,
      ),
    ]).animate(
      CurvedAnimation(parent: _controller, curve: Curves.linear), //绑定控制器
    )..addListener(() {
      setState(() {});
    });

    // 启动动画(无限循环)
    _controller.repeat();  // 重复播放
  }

3.注销

Dart 复制代码
 @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

4.扇形的绘制

Dart 复制代码
// 扇形绘制器
class SectorPainter extends CustomPainter {
  final double startAngle; //起始角度
  final double sweepAngle; //扫描角度(扇形的大小)
  final Color color;  // 改为单个颜色

  SectorPainter({
    required this.startAngle,
    required this.sweepAngle,
    required this.color,  // 单个颜色
  });

  @override
  void paint(Canvas canvas, Size size) {

    //创建绘制区域
    final Rect rect = Rect.fromLTWH(0, 0, size.width, size.height);

    //创建画笔
    final Paint paint = Paint()
      ..color = color
      ..style = PaintingStyle.fill;

    //绘制圆弧
    canvas.drawArc(
      rect,
      startAngle,
      sweepAngle,
      true,
      paint,
    );
  }


  //重绘判断
  @override
  bool shouldRepaint(covariant SectorPainter oldDelegate) {
    return oldDelegate.startAngle != startAngle ||
        oldDelegate.sweepAngle != sweepAngle ||
        oldDelegate.color != color;
  }
}

5.UI的构建

Dart 复制代码
  @override
  Widget build(BuildContext context) {
    return Container(
        height: 245,
        width: double.infinity,
        child: Stack(
          alignment: Alignment.center,
          children: [
            // 最外层圆环
            Positioned(
              child: Container(
                height: 245,
                width: 245,
                decoration: BoxDecoration(
                  color: const Color(0xFFD4D7DC).withOpacity(0.2),
                  shape: BoxShape.circle,
                ),
              ),
            ),
            // 中间圆环
            Positioned(
              child: Container(
                height: 200,
                width: 200,
                decoration: BoxDecoration(
                  color: const Color(0xFFD4D7DC).withOpacity(0.3),
                  shape: BoxShape.circle,
                ),
              ),
            ),
            // 内层圆环
            Positioned(
              child: Container(
                height: 155,
                width: 155,
                decoration: BoxDecoration(
                  color: const Color(0xFFD4D7DC).withOpacity(0.3),
                  shape: BoxShape.circle,
                ),
              ),
            ),


            // 旋转的扇形
            Container(
              width: 245,
              height: 245,
              child: CustomPaint(
                painter: SectorPainter(
                  startAngle: _animation.value, //开始角度设置为动画值,使扇形一直变化旋转
                  sweepAngle: pi / 4,
                  color: const Color(0xFF21A2EF).withOpacity(0.3),  // 纯蓝色
                ),
              ),
            )
          ],
        )
    );
  }
}

代码示例

Dart 复制代码
import 'package:flutter/material.dart';
import 'dart:math';

class SearchDeviceAnimation extends StatefulWidget {
  const SearchDeviceAnimation({super.key});

  @override
  State<SearchDeviceAnimation> createState() => _SearchDeviceAnimationState();
}

class _SearchDeviceAnimationState extends State<SearchDeviceAnimation> with SingleTickerProviderStateMixin {


  double startAngle = 0;
  late AnimationController _controller;
  late Animation<double> _animation;

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

    //动画控制器
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 3),
    );


    //动画
    _animation = TweenSequence<double>([
      TweenSequenceItem(
        tween: Tween<double>(begin: 0, end: pi / 2), //四分之一圆
        weight: 1.0,
      ),
      TweenSequenceItem(
        tween: Tween<double>(begin: pi / 2, end: pi), //半圆
        weight: 1.0,
      ),
      TweenSequenceItem(
        tween: Tween<double>(begin: pi, end: pi * 1.5), //四分之三圆
        weight: 1.0,
      ),
      TweenSequenceItem(
        tween: Tween<double>(begin: pi * 1.5, end: pi * 2), //圆
        weight: 1.0,
      ),
    ]).animate(
      CurvedAnimation(parent: _controller, curve: Curves.linear), //绑定控制器
    )..addListener(() {
      setState(() {});
    });

    // 启动动画(无限循环)
    _controller.repeat();  // 重复播放
  }

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

  @override
  Widget build(BuildContext context) {
    return Container(
        height: 245,
        width: double.infinity,
        child: Stack(
          alignment: Alignment.center,
          children: [
            // 最外层圆环
            Positioned(
              child: Container(
                height: 245,
                width: 245,
                decoration: BoxDecoration(
                  color: const Color(0xFFD4D7DC).withOpacity(0.2),
                  shape: BoxShape.circle,
                ),
              ),
            ),
            // 中间圆环
            Positioned(
              child: Container(
                height: 200,
                width: 200,
                decoration: BoxDecoration(
                  color: const Color(0xFFD4D7DC).withOpacity(0.3),
                  shape: BoxShape.circle,
                ),
              ),
            ),
            // 内层圆环
            Positioned(
              child: Container(
                height: 155,
                width: 155,
                decoration: BoxDecoration(
                  color: const Color(0xFFD4D7DC).withOpacity(0.3),
                  shape: BoxShape.circle,
                ),
              ),
            ),


            // 旋转的扇形
            Container(
              width: 245,
              height: 245,
              child: CustomPaint(
                painter: SectorPainter(
                  startAngle: _animation.value, //开始角度设置为动画值,使扇形一直变化旋转
                  sweepAngle: pi / 4,
                  color: const Color(0xFF21A2EF).withOpacity(0.3),  // 纯蓝色
                ),
              ),
            )
          ],
        )
    );
  }
}


// 扇形绘制器
class SectorPainter extends CustomPainter {
  final double startAngle; //起始角度
  final double sweepAngle; //扫描角度(扇形的大小)
  final Color color;  // 改为单个颜色

  SectorPainter({
    required this.startAngle,
    required this.sweepAngle,
    required this.color,  // 单个颜色
  });

  @override
  void paint(Canvas canvas, Size size) {

    //创建绘制区域
    final Rect rect = Rect.fromLTWH(0, 0, size.width, size.height);

    //创建画笔
    final Paint paint = Paint()
      ..color = color
      ..style = PaintingStyle.fill;

    //绘制圆弧
    canvas.drawArc(
      rect,
      startAngle,
      sweepAngle,
      true,
      paint,
    );
  }


  //重绘判断
  @override
  bool shouldRepaint(covariant SectorPainter oldDelegate) {
    return oldDelegate.startAngle != startAngle ||
        oldDelegate.sweepAngle != sweepAngle ||
        oldDelegate.color != color;
  }
}
相关推荐
向阳是我1 小时前
Flutter Android 编译错误修复:JVM Target Compatibility 不一致问题记录
android·jvm·flutter
恋猫de小郭2 小时前
Flutter 凉了没?Flutter 2026 的未来行程和规划,一些有趣的变化
android·前端·flutter
Lanren的编程日记2 小时前
任务77:Flutter 鸿蒙应用视频录制功能实战:视频录制+录制控制+视频编辑,打造完整视频处理能力
flutter·音视频·harmonyos
Hello__77772 小时前
开源鸿蒙 Flutter 实战|进度条组件全流程实现
flutter·开源·harmonyos
IntMainJhy2 小时前
【flutter for open harmony】第三方库 Flutter分享卡片的鸿蒙化适配与实战指南
flutter·华为·harmonyos
Lanren的编程日记3 小时前
任务76:Flutter 鸿蒙应用音频录制功能实战:音频录制+录音管理+录音编辑,打造完整音频处理能力
flutter·华为·音视频·harmonyos
IntMainJhy3 小时前
【flutter for open harmony】第三方库 Flutter运动计时器的鸿蒙化适配与实战指南
flutter·华为·信息可视化·数据库开发·harmonyos
Hello__77773 小时前
开源鸿蒙 Flutter 实战|徽章组件全流程实现
flutter·开源·harmonyos
IntMainJhy3 小时前
【flutter for open harmony】 第三方库 Flutter饮食记录的鸿蒙化适配与实战指南
flutter·华为·信息可视化·数据库开发·harmonyos