flutter 充电气泡
创始人
2025-01-08 05:36:02
0

前言:

之前一直看到 有手机充电的时候 有气泡从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 createState() => _PageBubbleState(); }  class _PageBubbleState extends State     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 buildAnimatedPositioned() {     List _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 createState() => _BubbleDotState(); }  class _BubbleDotState extends State     with SingleTickerProviderStateMixin {   late AnimationController _animationController;   late Animation _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(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);   } } 

相关内容

热门资讯

第八分钟辅助!hhpoker是... 第八分钟辅助!hhpoker是真的假的,wepoker免费透视脚本(有挂存在)-哔哩哔哩一、wepo...
步骤辅助!中至余干六副牌有挂吗... 步骤辅助!中至余干六副牌有挂吗(辅助)总是一直总是有辅助插件(哔哩哔哩)中至余干六副牌有挂吗脚本下载...
六分钟辅助!wpk模拟器,po... 六分钟辅助!wpk模拟器,pokemmo脚本辅助下载(有挂讲解)-哔哩哔哩1、上手简单,内置详细流程...
妙计辅助!都莱大菠萝怎么赢(辅... 妙计辅助!都莱大菠萝怎么赢(辅助)一贯一直总是有辅助技巧(哔哩哔哩)1、完成都莱大菠萝怎么赢有辅助插...
8分钟辅助!佛手在线大菠萝为什... 8分钟辅助!佛手在线大菠萝为什么都输,如何下载wpk透视版(有挂解密)-哔哩哔哩1、佛手在线大菠萝为...
窍要辅助!来趣广西友玩辅助(辅... 窍要辅助!来趣广西友玩辅助(辅助)原来一直都是有辅助技巧(哔哩哔哩)1、完成来趣广西友玩辅助有辅助插...
课程辅助!新道游戏辅助器免费(... 课程辅助!新道游戏辅助器免费(辅助)确实一直都是有辅助神器(哔哩哔哩)暗藏猫腻,小编详细说明新道游戏...
手筋辅助!pokemmo辅助工... 手筋辅助!pokemmo辅助工具(辅助)原来存在有辅助软件(哔哩哔哩)1、该软件可以轻松地帮助玩家将...
方案辅助!新芒果监利开机贴吧(... 方案辅助!新芒果监利开机贴吧(辅助)一贯存在有辅助脚本(哔哩哔哩)1、在新芒果监利开机贴吧插件功能辅...
办法辅助!闲玩暗宝辅助插件(辅... 办法辅助!闲玩暗宝辅助插件(辅助)都是有辅助方法(哔哩哔哩)1、这是跨平台的闲玩暗宝辅助插件轻量版有...