flutter倒计时加载页面

本文介绍如何使用Flustars库中的TimelineUtil和TimerUtil类实现页面倒计时加载效果,包括初始化计时器、构建页面结构以及关键代码片段。通过简单的步骤和示例,帮助开发者快速上手动态加载功能。
摘要由CSDN通过智能技术生成

实现倒计时加载页面

效果图

在这里插入图片描述

实现步骤

  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'),
          ),
        );
      }
    }
    
Flutter是谷歌开发的移动UI框架,可以快速在iOS和Android上构建高质量的原生用户界面。在Flutter中实现倒计时功能,通常会使用StatefulWidget,因为倒计时涉及到界面状态的更新。 一个简单的Flutter倒计时实现可以分为以下几个步骤: 1. 创建一个StatefulWidget类,并在State类中定义倒计时所需的变量,比如剩余时间、计时器的标识等。 2. 使用`Timer`类设置一个定时器,通常使用`Timer.periodic`方法,这个方法允许定时执行任务。 3. 在定时器的回调函数中更新倒计时的时间,并调用`setState`方法通知Flutter框架更新界面。 4. 在构建UI的时候,根据剩余时间显示倒计时,并提供暂停、继续和重置等操作的按钮。 以下是一个简单的Flutter倒计时示例代码: ```dart import 'package:flutter/material.dart'; import 'dart:async'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Countdown Demo', home: CountdownPage(), ); } } class CountdownPage extends StatefulWidget { @override _CountdownPageState createState() => _CountdownPageState(); } class _CountdownPageState extends State<CountdownPage> { int _secondsRemaining = 60; // 倒计时60秒 bool _isCounting = true; late Timer _timer; void _startCountdown() { const oneSec = Duration(seconds: 1); _timer = Timer.periodic( oneSec, (Timer timer) { if (_secondsRemaining == 0) { _stopTimer(); } else { setState(() { _secondsRemaining--; }); } }, ); } void _stopTimer() { setState(() { _isCounting = false; }); _timer.cancel(); } void _reset() { setState(() { _secondsRemaining = 60; _isCounting = true; }); _startCountdown(); } @override Widget build(BuildContext context) { String displayTime = _secondsRemaining.toString(); return Scaffold( appBar: AppBar(title: Text('Flutter Countdown Timer')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( displayTime, style: Theme.of(context).textTheme.headline4, ), if (_isCounting) TextButton( onPressed: _stopTimer, child: Text("Stop"), ), if (!_isCounting) TextButton( onPressed: _reset, child: Text("Restart"), ), ], ), ), ); } } ``` 这段代码创建了一个简单的倒计时页面,包含一个倒计时数字显示和控制按钮。倒计时的时间以秒为单位,并提供了开始、停止和重置功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值