前言

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

接口描述

// LinearProgressIndicator是一个线性、条状的进度条
const LinearProgressIndicator({
Key key,
// value表示当前的进度,取值范围为[0,1];如果value为null时则指示器会执行一个循环动画(模糊进度);当value不为null时,指示器为一个具体进度的进度条。
double value,
// 指示器的背景色。
Color backgroundColor,
// 指示器的进度条颜色;值得注意的是,该值类型是Animation<Color>,这允许对进度条的颜色也可以指定动画。
// 如果不需要对进度条颜色执行动画,换言之,想对进度条应用一种固定的颜色,此时可以通过AlwaysStoppedAnimation来指定。
Animation<Color> valueColor,
String semanticsLabel,
String semanticsValue,
}) //
const CircularProgressIndicator({
Key key,
double value,
Color backgroundColor,
Animation<Color> valueColor,
// 表示圆形进度条的粗细。
this.strokeWidth = 4.0,
String semanticsLabel,
String semanticsValue,
})

代码示例

import 'package:flutter/material.dart';

class ProgressTest extends StatefulWidget{
@override
_ProgressTestState createState() => _ProgressTestState();
} class _ProgressTestState extends State<ProgressTest> with SingleTickerProviderStateMixin { AnimationController _animationController; @override
void initState() {
// 动画执行时间
_animationController = AnimationController(vsync: this, duration: Duration(seconds: 5));
_animationController.forward();
_animationController.addListener(() => setState(() => {}));
super.initState();
} @override
void dispose() {
_animationController.dispose();
super.dispose();
} @ override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('进度指示器'),
),
body: Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0, horizontal: 24.0),
child: Column(
children: <Widget>[ // 模糊线性进度条(执行循环动画,蓝色条一直在移动)
LinearProgressIndicator(
backgroundColor: Colors.grey[200],
valueColor: AlwaysStoppedAnimation(Colors.blue),
), // 静止线性进度条(进度条是静止的,停在50%的位置)
LinearProgressIndicator(
backgroundColor: Colors.grey[200],
valueColor: AlwaysStoppedAnimation(Colors.blue),
value: .5,
), // 模糊圆形进度条(执行旋转动画)
CircularProgressIndicator(
backgroundColor: Colors.grey[200],
valueColor: AlwaysStoppedAnimation(Colors.blue),
), // 静止圆形进度条(停在50%的位置)
CircularProgressIndicator(
backgroundColor: Colors.grey[200],
valueColor: AlwaysStoppedAnimation(Colors.blue),
value: .5,
), // 自定义线性进度条
// 高度指定为10
SizedBox(
height: 10,
child: LinearProgressIndicator(
backgroundColor: Colors.grey[200],
valueColor: AlwaysStoppedAnimation(Colors.blue),
),
), // 自定义圆形进度条
// 直径指定为100
SizedBox(
// 如果设置的宽高不一样,则会为椭圆
height: 100,
width: 100,
child: CircularProgressIndicator(
backgroundColor: Colors.grey[200],
valueColor: AlwaysStoppedAnimation(Colors.blue),
),
), // 进度条颜色动画,实现一个进度条在5秒内从红色变成蓝色的动画
LinearProgressIndicator(
backgroundColor: Colors.red,
valueColor: ColorTween(begin: Colors.grey, end: Colors.blue).animate(_animationController),
value: _animationController.value,
), ],
),
),
);
}
}

代码分析

自定义尺寸

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

其实LinearProgressIndicator和CircularProgressIndicator都是取父容器的尺寸作为绘制的边界的。知道了这点,便可以通过尺寸限制类Widget,如ConstrainedBox、SizedBox等容器类组件来指定尺寸。

自定义进度指示器样式

  1. 定制进度指示器风格样式,可以通过CustomPainter Widget 来自定义绘制逻辑,实际上LinearProgressIndicator和CircularProgressIndicator也正是通过CustomPainter来实现外观绘制的。
  2. 利用一些优秀的第三方包。例如flutter_spinkit包提供了多种风格的模糊进度指示器。

