Flutter —— 加載動畫效果loading 類似iOS的SVProgressHUD效果

效果圖


gif




根據需求創(chuàng)建一個自定義的Widget

import 'package:flutter/material.dart';

enum AnimationType {

? ///圓圈模式

? circular,

? ///橫線模式

? Linear,

? ///刷新模式

? refresh

}

class KSProgressDialog extends StatelessWidget {

? ///是否顯示

? final bool isShow;

? ///提醒內容

? final String? title;

? ///動畫效果? ? 默認圓圈模式

? final AnimationType? showType;

? ///背景透明度

? final double? alpha;

? ///字體顏色

? final Color? textColor;

? ///方塊背景顏色

? final Color? itemColor;

? const KSProgressDialog(

? ? ? {Key? key,

? ? ? required this.isShow,

? ? ? this.title,

? ? ? this.showType = AnimationType.circular,

? ? ? this.alpha = 0.1,

? ? ? this.textColor = Colors.black,

? ? ? this.itemColor = Colors.black12})

? ? ? : super(key: key);

? @override

? Widget build(BuildContext context) {

? ? List<Widget> widgetList = [];

? ? //1.設置動畫效果

? ? var progressIndicator;

? ? var needWidth =

? ? ? ? 0.0; //Linear 模式下需要設置寬度识啦,不然的話就會占滿全屏,傳遞文字過多的話需要傳遞一個寬度的參數過來虏冻,可自己完善

? ? switch (showType) {

? ? ? case AnimationType.circular:

? ? ? ? progressIndicator = CircularProgressIndicator(

? ? ? ? ? strokeWidth: 2, //線的寬度

? ? ? ? ? valueColor: AlwaysStoppedAnimation<Color>(Colors.white), //線的顏色

? ? ? ? ? backgroundColor: Colors.grey, //圓圈的背景色

? ? ? ? );

? ? ? ? break;

? ? ? case AnimationType.Linear:

? ? ? ? title == null ? needWidth = 100 : needWidth = 150;

? ? ? ? progressIndicator = LinearProgressIndicator(

? ? ? ? ? valueColor: AlwaysStoppedAnimation<Color>(Colors.white), //線的顏色

? ? ? ? ? backgroundColor: Colors.grey, //圓圈的背景

? ? ? ? );

? ? ? ? break;

? ? ? case AnimationType.refresh:

? ? ? ? progressIndicator = RefreshProgressIndicator(

? ? ? ? ? valueColor: AlwaysStoppedAnimation<Color>(Colors.white), //線的顏色

? ? ? ? ? backgroundColor: Colors.grey, //圓圈的背景色

? ? ? ? );

? ? ? ? break;

? ? ? default:

? ? }

? ? if (isShow) {

? ? ? Widget progressView;

? ? ? if (title == null) {

? ? ? ? progressView = Center(

? ? ? ? ? child: Container(

? ? ? ? ? ? width: needWidth > 0 ? needWidth : null,

? ? ? ? ? ? padding: needWidth > 0

? ? ? ? ? ? ? ? ? EdgeInsets.only(top: 40, bottom: 40, left: 10, right: 10)

? ? ? ? ? ? ? ? : EdgeInsets.all(30.0),

? ? ? ? ? ? decoration: BoxDecoration(

? ? ? ? ? ? ? ? color: itemColor, borderRadius: BorderRadius.circular(10.0)),

? ? ? ? ? ? child: Column(

? ? ? ? ? ? ? mainAxisSize: MainAxisSize.min,

? ? ? ? ? ? ? children: <Widget>[

? ? ? ? ? ? ? ? progressIndicator,

? ? ? ? ? ? ? ],

? ? ? ? ? ? ),

? ? ? ? ? ),

? ? ? ? );

? ? ? } else {

? ? ? ? progressView = Center(

? ? ? ? ? child: Container(

? ? ? ? ? ? width: needWidth > 0 ? needWidth : null,

? ? ? ? ? ? padding: const EdgeInsets.all(20.0),

? ? ? ? ? ? decoration: BoxDecoration(

? ? ? ? ? ? ? ? color: itemColor, borderRadius: BorderRadius.circular(4.0)),

? ? ? ? ? ? child: Column(

? ? ? ? ? ? ? mainAxisSize: MainAxisSize.min,

? ? ? ? ? ? ? children: <Widget>[

? ? ? ? ? ? ? ? progressIndicator,

? ? ? ? ? ? ? ? Container(

? ? ? ? ? ? ? ? ? padding: const EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 0),

? ? ? ? ? ? ? ? ? child: Text(

? ? ? ? ? ? ? ? ? ? title!,

? ? ? ? ? ? ? ? ? ? style: TextStyle(color: textColor, fontSize: 16.0),

? ? ? ? ? ? ? ? ? ),

? ? ? ? ? ? ? ? )

? ? ? ? ? ? ? ],

? ? ? ? ? ? ),

? ? ? ? ? ),

? ? ? ? );

? ? ? }

? ? ? widgetList.add(Opacity(

? ? ? ? opacity: alpha!,

? ? ? ? child: new ModalBarrier(color: Colors.black38),

? ? ? ));

? ? ? widgetList.add(progressView);

? ? }

? ? return Stack(

? ? ? children: widgetList,

? ? );

? }

}

