https://api.flutter.dev/flutter/material/LinearProgressIndicator-class.html

Material 组件库中提供了两种进度指示器:LinearProgressIndicatorCircularProgressIndicator,它们都可以同时用于精确的进度指示和模糊的进度指示。精确进度通常用于任务进度可以计算和预估的情况,比如文件下载;而模糊进度则用户任务进度无法准确获得的情况,如下拉刷新,数据提交等。

LinearProgressIndicator 水平进度条

LinearProgressIndicator是一个线性、条状的进度条。定义如下:

  1. LinearProgressIndicator({
  2. double value,
  3. Color backgroundColor,
  4. Animation<Color> valueColor,
  5. this.minHeight,
  6. String semanticsLabel,
  7. String semanticsValue,
  8. })
  • valuevalue表示当前的进度,取值范围为[0,1];如果valuenull时则指示器会执行一个循环动画(模糊进度);当value不为null时,指示器为一个具体进度的进度条。
  • backgroundColor:指示器的背景色。
  • valueColor: 指示器的进度条颜色;值得注意的是,该值类型是Animation<Color>,这允许我们对进度条的颜色也可以指定动画。如果我们不需要对进度条颜色执行动画,换言之,我们想对进度条应用一种固定的颜色,此时我们可以通过AlwaysStoppedAnimation来指定。

示例1:不设置进度值,会执行一个循环动画

fdtdttyddasdyrt.gif

  1. LinearProgressIndicator(),
  2. SizedBox(height: 10),
  3. LinearProgressIndicator(
  4. minHeight: 20,
  5. ),
  6. SizedBox(height: 10),
  7. LinearProgressIndicator(
  8. backgroundColor: Colors.green,
  9. valueColor: AlwaysStoppedAnimation(Colors.red),
  10. ),

示例2:指定进度值

image.png

  1. LinearProgressIndicator(
  2. value: 0.2,
  3. backgroundColor: Colors.green,
  4. valueColor: AlwaysStoppedAnimation<Color>(Colors.red),
  5. ),

CircularProgressIndicator 圆形进度条

CircularProgressIndicator是一个圆形进度条,定义如下:

  1. CircularProgressIndicator({
  2. double value,
  3. Color backgroundColor,
  4. Animation<Color> valueColor,
  5. this.strokeWidth = 4.0,
  6. ...
  7. })

前三个参数和LinearProgressIndicator相同,不再赘述。strokeWidth 表示圆形进度条的粗细。

示例1:不设置进度值,会执行一个循环动画

fdtdttyddasdyrt.gif

  1. CircularProgressIndicator(),
  2. SizedBox(height: 10),
  3. Container(
  4. width: 100,
  5. height: 100,
  6. child: CircularProgressIndicator(
  7. backgroundColor: Colors.green,
  8. valueColor: AlwaysStoppedAnimation<Color>(Colors.red),
  9. strokeWidth: 10,
  10. ),
  11. ),

示例2:制定进度值

image.png

  1. CircularProgressIndicator(
  2. value: 0.2,
  3. backgroundColor: Colors.grey,
  4. valueColor: AlwaysStoppedAnimation(Colors.green),
  5. ),

CupertinoActivityIndicator

ios风格的指示器,CupertinoActivityIndicator不能设置进度,只能一直转“菊花”。
进度条 - 图5

  1. CupertinoActivityIndicator(
  2. radius: 10, //radius参数是半径,值越大,控件越大。
  3. ),


自定义尺寸

我们可以发现LinearProgressIndicatorCircularProgressIndicator,并没有提供设置圆形进度条尺寸的参数;如果我们希望LinearProgressIndicator的线细一些,或者希望CircularProgressIndicator的圆大一些该怎么做?

其实LinearProgressIndicatorCircularProgressIndicator都是取父容器的尺寸作为绘制的边界的。知道了这点,我们便可以通过尺寸限制类Widget,如ConstrainedBoxSizedBox (我们将在后面容器类组件一章中介绍)来指定尺寸,如:

  1. // 线性进度条高度指定为3
  2. SizedBox(
  3. width: 100, //宽100
  4. height: 10, //高10
  5. child: LinearProgressIndicator(
  6. value: 0.2,
  7. ),
  8. ),
  9. // 圆形进度条直径指定为100
  10. SizedBox(
  11. width: 100,
  12. height: 100, //注意,如果显示空间的宽高不同,则会显示为椭圆。
  13. child: CircularProgressIndicator(
  14. value: 0.2,
  15. backgroundColor: Colors.grey,
  16. valueColor: AlwaysStoppedAnimation(Colors.green),
  17. ),
  18. ),

image.png

进度色动画

前面说过可以通过valueColor对进度条颜色做动画,关于动画我们将在后面专门的章节详细介绍,这里先给出一个例子,读者在了解了Flutter动画一章后再回过头来看。

我们实现一个进度条在3秒内从灰色变成蓝色的动画:

  1. import 'package:flutter/material.dart';
  2. class ProgressRoute extends StatefulWidget {
  3. @override
  4. _ProgressRouteState createState() => _ProgressRouteState();
  5. }
  6. class _ProgressRouteState extends State<ProgressRoute>
  7. with SingleTickerProviderStateMixin {
  8. AnimationController _animationController;
  9. @override
  10. void initState() {
  11. //动画执行时间3秒
  12. _animationController =
  13. new AnimationController(vsync: this, duration: Duration(seconds: 3));
  14. _animationController.forward();
  15. _animationController.addListener(() => setState(() => {}));
  16. super.initState();
  17. }
  18. @override
  19. void dispose() {
  20. _animationController.dispose();
  21. super.dispose();
  22. }
  23. @override
  24. Widget build(BuildContext context) {
  25. return SingleChildScrollView(
  26. child: Column(
  27. children: <Widget>[
  28. Padding(
  29. padding: EdgeInsets.all(16),
  30. child: LinearProgressIndicator(
  31. backgroundColor: Colors.grey[200],
  32. valueColor: ColorTween(begin: Colors.grey, end: Colors.blue)
  33. .animate(_animationController), // 从灰色变成蓝色
  34. value: _animationController.value,
  35. ),
  36. );
  37. ],
  38. ),
  39. );
  40. }
  41. }

自定义进度指示器样式

定制进度指示器风格样式,可以通过CustomPainter Widget 来自定义绘制逻辑,实际上LinearProgressIndicatorCircularProgressIndicator也正是通过CustomPainter来实现外观绘制的。关于CustomPainter,我们将在后面“自定义Widget”一章中详细介绍。

flutter_spinkit 包提供了多种风格的模糊进度指示器,读者若是感兴趣,可以参考。