flutter實現(xiàn)倒計時加載頁面
本文實例為大家分享了flutter實現(xiàn)倒計時加載頁面的具體代碼,供大家參考,具體內(nèi)容如下
效果圖

實現(xiàn)步驟
1、pubspec.yaml中添加依賴 flustars,該包的TimelineUtil和TimerUtil類可以實現(xiàn)計時功能
dependencies: ? flustars: ^0.3.3
!注意空格哦
2、代碼實現(xiàn)
初始化TimerUtil
late TimerUtil util; ?
double current_time = 0;
void initState() {
? ? super.initState();
? ? util = new TimerUtil(mInterval: 18, mTotalTime: 5000);
? ? util.setOnTimerTickCallback((millisUntilFinished) {
? ? ? setState(() {
? ? ? ? //每次時間間隔回調(diào),把每次當(dāng)前總時間ms除以1000就是秒
? ? ? ? current_time = millisUntilFinished / 1000;
? ? ? ? //倒計時結(jié)束時 跳轉(zhuǎn)到首頁 當(dāng)然也可以等待資源加載完成再跳轉(zhuǎn)
? ? ? ? if (current_time == 0) {
? ? ? ? ? /*等待資源完成代碼塊*/
? ? ? ? ? //跳轉(zhuǎn)到首頁
? ? ? ? ? Navigator.push(
? ? ? ? ? ? ? context, MaterialPageRoute(builder: (context) => HomePage()));
? ? ? ? }
? ? ? });
? ? });構(gòu)造頁面
?Widget build(BuildContext context) {
? ? return Scaffold(
? ? ? ? body: Column(
? ? ? children: [
? ? ? ? Image.asset('images/2.0/beijing.jpg'),
? ? ? ? Container(
? ? ? ? ? alignment: Alignment.centerRight,
? ? ? ? ? child: SizedBox(
? ? ? ? ? ? ? height: 50,
? ? ? ? ? ? ? width: 50,
? ? ? ? ? ? ? child: Stack(
? ? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? ? Center(child: CircularProgressIndicator(
? ? ? ? ? ? ? ? ? ? value: current_time == 5.0 ? 0 : (5 - current_time) / 5,
? ? ? ? ? ? ? ? ? ),),
? ? ? ? ? ? ? ? ? Center(child: Text('${current_time.toInt()}'),)
? ? ? ? ? ? ? ? ],)
? ? ? ? ? ),
? ? ? ? ),
? ? ? ],
? ? ));
? }完整代碼
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
void main() {
? runApp(MyApp());
}
class MyApp extends StatelessWidget {
? @override
? Widget build(BuildContext context) {
? ? return MaterialApp(
? ? ? home: LoadingPage(),
? ? );
? }
}
class LoadingPage extends StatefulWidget {
? const LoadingPage({Key? key}) : super(key: key);
? @override
? _LoadingPageState createState() => _LoadingPageState();
} ??
class _LoadingPageState extends State<LoadingPage> {
? late TimerUtil util; //計時對象
? double current_time = 0; //當(dāng)前時間
? @override
? Widget build(BuildContext context) {
? ? return Scaffold(
? ? ? ? body: Column(
? ? ? children: [
? ? ? ? Image.asset('images/2.0/beijing.jpg'),
? ? ? ? Container(
? ? ? ? ? alignment: Alignment.centerRight,
? ? ? ? ? child: SizedBox(
? ? ? ? ? ? ? height: 50,
? ? ? ? ? ? ? width: 50,
? ? ? ? ? ? ? child: Stack(
? ? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? ? Center(child: CircularProgressIndicator(
? ? ? ? ? ? ? ? ? ? value: current_time == 5.0 ? 0 : (5 - current_time) / 5,
? ? ? ? ? ? ? ? ? ),),
? ? ? ? ? ? ? ? ? Center(child: Text('${current_time.toInt()}'),)
? ? ? ? ? ? ? ? ],)
? ? ? ? ? ),
? ? ? ? ),
? ? ? ],
? ? ));
? }
? @override
? void initState() {
? ? super.initState();
? ? util = new TimerUtil(mInterval: 18, mTotalTime: 5000);
? ? util.setOnTimerTickCallback((millisUntilFinished) {
? ? ? setState(() {
? ? ? ? //每次時間間隔回調(diào),把每次當(dāng)前總時間ms除以1000就是秒
? ? ? ? current_time = millisUntilFinished / 1000;
? ? ? ? //倒計時結(jié)束時 跳轉(zhuǎn)到首頁 當(dāng)然也可以等待資源加載完成再跳轉(zhuǎn)
? ? ? ? if (current_time == 0) {
? ? ? ? ? /*等待資源完成代碼塊*/
? ? ? ? ? //跳轉(zhuǎn)到首頁
? ? ? ? ? Navigator.push(
? ? ? ? ? ? ? context, MaterialPageRoute(builder: (context) => HomePage()));
? ? ? ? }
? ? ? });
? ? });
? ? //開始倒計時
? ? util.startCountDown();
? }
}
class HomePage extends StatelessWidget {
? const HomePage({Key? key}) : super(key: key);
? @override
? Widget build(BuildContext context) {
? ? return Scaffold(
? ? ? appBar: AppBar(
? ? ? ? title: Text('HomePage'),
? ? ? ),
? ? );
? }
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決flutter 錯誤: 程序包androidx.lifecycle不存在問題
這篇文章主要介紹了解決flutter 錯誤: 程序包androidx.lifecycle不存在問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Android 實現(xiàn)電話攔截及攔截提示音功能的開發(fā)
本文主要介紹Android 實現(xiàn)電話攔截和攔截提示音功能的開發(fā),這里提供實現(xiàn)代碼和詳細(xì)講解,有需要的小伙伴可以參考下2016-08-08
Android自定義View實現(xiàn)可拖拽縮放的矩形框
這篇文章主要為大家詳細(xì)介紹了Android自定義View實現(xiàn)可拖拽縮放的矩形框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05
Android Studio通過Artifactory搭建本地倉庫優(yōu)化編譯速度的方法
這篇文章主要介紹了Android Studio通過Artifactory搭建本地倉庫優(yōu)化編譯速度的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
AndroidStudio實現(xiàn)微信界面設(shè)計
這篇文章帶你通過Androidstudio來實現(xiàn)微信的基礎(chǔ)界面,微信的界面主要包含了主頁、通訊錄、發(fā)現(xiàn)以及我的賬號功能區(qū),下文包含了整個開發(fā)過程,以及解決該問題的過程及思路并提供了源碼2021-10-10
Android Drawerlayout實現(xiàn)側(cè)滑菜單效果
這篇文章主要為大家詳細(xì)介紹了Android Drawerlayout實現(xiàn)側(cè)滑菜單效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Android基于TextView屬性android:ellipsize實現(xiàn)跑馬燈效果的方法
這篇文章主要介紹了Android基于TextView屬性android:ellipsize實現(xiàn)跑馬燈效果的方法,涉及Android跑馬燈效果所涉及的TextView相關(guān)屬性與使用方法,需要的朋友可以參考下2016-08-08

