flutter 充电气泡

news2024/9/21 11:04:17

前言:

之前一直看到 有手机充电的时候 有气泡从Type-C 的位置冒泡上来 慢慢上移, 然后和上面的圆圈 会和,感觉还是挺好看的。今天试了下用 Flutter 实现了一版本。大致效果如下,而且气泡 和 气泡直接还可以粘黏

 实现原理:

大致的布局就是这样的: Stack 包裹住所有的元素,需要位置移动的是 AnimatedBuilder,这里是把他们独立出来,方便随机的时候打散处理。

代码实现:

  • PageBubble.dart 整个页面 气泡的粘连效果 有点吃性能
    import 'dart:math';
    import 'dart:ui';
    
    import 'package:flutter/material.dart';
    import 'package:untitled1a/pages/example1/bubble_dot.dart';
    
    class PageBubble extends StatefulWidget {
      const PageBubble({Key? key}) : super(key: key);
    
      @override
      State<PageBubble> createState() => _PageBubbleState();
    }
    
    class _PageBubbleState extends State<PageBubble>
        with SingleTickerProviderStateMixin {
      late AnimationController _animationController;
      final Random random = Random();
      @override
      void initState() {
        _animationController = AnimationController(
          vsync: this,
          duration: const Duration(milliseconds: 2500),
        );
        _animationController.repeat();
        super.initState();
      }
    
      @override
      void dispose() {
        _animationController.dispose();
    
        // TODO: implement dispose
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Theme.of(context).colorScheme.inversePrimary,
            title: Text('充电气泡'),
          ),
          body: Align(
            alignment: Alignment(0.0, 1),
            child: Container(
              height: MediaQuery.of(context).size.height / 3 * 2,
              width: 250,
              // color: Colors.blue,
              child: Stack(
                alignment: Alignment.topCenter,
                children: [
                  ...buildAnimatedPositioned(),
                  Padding(
                    padding: const EdgeInsets.only(
                      top: 40,
                      left: 0,
                    ),
                    child: RotationTransition(
                      alignment: Alignment.center,
                      turns: _animationController,
                      child: Container(
                        width: 200,
                        height: 200,
                        decoration: const BoxDecoration(
                          color: Colors.deepPurple,
                          borderRadius: BorderRadius.only(
                            topRight: Radius.circular(70),
                            topLeft: Radius.circular(90),
                            bottomRight: Radius.circular(60),
                            bottomLeft: Radius.circular(80),
                          ),
                        ),
                      ),
                    ),
                  ),
                  //这个效果很有意思 就是有费性能  不需要可以移除掉
                  BackdropFilter(
                    filter: ImageFilter.dilate(radiusX: 3.0, radiusY: 3.0),
                    child: Container(),
                  ),
                  Positioned(
                    left: 35,
                    top: 45,
                    child: Container(
                      width: 180,
                      height: 180,
                      decoration: BoxDecoration(
                        color: Colors.black,
                        borderRadius: BorderRadius.circular(90),
                      ),
                      child: const Center(
                        child: Text(
                          '89%',
                          style: TextStyle(
                            fontSize: 40,
                            color: Colors.white,
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      }
    
      int getRandomNumber(int min, int max) {
        var random = Random();
        return min + random.nextInt(max - min + 1);
      }
    
      List<Widget> buildAnimatedPositioned() {
        List<Widget> _list = [];
        List.generate(
            9,
            (index) => {
                  _list.add(BubbleDot(time: getRandomNumber(2000, 3500))),
                });
    
        return _list;
      }
    }
    

  • BubbleDot.dart    气泡效果    之所以把气泡单独出来是为了后面的随机打散操作
    import 'dart:math';
    import 'package:flutter/material.dart';
    
    class BubbleDot extends StatefulWidget {
      final int time;
      const BubbleDot({super.key, required this.time});
    
      @override
      State<BubbleDot> createState() => _BubbleDotState();
    }
    
    class _BubbleDotState extends State<BubbleDot>
        with SingleTickerProviderStateMixin {
      late AnimationController _animationController;
      late Animation<double> _animation;
      final Random random = Random();
      late double _leftPos = 0;
      late double _dotWidth = 0;
      @override
      void initState() {
        _animationController = AnimationController(
          vsync: this,
          duration: Duration(milliseconds: widget.time),
        );
        _animation = _animationController.drive(
            Tween<double>(begin: getRandomNumber(660, 800).toDouble(), end: 100));
        //_leftPos = random.nextDouble() * 200;
        _leftPos = getRandomNumber(35, 180).toDouble();
        _dotWidth = getRandomNumber(30, 66).toDouble();
    
        _animationController.addStatusListener(
            (AnimationStatus status) => {print('status  $status')});
        _animationController.repeat();
        // TODO: implement initState
        super.initState();
      }
    
      @override
      void dispose() {
        // TODO: implement dispose
        _animationController.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return AnimatedBuilder(
          animation: _animation,
          builder: (BuildContext context, Widget? child) {
            return Positioned(
              top: _animation.value,
              //top: 240,
              left: _leftPos,
              child: ClipOval(
                child: Container(
                  width: _dotWidth,
                  height: _dotWidth,
                  decoration: BoxDecoration(
                    color: Colors.deepPurple,
                    boxShadow: [
                      BoxShadow(
                        color: Colors.deepPurple.withOpacity(0.5),
                        spreadRadius: 6,
                        blurRadius: 8,
                        offset: Offset(4, 4), // changes position of shadow
                      ),
                    ],
                  ),
                ),
              ),
            );
          },
        );
      }
    
      int getRandomNumber(int min, int max) {
        return min + random.nextInt(max - min + 1);
      }
    }
    

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1939130.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

U盘数据误删如何恢复?

问&#xff1a;U盘数据误删如何恢复&#xff1f; 答&#xff1a; 1.若出现U盘中的文件被误删后第一步&#xff1a;不进行任何写入操作&#xff08;向U盘中复制或移动文件&#xff09;&#xff0c; 接着直接拔掉U盘。 2.在电脑上下载数据恢复软件&#xff0c;给大家推荐一个数据…

stm32学习:(寄存器2)GPIO总体说明

目录 GPIO的主要特点 GPIO的8种工作模式 GPIO电路结构 GPIO输出模式 输出流程 复用输出模式 GPIO输入模式 输入流程 模拟输入流程 GPIO相关的7个寄存器 GPIOx_CRL GPIOx_CRH GPIOx_IDR GPIOx_ODR GPIOx_BSRR GPIOx_BRR GPIOx_LCKR 实例 三个灯流水灯 main.…

0305-0308TTL逻辑门电路

逻辑门电路-逻辑描述中的问题 3.5TTL逻辑门电路3.5.1BJT的开关特性BJT的开关时间 3.5.2TTL反相器的基本电路1.电路组成2.TTL反相器的工作原理 3.5.3改进型TTL门电路-抗饱和TTL门电路1.抗饱和TTL门电路2.其他TTL门电路 3.7逻辑描述中的几个问题3.7.1正负逻辑问题3.7.2 基本逻辑门…

VirtualBox安装增强功能时报错:未能加载虚拟光盘VBoxGuestAdditions.iso 到虚拟电脑

VirtualBox安装增强功能时报错&#xff1a;未能加载虚拟光盘VBoxGuestAdditions.iso 到虚拟电脑 选中一个虚拟机 进入设置点击存储 再点击光盘样子的加号进入后就可以选择 添加 选中双击就好&#xff0c;我已经添加过了 所以我的会在上面 选中一个虚拟机 进入设置 点击存储 再点…

基于单片机的智能窗帘系统设计

【 摘 要 】 随着物联网技术的发展,智能家居越来越受到业界的关注,针对目前市场上智能窗帘的弊端,设计了一款基于单片机的智能窗帘 。 普遍窗帘需要手工进行控制,遥控窗帘通常需要远程控制,智能窗帘与之相比,可以实现自主控制。 系统前端探测器采用光敏传感器,对光线进行…

小程序中视频课程下载工具使用教程,一键下载小程序中的视频课程!

前言&#xff1a; 现代随着知识付费时代的发展&#xff0c;我们很多视频课程或需要学习的资料是保存在小程序当中的&#xff0c;但是现在很多小程序它是禁止让我们下载我们所观看到的视频课程的&#xff0c;这就给我们带来了很多不方便。今天我就教大家如何一键下载小程序中的…

python实现图像对比度增强算法

python实现直方图均衡化、自适应直方图均衡化、连接组件标记算法 1.直方图均衡化算法详解算法步骤公式Python 实现详细解释优缺点 2.自适应直方图均衡化算法详解算法步骤公式Python 实现详细解释优缺点 3.连接组件标记算法详解算法步骤8连通与4连通公式Python 实现详细解释优缺…

Qt 使用Installer Framework制作安装包

Qt 使用Installer Framework制作安装包 引言一、下载安装 Qt Installer Framework二、简单使用2.1 创建目录结构 (文件夹结构)2.2 制作程序压缩包2.3 制作程序安装包 引言 Qt Installer Framework (安装程序框架)是一个强大的工具集&#xff0c;用于创建自定义的在线和离线安装…

DP-适配器模式代码重新理解

package com.designpatterns.adapter;/*** 定义鸭子接口*/ public interface Duck {/*** 定义鸭子呱呱叫(quack)*/public void quack();public void fly(); }package com.designpatterns.adapter;/*** 实现一个绿头鸭*/ public class MallarDuck implements Duck{Overridepubl…

德国汉堡大学、清华大学联合英国布里斯托机器人实验室的研究工作分享:基于视觉遥操作的多指机械手灵巧操作

德国汉堡大学&#xff08;张建伟院士团队&#xff09;、清华大学&#xff08;孙富春教授和方斌&#xff09;联合英国布里斯托机器人实验室等单位在基于视觉信息遥操作的多指机械手灵巧操作研究方面取得进展。该工作得到了德国科学基金会&#xff08;DFG&#xff09;与中国国家自…

C++合作开发项目:美术馆1.0

快乐星空MakerZINCFFO 合作入口&#xff1a;CM工作室 效果图&#xff1a; 代码&#xff1a; &#xff08;还有几个音乐&#xff01;&#xff09; main.cpp #include <bits/stdc.h> #include <windows.h> #include <conio.h> #include <time.h> #in…

E16.【C语言】练习:输入一个正的整数,逆序打印这个整数的每一位

输入一个正的整数&#xff0c;逆序打印这个整数的每一位 输入&#xff1a;123 输出&#xff1a;321 步骤&#xff1a;1.取出整数的每一位 2.倒序打印 分析&#xff1a;123%103-->123/1012(3被去除了)-->12%102-->12/101(2被去除了)-->1%101(取出最后一位)->…

Michael.W基于Foundry精读Openzeppelin第66期——ProxyAdmin.sol

Michael.W基于Foundry精读Openzeppelin第66期——ProxyAdmin.sol 0. 版本0.1 ProxyAdmin.sol 1. 目标合约2. 代码精读2.1 getProxyImplementation(ITransparentUpgradeableProxy proxy)2.2 getProxyAdmin(ITransparentUpgradeableProxy proxy) && changeProxyAdmin(ITr…

【计算机网络】0 课程主要内容(自顶向下方法,中科大郑烇、杨坚)(待)

1 教学目标 掌握计算机网络 基本概念 工作原理 常用技术 为将来学习、应用和研究计算机网络打下坚实基础 2 课程主要内容 1 计算机网络和互联网2 应用层3 传输层4 网络层&#xff1a;数据平面5 网络层&#xff1a;控制平面6 数据链路层和局域网7 网络安全8 无线和移动网络9 多…

C2W1.LAB.Vocabulary Creation+Candidates from String Edits

理论课&#xff1a;C2W1.Auto-correct 文章目录 Vocabulary CreationImports and DataPreprocessingCreate Vocabulary法1.集合法法2.词典加词频法Visualization Ungraded Exercise Candidates from String EditsImports and DataSplitsDelete Edit Ungraded Exercise 理论课&…

Linux_实现线程池

目录 1、线程池的实现逻辑 2、创建多线程 3、对线程池分配任务 3.1 任务类 3.2 发送与接收任务 结语 前言&#xff1a; 在Linux下实现一个线程池&#xff0c;线程池就是创建多个线程&#xff0c;然后对这些线程进行管理&#xff0c;并且可以发放任务给到线程池…

【springboot】中使用--WebMvcConfigurer

WebMvcConfigurer 一、页面跳转控制器step1:创建视图&#xff0c;resources/templates/index.htmlstep2:创建SpringMVC配置类step3:测试功能 二、数据格式化step1:创建 DeviceInfo 数据类step2&#xff1a;自定义 Formatterstep3: 登记自定义的 DeviceFormatterstep4: 新建 Con…

杭州外贸网站建设 最好用wordpress模板来搭建

防护服wordpress外贸网站模板 消防服、防尘服、隔热服、防化服、防静电服、电焊服wordpress外贸网站模板。 https://www.jianzhanpress.com/?p4116 工业品wordpress外贸网站模板 机械及行业设备、五金工具、安全防护、包装、钢铁、纺织皮革等工业品wordpress外贸网站模板。…

实现高效离职管理,智慧校园人事管理功能全解析

智慧校园人事管理系统中的离职管理功能&#xff0c;为教职工提供了一个高效、透明且合规的离职流程&#xff0c;同时为学校管理层提供了优化人力资源配置的有力工具。教职工可以在线轻松提交离职申请&#xff0c;系统随即自动记录并启动后续流程&#xff0c;从申请审核到工作交…

C语言 | Leetcode C语言题解之第241题为运算表达式设计优先级

题目&#xff1a; 题解&#xff1a; #define ADDITION -1 #define SUBTRACTION -2 #define MULTIPLICATION -3int* diffWaysToCompute(char * expression, int* returnSize) {int len strlen(expression);int *ops (int *)malloc(sizeof(int) * len);int opsSize 0;for (in…