Flutter 基础组件:进度指示器
前言
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等容器类组件来指定尺寸。
自定义进度指示器样式
- 定制进度指示器风格样式,可以通过CustomPainter Widget 来自定义绘制逻辑,实际上LinearProgressIndicator和CircularProgressIndicator也正是通过CustomPainter来实现外观绘制的。
- 利用一些优秀的第三方包。例如flutter_spinkit包提供了多种风格的模糊进度指示器。
Flutter 基础组件:进度指示器的更多相关文章
- Flutter 基础组件:单选框和复选框
前言 Material组件库中提供了Material风格的单选开关Switch和复选框Checkbox,虽然它们都是继承自StatefulWidget,但它们本身不会保存当前选中状态,选中状态都是由父 ...
- Flutter 基础组件:图片和Icon
前言 Flutter中,可以通过Image组件来加载并显示图片,Image的数据源可以是asset.文件.内存以及网络. ImageProvider 是一个抽象类,主要定义了图片数据获取的接口load ...
- Flutter 基础组件:按钮
前言 Material组件库中提供了多种按钮组件如RaisedButton.FlatButton.OutlineButton等,它们都是直接或间接对RawMaterialButton组件的包装定制,所 ...
- Flutter 基础组件:文本、字体样式
// 文本.字体样式 import 'package:flutter/material.dart'; class TextFontStyle extends StatelessWidget { // ...
- Flutter 基础组件:状态管理
前言 一个永恒的主题,"状态(State)管理",无论是在React/Vue(两者都是支持响应式编程的Web开发框架)还是Flutter中,他们讨论的问题和解决的思想都是一致的. ...
- Flutter 基础组件:Widget简介
概念 在Flutter中几乎所有的对象都是一个Widget.与原生开发中"控件"不同的是,Flutter中的Widget的概念更广泛,它不仅可以表示UI元素,也可以表示一些功能性的 ...
- flutter 基础组件
TextWidget class TextWidget extends StatelessWidget { final TextStyle _textStyle = TextStyle( fontSi ...
- Flutter 基础组件:输入框和表单
前言 Material组件库中提供了输入框组件TextField和表单组件Form. 输入框TextField 接口描述 const TextField({ Key key, // 编辑框的控制器,通 ...
- 「小程序JAVA实战」小程序的基础组件(24)
转自:https://idig8.com/2018/08/12/xiaochengxu-chuji-24/ 来说下 ,小程序的基础组件.源码:https://github.com/limingios/ ...
随机推荐
- 记一次storm提交任务遇到的坑
摘要:主要是自己没有真正理解storm jar命令参数的意义. 情景复现: 在storm集群中使用命令提交后,在UI界面中,一直看不见任务提交上来的任务,但是在集群提交的shell界面中,是可以看到相 ...
- logstash导入DNS解析数据到es中,中间有filebeat
这个过程中,主要用logstash处理数据的时候不好处理. 在logstash-sample.conf这个配置文件中的配置,我用这个监控filebeat的5044端口 # Sample Logstas ...
- yum install nginx-没有可用软件包 nginx。
1. 错误提示 Centos 7下安装nginx,使用yum install nginx,报错提示没有可用的软件包.具体错误提示如下: 已加载插件:fastestmirror, product-id, ...
- js上 十二、函数初步-1
11-1.引入(认识函数) 引入: 说起函数,其实我们并不陌生,在初中数学中我们就接触过函数:例如我们所学的 y = 2X+1 ; 这是一个二元一次方程,也是我们数字中的函数: 当我们每次输入 ...
- js下 Day14、面向对象案例
一.软键盘拖拽 效果图: )
最近在使用数据库迁移适配,由MySQL 库迁移到达梦数据库,其中进行行转列时,MySQL转换达梦sql语法有些问题,特记录. 在MySQL 下有Group_Concat(expr) ,在达梦及神通数 ...