How to realize the countdown to load pages with flutter
This article mainly explains "how to realize the countdown loading page in flutter". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "flutter how to achieve countdown loading page" it!
Effect picture
Implementation steps
1. Add dependency flustars to pubspec.yaml. The TimelineUtil and TimerUtil classes of the package can implement the timing function.
Dependencies: flustars: ^ 0.3.3
! Pay attention to the spaces.
2. Code implementation
Initialize TimerUtil
Late TimerUtil util; double current_time = 0 void initState () {super.initState (); util = new TimerUtil (mInterval: 18, mTotalTime: 5000); util.setOnTimerTickCallback ((millisUntilFinished) {setState () {/ / callback for each interval, dividing each current total time ms by 1000 is the second current_time = millisUntilFinished / 1000 / / Jump to the home page at the end of the countdown, of course, you can also wait for the resource to load and then jump to if (current_time = = 0) {/ * wait for the resource to complete the code block * / / jump to the home page Navigator.push (context, MaterialPageRoute (builder: (context) = > HomePage ());}});})
Construct a page
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.00: (5-current_time) / 5,),), Center (child: Text ('${current_time.toInt ()}'),)] ),),],) }
Complete code
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 {late TimerUtil util; / / timing object double current_time = 0 / / current time @ 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.00: (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 () {/ / callback for each interval, dividing the current total time ms by 1000 is the second current_time = millisUntilFinished / 1000 / / Jump to the home page at the end of the countdown, of course, you can also wait for the resource to load and then jump to if (current_time = = 0) {/ * wait for the resource to complete the code block * / / jump to the home page Navigator.push (context, MaterialPageRoute (builder: (context) = > HomePage ());}});}) / / start the countdown to 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'),),);}} at this point, I believe you have a better understanding of "how to realize the countdown to loading pages in flutter". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!