Flutter窗口加载动画插件winloading的使用

Flutter窗口加载动画插件winloading的使用

什么是它?

在线测试

以下是一些演示视频:

如何使用

在 pubspec.yaml 文件中添加依赖:

dependencies:
  winloading: ^0.0.2

然后导入包并使用:

import 'package:winloading/winloading.dart';

Center(
  child: View3D.me(),
)

自定义配置

WinLoading 构造函数提供了丰富的参数来满足不同的需求:

const WinLoading({
    this.color = Colors.white,       // 加载动画的颜色,默认为白色
    this.radius = 3,                 // 圆角半径,默认为 3
    this.gap = 0.04,                 // 空隙比例,默认为 0.04
    this.amount = 5,                 // 动画数量,默认为 5
    this.width = 60,                 // 宽度,默认为 60
    this.height = 60,                // 高度,默认为 60
    this.duration = const Duration(milliseconds: 7200), // 动画持续时间,默认为 7200 毫秒
    this.cubic = const Cubic(.21, .6, .59, .8), // 动画曲线,默认为 Cubic(.21, .6, .59, .8)
    Key? key,                        // 键值
})

示例代码

以下是一个完整的示例代码,展示了如何在 Flutter 中使用 winloading 插件:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:winloading/winloading.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // 应用程序的根部件
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          // 第一个加载动画
          const ColoredBox(
            color: Colors.black38,
            child: WinLoading(), // 使用默认配置
          ),
          const SizedBox.square(dimension: 10,), // 添加间距

          // 第二个加载动画,自定义 cubic 曲线
          const ColoredBox(
            color: Colors.black38,
            child: WinLoading(cubic: const Cubic(.07, .59, .59, .8)),
          ),
          const SizedBox.square(dimension: 10,),

          // 第三个加载动画,再次自定义 cubic 曲线
          const ColoredBox(
            color: Colors.black38,
            child: WinLoading(cubic: const Cubic(.17,.59,.59,.8)),
          ),
          const SizedBox.square(dimension: 10,),

          // 第四个加载动画,继续自定义 cubic 曲线
          const ColoredBox(
            color: Colors.black38,
            child: WinLoading(cubic: const Cubic(.22,.42,.64,.79)),
          ),
          const SizedBox.square(dimension: 10,),

          // 第五个加载动画,更多自定义选项
          const ColoredBox(
            color: Colors.black38,
            child: WinLoading(cubic: const Cubic(.22,.42,.44,.71)),
          ),
          const SizedBox.square(dimension: 10,),

          // 第六个加载动画,进一步自定义
          const ColoredBox(
            color: Colors.black38,
            child: WinLoading(cubic: const Cubic(.15,.6,.64,.79)),
          ),
          const SizedBox.square(dimension: 10,),
        ],
      ),
    );
  }
}

更多关于Flutter窗口加载动画插件winloading的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter窗口加载动画插件winloading的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


winloading 是一个用于在 Flutter 应用中显示窗口加载动画的插件。它可以帮助你在应用程序加载数据或执行耗时操作时显示一个加载动画,以改善用户体验。以下是如何使用 winloading 插件的基本步骤。

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 winloading 插件的依赖。

dependencies:
  flutter:
    sdk: flutter
  winloading: ^0.0.1  # 请检查最新版本

然后运行 flutter pub get 来安装依赖。

2. 导入包

在你的 Dart 文件中导入 winloading 包。

import 'package:winloading/winloading.dart';

3. 使用 WinLoading 组件

WinLoading 组件可以很容易地集成到你的应用中。你可以通过设置 isLoading 属性来控制加载动画的显示和隐藏。

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool _isLoading = false;

  void _startLoading() {
    setState(() {
      _isLoading = true;
    });

    // 模拟一个耗时操作
    Future.delayed(Duration(seconds: 3), () {
      setState(() {
        _isLoading = false;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('WinLoading Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: _startLoading,
              child: Text('Start Loading'),
            ),
            SizedBox(height: 20),
            WinLoading(
              isLoading: _isLoading,
              color: Colors.blue, // 可选:设置加载动画的颜色
            ),
          ],
        ),
      ),
    );
  }
}

4. 运行应用

现在你可以运行你的应用,点击按钮时,加载动画会显示,并在 3 秒后自动消失。

5. 自定义 WinLoading

WinLoading 插件提供了一些可选的参数来自定义加载动画的外观:

  • color: 设置加载动画的颜色。
  • size: 设置加载动画的大小。
  • strokeWidth: 设置加载动画的线条宽度。
WinLoading(
  isLoading: _isLoading,
  color: Colors.red,
  size: 50.0,
  strokeWidth: 3.0,
);
回到顶部