Flutter类似于电力输送的动画

利用flutter写了个类似于充电的动画,比如光伏板给电池充电,模仿电力输送的过程,效果如下图所示

gif.gif

下面直接贴出代码,属性都标注了注释,使用的时候在外面加个盒子固定宽高,然后给出每个点的坐标,内部会根据坐标的点数来判断有几根线,然后画出线的路径,进行动画模拟,可以根据项目需求来自定义点的颜色,每条路径上点的个数以及点的大小等等,
import 'package:flutter/material.dart';

class ElectricAnimated extends StatelessWidget {
  final int duration; //动画时长
  final List<Offset> path; //路径的点坐标
  const ElectricAnimated({
    super.key,
    this.duration = 5,
    required this.path,
  });

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: List.generate(
        path.length - 1,
        (index) {
          return AnimatedDots(
            path: [
              path[index],
              path[index + 1],
            ],
            dotCount: 3,
            duration: Duration(
              seconds: duration,
            ),
          );
        },
      ),
    );
  }
}

class AnimatedDots extends StatefulWidget {
  final List<Offset> path; //路径的点坐标
  final int dotCount; //每条路径上的点的个数
  final double dotSize; //点的大小
  final Duration duration;
  final Color dotColor; //点的颜色

  const AnimatedDots({
    Key? key,
    required this.path,
    this.dotCount = 5,
    this.dotSize = 2,
    this.duration = const Duration(seconds: 2),
    this.dotColor = Colors.white,
  }) : super(key: key);

  @override
  State<AnimatedDots> createState() => _AnimatedDotsState();
}

class _AnimatedDotsState extends State<AnimatedDots>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _animation;
  int _currentIndex = 0;

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

    _animationController = AnimationController(
      vsync: this,
      duration: widget.duration,
    );

    _animation = Tween<double>(
      begin: 0,
      end: 1,
    ).animate(_animationController);

    _animationController.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        _currentIndex++;
        if (_currentIndex >= widget.path.length - 1) {
          _currentIndex = 0;
        }

        _animationController.reset();
        _animationController.forward();
      }
    });

    _animationController.forward();
  }

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

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _animation,
      builder: (context, child) {
        final value = _animation.value;
        final current = widget.path[_currentIndex];
        final next = widget.path[(_currentIndex + 1) % widget.path.length];

        final dots = List.generate(widget.dotCount, (index) {
          final dotValue = (value + index / widget.dotCount) % 1;
          final x = current.dx + (next.dx - current.dx) * dotValue;
          final y = current.dy + (next.dy - current.dy) * dotValue;

          return Positioned(
            left: x - widget.dotSize / 2,
            top: y - widget.dotSize / 2,
            child: Container(
              width: widget.dotSize,
              height: widget.dotSize,
              decoration: BoxDecoration(
                color: widget.dotColor,
                shape: BoxShape.circle,
              ),
            ),
          );
        });

        return Stack(
          children: dots,
        );
      },
    );
  }
}

使用

body: Center(
        child: Container(
          color: Colors.black,
          width: 150,
          height: 150,
          child: const ElectricAnimated(
            duration: 3,
            path: [
              Offset(30, 0),
              Offset(30, 67),
              Offset(90, 67),
              Offset(90, 137),
            ],
          ),
        ),
      ),

简书地址

相关推荐

  1. Flutter动画实现

    2023-12-10 08:58:03       46 阅读
  2. 动画js动画css3区别?

    2023-12-10 08:58:03       28 阅读
  3. 一次消谐器适用各种电压等级电力系统

    2023-12-10 08:58:03       28 阅读
  4. 在vim中映射类似Windows编辑器快捷键

    2023-12-10 08:58:03       57 阅读
  5. sparkjdbc接口,类似hiveserver2

    2023-12-10 08:58:03       51 阅读
  6. Python实现类似Qt信号、槽

    2023-12-10 08:58:03       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-10 08:58:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-10 08:58:03       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-10 08:58:03       82 阅读
  4. Python语言-面向对象

    2023-12-10 08:58:03       91 阅读

热门阅读

  1. wordpress小记

    2023-12-10 08:58:03       53 阅读
  2. spring 单元测试 Junit

    2023-12-10 08:58:03       54 阅读
  3. Spring Boot与Redis的整合

    2023-12-10 08:58:03       46 阅读
  4. d8week17

    d8week17

    2023-12-10 08:58:03      66 阅读
  5. centos7部署docker环境

    2023-12-10 08:58:03       56 阅读
  6. CentOS 7.9--离线安装python3.9.18+virtualenv-20.25.0

    2023-12-10 08:58:03       52 阅读
  7. mysql error: #1093

    2023-12-10 08:58:03       58 阅读
  8. mybatis-config.xml的配置

    2023-12-10 08:58:03       46 阅读
  9. css顺时针旋转90°再3D中绕Y轴旋转180°

    2023-12-10 08:58:03       67 阅读
  10. ElasticSearch之cat nodes API

    2023-12-10 08:58:03       58 阅读
  11. 总结:服务器批量处理http请求的大致流程

    2023-12-10 08:58:03       50 阅读
  12. 【Docker管理工具】Rancher容器管理平台的基本使用

    2023-12-10 08:58:03       60 阅读