Flutter Progress Bar

 

Flutter Progress Bar

A progress bar is a graphical control element used to show the progress of a task such as downloading, uploading, installation, file transfer, etc. In this section, we are going to understand how to show a progress bar in a flutter application.

Flutter can display the progress bar with the help of two widgets, which are given below:

  1. LinearProgressIndicator
  2. CircularProgressIndicator

Let us understand it in detail.

LinearProgressIndicator

The linear progress bar is used to show the progress of the task in a horizontal line.

Flutter provides mainly two types of linear progress indicators:

Determinate: Determinate progress bar indicates the actual amount of progress at each point in making the task. Its value will increase monotonically from 0.0 to 1.0 to show the amount of task completed at that time. We need to use a non-null value from 0.0 to 1.0 for creating a determinate progress indicator.

Indeterminate: Indeterminate progress bar does not indicate the amount of progress in completing the task. It means we do not know when the task is finished. It makes progress without indicating how much progress remains. We can make an indeterminate progress indicator by using a null value.

Properties

The following are the most common attributes of linear progress indicator:

double value: It is used to specify the non-null value between 0.0 to 1.0, representing the completion of task progress.

Color backgroundColor: It is used to specify the background color of the widget.

Animation<Color> valueColor: It is used to specify the progress indicator's color as an animated value.

Example

The following code explains the use of an indeterminate linear progress bar that shows a download where we do not know when it will be finished. A floating button is used to change the state from not downloading to downloading. When there is no downloading, it shows a text; otherwise, it will show the progress indicator:

  1. import 'package:flutter/material.dart';  
  2.   
  3. void main() => runApp(MyApp());  
  4.   
  5. class MyApp extends StatelessWidget {  
  6.   @override  
  7.   Widget build(BuildContext context) {  
  8.     return MaterialApp(  
  9.       home: LinearProgressIndicatorApp(),  
  10.     );  
  11.   }  
  12. }  
  13.   
  14. class LinearProgressIndicatorApp extends StatefulWidget {  
  15.   @override  
  16.   State<StatefulWidget> createState() {  
  17.     return LinearProgressIndicatorAppState();  
  18.   }  
  19. }  
  20.   
  21. class LinearProgressIndicatorAppState extends State<LinearProgressIndicatorApp> {  
  22.   bool _loading;  
  23.   
  24.   @override  
  25.   void initState() {  
  26.     super.initState();  
  27.     _loading = false;  
  28.   }  
  29.   
  30.   @override  
  31.   Widget build(BuildContext context) {  
  32.     return Scaffold(  
  33.       appBar: AppBar(  
  34.         title: Text("Flutter Linear Progress Bar"),  
  35.       ),  
  36.       body: Center(  
  37.         child: Container(  
  38.           padding: EdgeInsets.all(12.0),  
  39.           child: _loading ? LinearProgressIndicator() : Text(  
  40.               "Press button for downloading",  
  41.               style: TextStyle(fontSize: 25)),  
  42.         ),  
  43.       ),  
  44.       floatingActionButton: FloatingActionButton(  
  45.         onPressed: () {  
  46.           setState(() {  
  47.             _loading = !_loading;  
  48.           });  
  49.         },  
  50.         tooltip: 'Download',  
  51.         child: Icon(Icons.cloud_download),  
  52.       ),  
  53.     );  
  54.   }  
  55. }  

Output:

Now, run the app in your IDE. We can see the UI of the screen as below screenshot.

Flutter Progress Bar

When we press the floating button, it changes the state from not downloading to downloading and shows the progress indicator like the below screenshot:

Flutter Progress Bar