2. 使用舉例

@override

? Widget build(BuildContext context) {

? ? if (isLoading == false) {

? ? ? return Scaffold(

? ? ? ? appBar: AppBar(

? ? ? ? ? title: Text("測試"),

? ? ? ? ),

? ? ? ? body: ListView.builder(

? ? ? ? ? itemBuilder: (context, index) {

? ? ? ? ? ? return _orderItemView();

? ? ? ? ? },

? ? ? ? ? itemCount: 10,

? ? ? ? ),

? ? ? );

? ? } else {

? ? ? return Scaffold(

? ? ? ? appBar: AppBar(

? ? ? ? ? title: Text("測試"),

? ? ? ? ),

? ? ? ? body: KSProgressDialog(isShow: true),

? ? ? );

? ? }

? }



具體的效果可以根據自己的情況進行修改商架,復制即用!

demo地址:

https://github.com/Turboks/KSProgress

如果對您有幫助,歡迎star?唤衫!

?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末狱从,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子滋尉,更是在濱河造成了極大的恐慌,老刑警劉巖飞主,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件狮惜,死亡現場離奇詭異,居然都是意外死亡碌识,警方通過查閱死者的電腦和手機碾篡,發(fā)現死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來丸冕,“玉大人,你說我怎么就攤上這事薛窥∨种颍” “怎么了?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵诅迷,是天一觀的道長佩番。 經常有香客問我,道長罢杉,這世上最難降的妖魔是什么趟畏? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮滩租,結果婚禮上赋秀,老公的妹妹穿的比我還像新娘。我一直安慰自己律想,他們只是感情好猎莲,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著技即,像睡著了一般著洼。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上而叼,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天身笤,我揣著相機與錄音,去河邊找鬼葵陵。 笑死液荸,一個胖子當著我的面吹牛,可吹牛的內容都是我干的脱篙。 我是一名探鬼主播莹弊,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼涤久,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了忍弛?” 一聲冷哼從身側響起响迂,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎细疚,沒想到半個月后蔗彤,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡疯兼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年然遏,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片吧彪。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡盐欺,死狀恐怖,靈堂內的尸體忽然破棺而出皆的,到底是詐尸還是另有隱情馁龟,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布傀缩,位于F島的核電站那先,受9級特大地震影響,放射性物質發(fā)生泄漏赡艰。R本人自食惡果不足惜售淡,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望慷垮。 院中可真熱鬧揖闸,春花似錦、人聲如沸料身。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽惯驼。三九已至蹲嚣,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間祟牲,已是汗流浹背隙畜。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留说贝,地道東北人议惰。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像乡恕,于是被迫代替她去往敵國和親言询。 傳聞我的和親對象是個殘疾皇子俯萎,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內容