草庐IT

Flutter —— 加载动画效果loading 类似iOS的SVProgressHUD效果

Turboks 2023-03-28 原文
效果图


gif




根据需求创建一个自定义的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✨!

有关Flutter —— 加载动画效果loading 类似iOS的SVProgressHUD效果的更多相关文章

  1. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  2. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  3. ruby - RuntimeError(自动加载常量 Apps 多线程时检测到循环依赖 - 2

    我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("

  4. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  5. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  6. ruby-on-rails - 使用 config.threadsafe 时从 lib/加载模块/类的正确方法是什么!选项? - 2

    我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co

  7. Unity 3D 制作开关门动画,旋转门制作,推拉门制作,门把手动画制作 - 2

    Unity自动旋转动画1.开门需要门把手先动,门再动2.关门需要门先动,门把手再动3.中途播放过程中不可以再次进行操作觉得太复杂?查看我的文章开关门简易进阶版效果:如果这个门可以直接打开的话,就不需要放置"门把手"如果门把手还有钥匙需要旋转,那就可以把钥匙放在门把手的"门把手",理论上是可以无限套娃的可调整参数有:角度,反向,轴向,速度运行时点击Test进行测试自己写的代码比较垃圾,命名与结构比较拉,高手轻点喷,新手有类似的需求可以拿去做参考上代码usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;u

  8. ruby-on-rails - 从应用程序中自定义文件夹内的命名空间自动加载 - 2

    我们目前正在为ROR3.2开发自定义cms引擎。在这个过程中,我们希望成为我们的rails应用程序中的一等公民的几个类类型起源,这意味着它们应该驻留在应用程序的app文件夹下,它是插件。目前我们有以下类型:数据源数据类型查看我在app文件夹下创建了多个目录来保存这些:应用/数据源应用/数据类型应用/View更多类型将随之而来,我有点担心应用程序文件夹被这么多目录污染。因此,我想将它们移动到一个子目录/模块中,该子目录/模块包含cms定义的所有类型。所有类都应位于MyCms命名空间内,目录布局应如下所示:应用程序/my_cms/data_source应用程序/my_cms/data_ty

  9. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  10. ruby - 用 YAML.load 解析 json 安全吗? - 2

    我正在使用ruby2.1.0我有一个json文件。例如:test.json{"item":[{"apple":1},{"banana":2}]}用YAML.load加载这个文件安全吗?YAML.load(File.read('test.json'))我正在尝试加载一个json或yaml格式的文件。 最佳答案 YAML可以加载JSONYAML.load('{"something":"test","other":4}')=>{"something"=>"test","other"=>4}JSON将无法加载YAML。JSON.load("

随机推荐