Flutter 基础组件:进度指示器的更多相关文章

  1. Flutter 基础组件:单选框和复选框

    前言 Material组件库中提供了Material风格的单选开关Switch和复选框Checkbox,虽然它们都是继承自StatefulWidget,但它们本身不会保存当前选中状态,选中状态都是由父 ...

  2. Flutter 基础组件:图片和Icon

    前言 Flutter中,可以通过Image组件来加载并显示图片,Image的数据源可以是asset.文件.内存以及网络. ImageProvider 是一个抽象类,主要定义了图片数据获取的接口load ...

  3. Flutter 基础组件:按钮

    前言 Material组件库中提供了多种按钮组件如RaisedButton.FlatButton.OutlineButton等,它们都是直接或间接对RawMaterialButton组件的包装定制,所 ...

  4. Flutter 基础组件:文本、字体样式

    // 文本.字体样式 import 'package:flutter/material.dart'; class TextFontStyle extends StatelessWidget { // ...

  5. Flutter 基础组件:状态管理

    前言 一个永恒的主题,"状态(State)管理",无论是在React/Vue(两者都是支持响应式编程的Web开发框架)还是Flutter中,他们讨论的问题和解决的思想都是一致的. ...

  6. Flutter 基础组件:Widget简介

    概念 在Flutter中几乎所有的对象都是一个Widget.与原生开发中"控件"不同的是,Flutter中的Widget的概念更广泛,它不仅可以表示UI元素,也可以表示一些功能性的 ...

  7. flutter 基础组件

    TextWidget class TextWidget extends StatelessWidget { final TextStyle _textStyle = TextStyle( fontSi ...

  8. Flutter 基础组件:输入框和表单

    前言 Material组件库中提供了输入框组件TextField和表单组件Form. 输入框TextField 接口描述 const TextField({ Key key, // 编辑框的控制器,通 ...

  9. 「小程序JAVA实战」小程序的基础组件(24)

    转自:https://idig8.com/2018/08/12/xiaochengxu-chuji-24/ 来说下 ,小程序的基础组件.源码:https://github.com/limingios/ ...

随机推荐

  1. python 数据分析与挖掘实战01

    python 数据分析与挖掘实战 day 01 08/02 这种从数据中"淘金",从大量数据包括文本中挖掘出隐含的.未知的.对决策有潜在价值关系.模式或者趋势,并用这些知识和规则建 ...

  2. 微信开发中,不同手机系统遇到的bug(不定时更新)

    Ios系统 1.body上绑定click事件失效. 解决:body标签下面,用个div,当做包裹所有内容的大容器.给这个div,绑定click事件. 2.不支持 YYYY-MM-DD 的时间格式. 用 ...

  3. 【Jmeter中,保存测试结果xml时报 error loading results file -see log file 问题的处理办法】

    使用JMeter测试并发保存测试文件时报错:Error loading results file - see file log解决办法:新建一个文本文件(什么类型都可以),在文件中加上<?xml ...

  4. 用列表+for循环生成乘法口诀表

    1 # 结合一下列表生成, 准备设计乘法表 2 # numlist = [1,2,3,4,5] 3 # [pow(i,3) for i in numlist] 4 # ## [1, 8, 27, 64 ...

  5. AdaBoost 算法-分析波士顿房价数据集

    公号:码农充电站pro 主页:https://codeshellme.github.io 在机器学习算法中,有一种算法叫做集成算法,AdaBoost 算法是集成算法的一种.我们先来看下什么是集成算法. ...

  6. Liunx运维(五)-信息显示与搜索文件命令

    文档目录: 一.uname:显示系统信息 二.hostname:显示或设置系统的主机名 三.dmesg:系统启动异常诊断 四.stat:显示文件或文件系统状态 五.du:统计磁盘空间使用情况 六.da ...

  7. Linux系列之Centos安装

    http://mirrors.aliyun.com/centos/6/isos/x86_64/可下载iso文件 第一步 笔记本进入BIOS开启虚拟化 第二步 进入vmware官网下载vm,作者用的是v ...

  8. Python 中日期函数

    导入日期库 datetime import datetime # 或者from datetime import datetime ,date 字符串转datetime

  9. C#动态实体集的反序列化(动态JSON反序列化)

    一.使用场景 我们在将 JSON 反序列化实体集的时候,如果字段是固定的,那么我们序列化非常简单,对应字段写的实体集就可以了.比如下面这种: { "data":[ { " ...

  10. C# 7-zip 压缩和解压缩

    using System; using System.Collections.Generic; using System.Text; using SevenZip; using System.IO; ...