Sometimes we want to make a determinate progress bar that means we will show how long it will take time to finish the task. In that case, we can simulate a download that will take time to finish the task and updates the value of LinearProgressIndicator as follows:

  1. import 'dart:async';  
  2. import 'package:flutter/material.dart';  
  3.   
  4. void main() => runApp(MyApp());  
  5.   
  6. class MyApp extends StatelessWidget {  
  7.   @override  
  8.   Widget build(BuildContext context) {  
  9.     return MaterialApp(  
  10.       home: LinearProgressIndicatorApp(),  
  11.     );  
  12.   }  
  13. }  
  14.   
  15. class LinearProgressIndicatorApp extends StatefulWidget {  
  16.   @override  
  17.   State<StatefulWidget> createState() {  
  18.     return LinearProgressIndicatorAppState();  
  19.   }  
  20. }  
  21.   
  22. class LinearProgressIndicatorAppState extends State<LinearProgressIndicatorApp> {  
  23.   bool _loading;  
  24.   double _progressValue;  
  25.   
  26.   @override  
  27.   void initState() {  
  28.     super.initState();  
  29.     _loading = false;  
  30.     _progressValue = 0.0;  
  31.   }  
  32.   @override  
  33.   Widget build(BuildContext context) {  
  34.     return Scaffold(  
  35.       appBar: AppBar(  
  36.         title: Text("Flutter Linear Progress Bar"),  
  37.       ),  
  38.       body: Center(  
  39.         child: Container(  
  40.           padding: EdgeInsets.all(12.0),  
  41.           child: _loading  
  42.               ? Column(  
  43.             mainAxisAlignment: MainAxisAlignment.center,  
  44.             children: <Widget>[  
  45.               LinearProgressIndicator(  
  46.                 backgroundColor: Colors.cyanAccent,  
  47.                 valueColor: new AlwaysStoppedAnimation<Color>(Colors.red),  
  48.                 value: _progressValue,  
  49.               ),  
  50.               Text('${(_progressValue * 100).round()}%'),  
  51.             ],  
  52.           )  
  53.               : Text("Press button for downloading", style: TextStyle(fontSize: 25)),  
  54.         ),  
  55.       ),  
  56.       floatingActionButton: FloatingActionButton(  
  57.         onPressed: () {  
  58.           setState(() {  
  59.             _loading = !_loading;  
  60.             _updateProgress();  
  61.           });  
  62.         },  
  63.         tooltip: 'Download',  
  64.         child: Icon(Icons.cloud_download),  
  65.       ),  
  66.     );  
  67.   }  
  68.   // this function updates the progress value  
  69.   void _updateProgress() {  
  70.     const oneSec = const Duration(seconds: 1);  
  71.     new Timer.periodic(oneSec, (Timer t) {  
  72.       setState(() {  
  73.         _progressValue += 0.1;  
  74.         // we "finish" downloading here  
  75.         if (_progressValue.toStringAsFixed(1) == '1.0') {  
  76.           _loading = false;  
  77.           t.cancel();  
  78.           return;  
  79.         }  
  80.       });  
  81.     });  
  82.   }  
  83. }  

Output:

Now, run the app in your IDE. When we press the button, it changes the state from not downloading to downloading and shows how much progress is finished at that time like the below screenshot:

Flutter Progress Bar

CircularProgressIndicator

It is a widget, which spins to indicate the waiting process in your application. It shows the progress of a task in a circular shape. It also displays the progress bar in two ways: Determinate and Indeterminate.

determinate progress bar is used when we want to show the progress of ongoing tasks such as the percentage of downloading or uploading files, etc. We can show the progress by specifying the value between 0.0 and 1.0.

An indeterminate progress bar is used when we do not want to know the percentage of an ongoing process. By default, CircularProgressIndicator shows the indeterminate progress bar.

Example

In the below example, we will see the circular progress indicator in an indeterminate mode that does not show any task's progress. It displays the circles continuously, which indicates that something is being worked out, and we have to wait for its completion. For this, there is no need to specify any value to the CircularProgressIndicator() constructor. See the following code:

  1. import 'package:flutter/material.dart';  
  2.   
  3. void main() => runApp(MyApp());  
  4.   
  5. class MyApp extends StatelessWidget {  
  6.   @override  
  7.   Widget build(BuildContext context) {  
  8.     return MaterialApp(  
  9.       home: Scaffold(  
  10.         appBar: AppBar(  
  11.           title: Text('Flutter Progress Bar Example'),  
  12.         ),  
  13.         body: Center(  
  14.             child: CircularProgressIndicatorApp()  
  15.         ),  
  16.       ),  
  17.     );  
  18.   }  
  19. }  
  20.   
  21. /// This is the stateless widget that the main application instantiates.  
  22. class CircularProgressIndicatorApp extends StatelessWidget {  
  23.   @override  
  24.   Widget build(BuildContext context) {  
  25.     return CircularProgressIndicator(  
  26.       backgroundColor: Colors.red,  
  27.       strokeWidth: 8,);  
  28.   }  
  29. }  

