您好,登錄后才能下訂單哦!
小編給大家分享一下怎么用flutter制作上班摸魚應(yīng)用,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
網(wǎng)上最近看到了個(gè)摸魚應(yīng)用,還挺好玩的,我就自己用flutter寫了一個(gè)。
之前我有用flutter制作過mobile應(yīng)用,但是沒有在desktop嘗試過;畢竟2.0大更新,我這里就在這試手一下,并說說flutter的體驗(yàn).
當(dāng)前flutter環(huán)境 2.8
增加flutter desktop支持 (默認(rèn)項(xiàng)目之存在ios,android項(xiàng)目包)
flutter config --enable-<platform>-desktop
我這里是mac,因此platform=macos,詳細(xì)看flutter官網(wǎng)
代碼十分簡單,UI部分就不講了
在摸魚界面,我是用了 Bloc 做倒計(jì)時(shí)計(jì)算邏輯,默認(rèn)摸魚時(shí)長15分鐘
MoYuBloc() : super(MoyuInit()) { on(_handleMoyuStart); on(_handleUpdateProgress); on(_handleMoyuEnd); }
摸魚開始事件處理
// handle moyu start action FutureOr<void> _handleMoyuStart( MoyuStarted event, Emitter<MoyuState> emit) async { if (_timer != null && _timer!.isActive) { _timer?.cancel(); } final totalTime = event.time; int progressTime = state is MoyuIng ? (state as MoyuIng).progressTime : 0; _timer = Timer.periodic(const Duration(seconds: 1), (timer) { add(MoyuProgressUpdated(totalTime, ++progressTime)); if (progressTime >= totalTime) { timer.cancel(); add(MoyuEnded()); } }); emit(MoyuIng(progress: 0, progressTime: 0)); }
摸魚進(jìn)度更新
// handle clock update FutureOr<void> _handleUpdateProgress( MoyuProgressUpdated event, Emitter<MoyuState> emit) async { final totalTime = event.totalTime; final progressTime = event.progressTime; emit( MoyuIng(progress: progressTime / totalTime, progressTime: progressTime), ); }
摸魚結(jié)束,釋放結(jié)束事件
// handle clock end FutureOr<void> _handleMoyuEnd( MoyuEnded event, Emitter<MoyuState> emit) async { emit(MoyuFinish()); }
總結(jié)3個(gè)event (摸魚開始,進(jìn)程更新,摸魚結(jié)束)
abstract class MoyuEvent {} class MoyuStarted extends MoyuEvent { final int time; final System os; MoyuStarted({required this.time, required this.os}); } class MoyuProgressUpdated extends MoyuEvent { final int totalTime; final int progressTime; MoyuProgressUpdated(this.totalTime, this.progressTime); } class MoyuEnded extends MoyuEvent { MoyuEnded(); }
其中3個(gè)state (摸魚初始,正在摸魚,摸魚結(jié)束)
abstract class MoyuState {} class MoyuInit extends MoyuState {} class MoyuIng extends MoyuState { final double progress; final int progressTime; MoyuIng({required this.progress, required this.progressTime}); } class MoyuFinish extends MoyuState {}
啟動(dòng)摸魚使用, 記錄總時(shí)長和消耗時(shí)間,計(jì)算進(jìn)度百分比,更新UI進(jìn)度條
下面是界面更新邏輯
BlocConsumer<MoYuBloc, MoyuState>( builder: (context, state) { if (state is MoyuIng) { final progress = state.progress; return _moyuIngView(progress); } else if (state is MoyuFinish) { return _replayView(); } return const SizedBox(); }, listener: (context, state) {}, listenWhen: (pre, cur) => pre != cur, ),
很簡單 最重要的是進(jìn)度狀態(tài),其次結(jié)束后是否重新摸魚按鈕
構(gòu)建運(yùn)行flutter應(yīng)用
flutter run -d macos
最后結(jié)果展示
看完了這篇文章,相信你對(duì)“怎么用flutter制作上班摸魚應(yīng)用”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。