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

實(shí)現(xiàn)步驟
1、pubspec.yaml中添加依賴 flustars,該包的TimelineUtil和TimerUtil類可以實(shí)現(xiàn)計(jì)時(shí)功能
dependencies: ? flustars: ^0.3.3
!注意空格哦
2、代碼實(shí)現(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(() {
? ? ? ? //每次時(shí)間間隔回調(diào),把每次當(dāng)前總時(shí)間ms除以1000就是秒
? ? ? ? current_time = millisUntilFinished / 1000;
? ? ? ? //倒計(jì)時(shí)結(jié)束時(shí) 跳轉(zhuǎn)到首頁(yè) 當(dāng)然也可以等待資源加載完成再跳轉(zhuǎn)
? ? ? ? if (current_time == 0) {
? ? ? ? ? /*等待資源完成代碼塊*/
? ? ? ? ? //跳轉(zhuǎn)到首頁(yè)
? ? ? ? ? Navigator.push(
? ? ? ? ? ? ? context, MaterialPageRoute(builder: (context) => HomePage()));
? ? ? ? }
? ? ? });
? ? });構(gòu)造頁(yè)面
?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; //計(jì)時(shí)對(duì)象
? double current_time = 0; //當(dāng)前時(shí)間
? @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(() {
? ? ? ? //每次時(shí)間間隔回調(diào),把每次當(dāng)前總時(shí)間ms除以1000就是秒
? ? ? ? current_time = millisUntilFinished / 1000;
? ? ? ? //倒計(jì)時(shí)結(jié)束時(shí) 跳轉(zhuǎn)到首頁(yè) 當(dāng)然也可以等待資源加載完成再跳轉(zhuǎn)
? ? ? ? if (current_time == 0) {
? ? ? ? ? /*等待資源完成代碼塊*/
? ? ? ? ? //跳轉(zhuǎn)到首頁(yè)
? ? ? ? ? Navigator.push(
? ? ? ? ? ? ? context, MaterialPageRoute(builder: (context) => HomePage()));
? ? ? ? }
? ? ? });
? ? });
? ? //開始倒計(jì)時(shí)
? ? 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'),
? ? ? ),
? ? );
? }
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android實(shí)現(xiàn)獲取有線和無線Ip地址的方法
這篇文章主要介紹了android實(shí)現(xiàn)獲取有線和無線Ip地址的方法,較為詳細(xì)的分析了Android獲取IP地址的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
解決flutter 錯(cuò)誤: 程序包androidx.lifecycle不存在問題
這篇文章主要介紹了解決flutter 錯(cuò)誤: 程序包androidx.lifecycle不存在問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Android 實(shí)現(xiàn)電話攔截及攔截提示音功能的開發(fā)
本文主要介紹Android 實(shí)現(xiàn)電話攔截和攔截提示音功能的開發(fā),這里提供實(shí)現(xiàn)代碼和詳細(xì)講解,有需要的小伙伴可以參考下2016-08-08
Android自定義View實(shí)現(xiàn)可拖拽縮放的矩形框
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)可拖拽縮放的矩形框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
Android Studio通過Artifactory搭建本地倉(cāng)庫(kù)優(yōu)化編譯速度的方法
這篇文章主要介紹了Android Studio通過Artifactory搭建本地倉(cāng)庫(kù)優(yōu)化編譯速度的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
AndroidStudio實(shí)現(xiàn)微信界面設(shè)計(jì)
這篇文章帶你通過Androidstudio來實(shí)現(xiàn)微信的基礎(chǔ)界面,微信的界面主要包含了主頁(yè)、通訊錄、發(fā)現(xiàn)以及我的賬號(hào)功能區(qū),下文包含了整個(gè)開發(fā)過程,以及解決該問題的過程及思路并提供了源碼2021-10-10
Android Drawerlayout實(shí)現(xiàn)側(cè)滑菜單效果
這篇文章主要為大家詳細(xì)介紹了Android Drawerlayout實(shí)現(xiàn)側(cè)滑菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android基于TextView屬性android:ellipsize實(shí)現(xiàn)跑馬燈效果的方法
這篇文章主要介紹了Android基于TextView屬性android:ellipsize實(shí)現(xiàn)跑馬燈效果的方法,涉及Android跑馬燈效果所涉及的TextView相關(guān)屬性與使用方法,需要的朋友可以參考下2016-08-08

