網站首頁 編程語言 正文
本文實例為大家分享了Flutter組件實現進度指示器的具體代碼,供大家參考,具體內容如下
進度指示器
Material 組件庫中提供了兩種進度指示器:LinearProgressIndicator和CircularProgressIndicator,它們都可以同時用于精確的進度指示和模糊的進度指示。精確進度通常用于任務進度可以計算和預估的情況,比如文件下載;而模糊進度則用戶任務進度無法準確獲得的情況,如下拉刷新,數據提交等。
LinearProgressIndicator
LinearProgressIndicator是一個線性、條狀的進度條,定義如下:
LinearProgressIndicator({ ? double value, ? Color backgroundColor, ? Animation<Color> valueColor, ? ... })
- value:value表示當前的進度,取值范圍為[0,1];如果value為null時則指示器會執行一個循環動畫(模糊進度);當value不為null時,指示器為一個具體進度的進度條。
- backgroundColor:指示器的背景色。
- valueColor:
指示器的進度條顏色;值得注意的是,該值類型是Animation,這允許我們對進度條的顏色也可以指定動畫。如果我們不需要對進度條顏色執行動畫,換言之,我們想對進度條應用一種固定的顏色,此時我們可以通過AlwaysStoppedAnimation來指定。
示例:
// 模糊進度條(會執行一個動畫) LinearProgressIndicator( ? backgroundColor: Colors.grey[200], ? valueColor: AlwaysStoppedAnimation(Colors.blue), ), //進度條顯示50% LinearProgressIndicator( ? backgroundColor: Colors.grey[200], ? valueColor: AlwaysStoppedAnimation(Colors.blue), ? value: .5,? )
運行效果如圖所示:
第一個進度條在執行循環動畫:藍色條一直在移動,而第二個進度條是靜止的,停在50%的位置。
CircularProgressIndicator
CircularProgressIndicator是一個圓形進度條,定義如下:
?CircularProgressIndicator({ ? double value, ? Color backgroundColor, ? Animation<Color> valueColor, ? this.strokeWidth = 4.0, ? ... ?? })
前三個參數和LinearProgressIndicator相同,不再贅述。strokeWidth 表示圓形進度條的粗細。示例如下:
// 模糊進度條(會執行一個旋轉動畫) CircularProgressIndicator( ? backgroundColor: Colors.grey[200], ? valueColor: AlwaysStoppedAnimation(Colors.blue), ), //進度條顯示50%,會顯示一個半圓 CircularProgressIndicator( ? backgroundColor: Colors.grey[200], ? valueColor: AlwaysStoppedAnimation(Colors.blue), ? value: .5, ),
運行效果如圖所示:
第一個進度條會執行旋轉動畫,而第二個進度條是靜止的,它停在50%的位置。
自定義尺寸
我們可以發現LinearProgressIndicator
和CircularProgressIndicator
,并沒有提供設置圓形進度條尺寸的參數;如果我們希望LinearProgressIndicator
的線細一些,或者希望CircularProgressIndicator
的圓大一些該怎么做?
其實LinearProgressIndicator
和CircularProgressIndicator
都是取父容器的尺寸作為繪制的邊界的。知道了這點,我們便可以通過尺寸限制類Widget,如ConstrainedBox
、SizedBox
來指定尺寸,如:
// 線性進度條高度指定為3 SizedBox( ? height: 3, ? child: LinearProgressIndicator( ? ? backgroundColor: Colors.grey[200], ? ? valueColor: AlwaysStoppedAnimation(Colors.blue), ? ? value: .5, ? ), ), // 圓形進度條直徑指定為100 SizedBox( ? height: 100, ? width: 100, ? child: CircularProgressIndicator( ? ? backgroundColor: Colors.grey[200], ? ? valueColor: AlwaysStoppedAnimation(Colors.blue), ? ? value: .7, ? ), ),
運行效果如圖所示:
注意,如果CircularProgressIndicator顯示空間的寬高不同,則會顯示為橢圓。如:
// 寬高不等 SizedBox( ? height: 100, ? width: 130, ? child: CircularProgressIndicator( ? ? backgroundColor: Colors.grey[200], ? ? valueColor: AlwaysStoppedAnimation(Colors.blue), ? ? value: .7, ? ), ),
運行效果如圖所示:
進度色動畫
前面說過可以通過valueColor對進度條顏色做動畫,這里先給出一個例子,讀者在了解了Flutter動畫一章后再回過頭來看。
我們實現一個進度條在3秒內從灰色變成藍色的動畫:
import 'package:flutter/material.dart'; class ProgressRoute extends StatefulWidget { ? @override ? _ProgressRouteState createState() => _ProgressRouteState(); } class _ProgressRouteState extends State<ProgressRoute> ? ? with SingleTickerProviderStateMixin { ? AnimationController _animationController; ? @override ? void initState() { ? ? //動畫執行時間3秒 ? ? ? _animationController = ? ? ? ? new AnimationController(vsync: this, duration: Duration(seconds: 3)); ? ? _animationController.forward(); ? ? _animationController.addListener(() => setState(() => {})); ? ? super.initState(); ? } ? @override ? void dispose() { ? ? _animationController.dispose(); ? ? super.dispose(); ? } ? @override ? Widget build(BuildContext context) { ? ? return SingleChildScrollView( ? ? ? child: Column( ? ? ? ? children: <Widget>[ ? ? ? ? ? ? Padding( ? ? ? ? ? ? padding: EdgeInsets.all(16), ? ? ? ? ? ? child: LinearProgressIndicator( ? ? ? ? ? ? ? backgroundColor: Colors.grey[200], ? ? ? ? ? ? ? valueColor: ColorTween(begin: Colors.grey, end: Colors.blue) ? ? ? ? ? ? ? ? .animate(_animationController), // 從灰色變成藍色 ? ? ? ? ? ? ? value: _animationController.value, ? ? ? ? ? ? ), ? ? ? ? ? ); ? ? ? ? ], ? ? ? ), ? ? ); ? } }
自定義進度指示器樣式
定制進度指示器風格樣式,可以通過CustomPainter Widget 來自定義繪制邏輯,實際上LinearProgressIndicator和CircularProgressIndicator也正是通過CustomPainter來實現外觀繪制的。關于CustomPainter,我們將在后面“自定義Widget”一章中詳細介紹。
flutter_spinkit 包提供了多種風格的模糊進度指示器,讀者若是感興趣,可以參考。
原文鏈接:https://blog.csdn.net/m0_46369686/article/details/104699568
相關推薦
- 2022-03-18 AndroidStudio集成OpenCV的實現教程_Android
- 2022-03-09 c++中STL庫隊列詳細介紹_C 語言
- 2022-10-17 QT?TCP實現簡單的通信示例_C 語言
- 2024-02-29 UNI-APP中點擊事件多重響應問題的解決,list列表項item和列表項item中按鈕的點擊事件沖
- 2022-05-18 React?Hook之使用State?Hook的方法_React
- 2021-12-16 方案缺陷-HAProxy + Sentinel +redis
- 2022-08-20 C#?Chart折線圖使用鼠標滾輪放大、縮小和平移曲線方式_C#教程
- 2023-01-02 Python中字符串的常用方法總結_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支