爆闪策略1:双色交替闪

爆闪策略1:双色交替 后 双色全亮 闪五次

效果视频 # 安全行车 # 爆闪灯 # 奥迪a6l 爆闪频率逐渐升高 刚才没啥事用# flutter 写的App https://v.douyin.com/iFnLFvGF/

image.png
flutter源码 https://gist.github.com/330132662/b1fd89eab951a358991a302c51ec5417

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/**
*  红蓝爆闪
*/
class RedBlue extends StatefulWidget {
 const RedBlue({super.key});

 @override
 State<RedBlue> createState() => _RedBlueState();
}

class _RedBlueState extends State<RedBlue> {
 @override
 void initState() {
   super.initState();
 }

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

 Widget _body() {
   return Scaffold(
       body: GestureDetector(
     child: Column(
       children: [
         Container(
           height: ScreenUtil().screenHeight / 2,
           // width: MediaQuery.of(context).size.width,
           width: ScreenUtil().screenWidth,
           color: red,
         ),
         Container(
           height: ScreenUtil().screenHeight / 2,
           // width: MediaQuery.of(context).size.width,
           width: ScreenUtil().screenWidth,
           color: blue,
         ),
       ],
     ),
     onTap: _allClick,
   ));
 }

 bool _isRed = false;
 Color closeLed = Colors.yellowAccent;

 Color? red = Colors.red;
 Color? blue = Colors.blue;
 bool _isRun = false;
 // late Timer _timer;
 final int _timeDistance = 100;

 _allClick() {
   _flashBlue();
 }

/*
无限交替方案
*/
 _startFlash() {
   if (_isRed) {
     //   亮红的
     _closeBlue();
     _showRed();
   } else {
     //  亮蓝的
     _showBlue();
     _closeRed();
   }
   _isRed = !_isRed;
   setState(() {});
 }

 /**
  *  单色交替循环 次数
  */
 final int singleTimes = 2;
 int singleTimess = 0;

 /**
  *  aabbaabb abab
  *
  *  bb--rr
  */
 _flashBlue() {
   Future.delayed(Duration(milliseconds: _timeDistance), () {
     blue = Colors.blue;
     if (mounted) {
       setState(() {});
     }
     Future.delayed(Duration(milliseconds: _timeDistance), () {
       _closeBlue();

       Future.delayed(Duration(milliseconds: _timeDistance), () {
         blue = Colors.blue;
         if (mounted) {
           setState(() {});
         }
         Future.delayed(Duration(milliseconds: _timeDistance), () {
           _closeBlue();
           _flashred2();
         });
       });
     });
   });
   // 亮红的两次

   if (!_exit) {
     _isRun = false;
   } else {
     _isRun = true;
     // _exit = true;
   }
 }

 /**
  * rr -- bb
  */
 _flashred2() {
   if (!_exit) {
     Future.delayed(Duration(milliseconds: _timeDistance), () {
       red = Colors.red;
       if (mounted) {
         setState(() {});
       }
       Future.delayed(Duration(milliseconds: _timeDistance), () {
         _closeRed();
         Future.delayed(Duration(milliseconds: _timeDistance), () {
           red = Colors.red;
           if (mounted) {
             setState(() {});
           }
           Future.delayed(Duration(milliseconds: _timeDistance), () {
             _closeRed();
             singleTimess++;

             if (singleTimess < singleTimes) {
               _flashBlue();
             } else {
               singleTimess = 0; // 单词爆闪归零
               _flashrb();
             }
           });
         });
       });
     });
   }
 }

 /**
  *  红蓝一起亮
  */
 final int _recycleTimes = 6;
 int countRec = 0;

 _flashrb() {
   Future.delayed(Duration(milliseconds: _timeDistance), () {
     red = Colors.red;
     blue = Colors.blue;
     Future.delayed(Duration(milliseconds: _timeDistance), () {
       _closeRed();
       _closeBlue();
     });
     countRec++;
     print("次数 $countRec");
     if (countRec < _recycleTimes) {
       Future.delayed(Duration(milliseconds: _timeDistance), () {
         _flashrb(); //不到4 就递归
       });
     } else {
       countRec = 0;
       _flashBlue(); // 全亮4次后重新开始单色爆闪
     }
     if (mounted) {
       setState(() {});
     }
     // _flash1();
   });
 }

 _showRed() {
   Future.delayed(Duration(milliseconds: _timeDistance), () {
     red = Colors.red;
     if (mounted) {
       setState(() {});
     }
     Future.delayed(Duration(milliseconds: _timeDistance), () {
       _closeRed();
     });
   });
   if (!_isRun) {}
 }

 _closeRed() {
   // red = Color(0xFFAB130B);
   red = closeLed;
   if (mounted) {
     setState(() {});
   }
 }

 /**
  *  设为 true  停止所有
  */
 bool _exit = false;

 _showBlue() {
   Future.delayed(Duration(milliseconds: _timeDistance), () {
     blue = Colors.blue;
     if (mounted) {
       setState(() {});
     }
     Future.delayed(Duration(milliseconds: _timeDistance), () {
       _closeBlue();
       blue = Colors.blue;
     });
   });
   if (!_isRun) {}
 }
                                                                              
 _closeBlue() {
   // blue = Color(0xFF040C88);
   blue = closeLed;
   if (mounted) {
     setState(() {});
   }
 }

 @override
 void dispose() {
   _exit = true;
   print("退出 $_exit");
   super.dispose();
 }
}

潍坊老登程序员
303 声望5 粉丝

内涵段子TV