Flutter —— 加載動(dòng)畫效果loading 類似iOS的SVProgressHUD效果

效果圖


gif




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

import 'package:flutter/material.dart';

enum AnimationType {

? ///圓圈模式

? circular,

? ///橫線模式

? Linear,

? ///刷新模式

? refresh

}

class KSProgressDialog extends StatelessWidget {

? ///是否顯示

? final bool isShow;

? ///提醒內(nèi)容

? final String? title;

? ///動(dòng)畫效果? ? 默認(rèn)圓圈模式

? 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.設(shè)置動(dòng)畫效果

? ? var progressIndicator;

? ? var needWidth =

? ? ? ? 0.0; //Linear 模式下需要設(shè)置寬度,不然的話就會(huì)占滿全屏,傳遞文字過多的話需要傳遞一個(gè)寬度的參數(shù)過來,可自己完善

? ? 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),

? ? ? );

? ? }

? }



具體的效果可以根據(jù)自己的情況進(jìn)行修改,復(fù)制即用!

demo地址:

https://github.com/Turboks/KSProgress

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容