flutter实现倒计时加载页面

本文实例为大家分享了flutter实现倒计时加载页面的具体代码,供大家参考,具体内容如下

效果图

实现步骤

1、pubspec.yaml中添加依赖 flustars,该包的TimelineUtil和TimerUtil类可以实现计时功能

dependencies:
  flustars: ^0.3.3

!注意空格哦

2、代码实现

初始化TimerUtil

late TimerUtil util;  
double current_time = 0;
void initState() {
    super.initState();

    util = new TimerUtil(mInterval: 18, mTotalTime: 5000);

    util.setOnTimerTickCallback((millisUntilFinished) {
      setState(() {
        //每次时间间隔回调,把每次当前总时间ms除以1000就是秒
        current_time = millisUntilFinished / 1000;
        //倒计时结束时 跳转到首页 当然也可以等待资源加载完成再跳转
        if (current_time == 0) {
          /*等待资源完成代码块*/
          //跳转到首页
          Navigator.push(
              context, MaterialPageRoute(builder: (context) => HomePage()));
        }
      });
    });

构造页面

 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; //当前时间
  @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(() {
        //每次时间间隔回调,把每次当前总时间ms除以1000就是秒
        current_time = millisUntilFinished / 1000;
        //倒计时结束时 跳转到首页 当然也可以等待资源加载完成再跳转
        if (current_time == 0) {
          /*等待资源完成代码块*/
          //跳转到首页
          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'),
      ),
    );
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Flutter Drawer实现抽屉菜单效果

    本文实例为大家分享了Flutter Drawer实现抽屉菜单的具体代码,供大家参考,具体内容如下 import 'package:flutter/material.dart'; void main() {   runApp(MyApp()); } class MyApp extends StatelessWidget {   @override   Widget build(BuildContext context) {     return MaterialApp(       debugSh

  • Flutter倒计时/计时器的实现代码

    在我们实现某些功能时,可能会有倒计时的需求. 比如发送短信验证码,发送成功后可能要求用户一段时间内不能再次发送,这时候我们就需要进行倒计时,时间到了才允许再次操作. 如下图: 为了实现这样场景的需求,我们需要使用 Timer.periodic . 一.引入Timer对应的库 import 'dart:async'; 二.定义计时变量 class _LoginPageState extends State<LoginPage> { ... Timer _timer; int _countdown

  • Flutter定时器、倒计时的快速上手及实战讲解

    今天给大家讲讲 Flutter 里面定时器/倒计时的实现. 一般有两种场景: 我只需要你在指定时间结束后回调告诉我.回调只需要一次. 我需要你在指定时间结束后回调告诉我.回调可能多次. 下面针对这两种场景,我们来说下如何在 Flutter 里面使用. 回调一次的定时器 const timeout = const Duration(seconds: 5); print('currentTime='+DateTime.now().toString()); Timer(timeout, () { //

  • Flutter之Timer实现短信验证码获取60s倒计时功能的代码

    先看下效果: 两种需求场景: 1.广告页3s后跳转到首页 2.短信验证码60s倒计时 第一种的话,根据需求我们可以知道,我们想要的效果就是3s结束做出一个动作. factory Timer(Duration duration, void callback()) { if (Zone.current == Zone.root) { // No need to bind the callback. We know that the root's timer will // be invoked in

  • flutter实现倒计时加载页面

    本文实例为大家分享了flutter实现倒计时加载页面的具体代码,供大家参考,具体内容如下 效果图 实现步骤 1.pubspec.yaml中添加依赖 flustars,该包的TimelineUtil和TimerUtil类可以实现计时功能 dependencies:   flustars: ^0.3.3 !注意空格哦 2.代码实现 初始化TimerUtil late TimerUtil util;   double current_time = 0; void initState() {     s

  • Flutter WebView 预加载实现方法(Http Server)

    目录 背景 分析 HttpServer 接下来? 资源配置 下载解压与本地存储 版本管理与更新 获取LocalServer Url并加载Webview 兜底措施 统一管理 展示与分析 总结 Demo 背景 WebView是在APP中,可以很方便的展示web页面,并且与web交互APP的数据.方便,并且更新内容无需APP发布新版本,只需要将最新的web代码部署完成,用户重新刷新即可. 在WebView中,经常能够听到的一个需求就是:减少首次白屏时间,加快加载速度.因为加载web页面,必然会受到网络

  • jquery加载页面的方法(页面加载完成就执行)

    1.$(function(){ $("#a").click(function(){ //adding your code here }); }); 2.$(document).ready(function(){ $("#a").click(function(){ //adding your code here }); }); 3.window.onload = function(){ $("#a").click(function(){ //add

  • js实现加载页面就自动触发超链接的示例

    如下所示: 以上这篇js实现加载页面就自动触发超链接的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们.

  • AngularJS使用ng-inlude指令加载页面失败的原因与解决方法

    本文实例讲述了AngularJS使用ng-inlude指令加载页面失败的原因与解决方法.分享给大家供大家参考,具体如下: AngularJS中提供的ng-include指令,很类似于JSP中的<jsp:include>用来将多个子页面合并到同一个父页面中,避免父页面过大,可读性差,不好维护. 父页面parent.html代码如下: <html> <head> <script src="angular-1.2.2/angular.js">&

  • 用js实现计算加载页面所用的时间

    客户端代码写法: 在页面Head部分加入初始时间 复制代码 代码如下: <script language=javascript> var t1 = new Date().getTime(); </script> 在Body中加入 复制代码 代码如下: <SCRIPT LANGUAGE="JavaScript"> window.onload = function() { document.getElementById("TimeShow&qu

  • nodejs实现bigpipe异步加载页面方案

    Bigpipe介绍 Facebook首创的一种减少HTTP请求的,首屏快速加载的的异步加载页面方案.是前端性能优化的一个方向. BigPipe与AJAX的比较 AJAX主要是XMLHttpRequest,前端异步的向服务器请求,获取动态数据添加到网页上.这样的往返请求需要耗费时间,而BigPipe技术并不需要发送XMLHttpRequest请求,这样就节省时间损耗.减少请求带来的另一个好处就是直接减少服务器负载.还有一个不同点就是AJAX请求前服务器在等待.请求后页面在等待.BIGPIPE可以前

  • 使用Yii整合的pjax(pushstate+ajax)实现无刷新加载页面

    Pjax是啥? Pjax = history.pushState + Ajax = history.pushState + Async JS + XML(xhr?) BOM对象history被增强了一波,主要是对历史栈的操作,以前只有 replace , go 之类的,都会跳转并刷新整个页面,现在有了 pushState , replaceState 等等单纯操作历史栈的方法,只是单纯修改历史栈里的内容,没有副作用(页面不会跳转刷新) PJAX效果 通过url可以跟踪ajax的动态加载内容.这种

  • 使用JQuery 加载页面时调用JS的实现方法

    1,window.onload = function() {}; 2,$(document).ready(function() {}); 一.一般的加载页面时调用js方法如下: window.onload = function() { $("table tr:nth-child(even)").addClass("even"); //这个是jquery代码 }; 这段代码会在整个页面的document全部加载完成以后执行.不幸的这种方式不仅要求页面的DOM tree

  • jQuery实现的自动加载页面功能示例

    本文实例讲述了jQuery实现的自动加载页面功能.分享给大家供大家参考,具体如下: demo.html: <li style="opacity:0;-moz-opacity: 0;filter: alpha(opacity=0);"><p>---------------</p></li> <li style="opacity:0;-moz-opacity: 0;filter: alpha(opacity=0);"

随机推荐