Output:

Now, run the app in your IDE. We will see the output of the flutter circular progress indicator like the below screenshot:

Flutter Progress Bar

Sometimes you want to make a determinate circular progress bar to show how much it will take time to finish the task. In that case, we can simulate a download that will take time to finish the task and updates the value of CircularProgressIndicator as follows:

  1. import 'dart:async';  
  2. import 'package:flutter/material.dart';  
  3.   
  4. void main() => runApp(MyApp());  
  5.   
  6. class MyApp extends StatelessWidget {  
  7.   @override  
  8.   Widget build(BuildContext context) {  
  9.     return MaterialApp(  
  10.       home: CircularProgressIndicatorApp(),  
  11.     );  
  12.   }  
  13. }  
  14.   
  15. class CircularProgressIndicatorApp extends StatefulWidget {  
  16.   @override  
  17.   State<StatefulWidget> createState() {  
  18.     return CircularProgressIndicatorAppState();  
  19.   }  
  20. }  
  21.   
  22. class CircularProgressIndicatorAppState extends State<CircularProgressIndicatorApp>{  
  23.   bool _loading;  
  24.   double _progressValue;  
  25.   
  26.   @override  
  27.   void initState() {  
  28.     super.initState();  
  29.     _loading = false;  
  30.     _progressValue = 0.0;  
  31.   }  
  32.   @override  
  33.   Widget build(BuildContext context) {  
  34.     return Scaffold(  
  35.       appBar: AppBar(  
  36.         title: Text("Flutter Circular Progress Bar"),  
  37.       ),  
  38.       body: Center(  
  39.         child: Container(  
  40.           padding: EdgeInsets.all(14.0),  
  41.           child: _loading  
  42.               ? Column(  
  43.             mainAxisAlignment: MainAxisAlignment.center,  
  44.             children: <Widget>[  
  45.               CircularProgressIndicator(  
  46.                 strokeWidth: 10,  
  47.                 backgroundColor: Colors.yellow,  
  48.                 valueColor: new AlwaysStoppedAnimation<Color>(Colors.red),  
  49.                 value: _progressValue,  
  50.               ),  
  51.               Text('${(_progressValue * 100).round()}%'),  
  52.             ],  
  53.           )  
  54.               : Text("Press button for downloading", style: TextStyle(fontSize: 25)),  
  55.         ),  
  56.       ),  
  57.       floatingActionButton: FloatingActionButton(  
  58.         onPressed: () {  
  59.           setState(() {  
  60.             _loading = !_loading;  
  61.             _updateProgress();  
  62.           });  
  63.         },  
  64.         child: Icon(Icons.cloud_download),  
  65.       ),  
  66.     );  
  67.   }  
  68.   // this function updates the progress value  
  69.   void _updateProgress() {  
  70.     const oneSec = const Duration(seconds: 1);  
  71.     new Timer.periodic(oneSec, (Timer t) {  
  72.       setState(() {  
  73.         _progressValue += 0.2;  
  74.         // we "finish" downloading here  
  75.         if (_progressValue.toStringAsFixed(1) == '1.0') {  
  76.           _loading = false;  
  77.           t.cancel();  
  78.           return;  
  79.         }  
  80.       });  
  81.     });  
  82.   }  
  83. }  

Output:

Now, run the app in your IDE. When we press the button, it shows how much progress is finished at that time like the below screenshot:

Flutter Progress Bar


Post a Comment

Previous Post Next Post