Flutter自定义GIF加载动画插件custom_gif_loading的使用

发布于 1周前 作者 caililin 来自 Flutter

Flutter自定义GIF加载动画插件custom_gif_loading的使用

开始使用

这是一个可定制的、简单且易于使用的GIF加载动画。

GIF展示

安装

在你的项目的pubspec.yaml文件中添加以下依赖:

dependencies:
  custom_gif_loading: ^latest

然后运行flutter pub get来安装该包。

导入

在你的 Dart 文件中导入该插件:

import 'package:custom_gif_loading/custom_gif_loading.dart';

如何使用

首先,将你的GIF文件添加到项目资源中:

Image gif = Image.asset('assets/GIF/Spinner.gif');

然后,通过传递GIF来启动加载:

Loading(gif).start(context);

停止加载:

Loading.stop();

自定义

你可以在使用时自定义GIF文件:

Image gif = Image.asset('assets/GIF/your_custom_gif.gif');

示例代码

以下是完整的示例代码,展示了如何使用 custom_gif_loading 插件:

import 'package:custom_gif_loading/custom_gif_loading.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Custom GIF Loading',
      showSemanticsDebugger: false,
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  // 添加你的GIF文件到gif变量中
  Image gif = Image.asset('assets/GIF/Spinner.gif');

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.blueAccent,
        title: const Text(
          'Custom GIF Loading',
          style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
        ),
        centerTitle: true,
      ),
      body: Container(
        margin: const EdgeInsets.only(top: 34),
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        child: Center(
          child: ElevatedButton(
            onPressed: () {
              // 如果点击按钮,则开始加载
              Loading(gif).start(context);
              Future.delayed(const Duration(seconds: 5), () {
                // 5秒后停止加载
                Loading.stop();
              });
            },
            style: ElevatedButton.styleFrom(
              primary: Colors.blueAccent,
              padding: const EdgeInsets.symmetric(horizontal: 28, vertical: 18),
              onPrimary: Colors.white,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(12),
              ),
            ),
            child: const Text(
              '开始加载',
              style: TextStyle(
                  fontSize: 16, fontWeight: FontWeight.bold, letterSpacing: 1),
            ),
          ),
        ),
      ),
    );
  }
}

以上代码展示了如何在 Flutter 应用中使用 custom_gif_loading 插件来创建一个自定义的GIF加载动画。你可以根据需要调整GIF文件路径和样式。


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

1 回复

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


当然,以下是如何在Flutter项目中使用custom_gif_loading插件的一个示例代码案例。假设你已经在pubspec.yaml文件中添加了custom_gif_loading依赖,并运行了flutter pub get

首先,确保你的pubspec.yaml文件中包含以下依赖项:

dependencies:
  flutter:
    sdk: flutter
  custom_gif_loading: ^最新版本号  # 替换为实际的最新版本号

然后,你可以在你的Flutter项目中按照以下步骤使用custom_gif_loading插件。

1. 导入插件

在你的Dart文件中导入custom_gif_loading插件:

import 'package:flutter/material.dart';
import 'package:custom_gif_loading/custom_gif_loading.dart';

2. 使用自定义GIF加载动画

以下是一个完整的示例,展示如何在Flutter应用中使用CustomGifLoading组件:

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

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('Custom GIF Loading Demo'),
      ),
      body: Center(
        child: isLoading
            ? CustomGifLoading(
                gifAsset: 'assets/loading.gif',  // 替换为你的GIF资源路径
                width: 100,
                height: 100,
                backgroundColor: Colors.white,
                progressIndicatorColor: Colors.blue,
              )
            : Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(
                    'Click the button to start loading',
                    style: TextStyle(fontSize: 20),
                  ),
                  SizedBox(height: 20),
                  ElevatedButton(
                    onPressed: _startLoading,
                    child: Text('Start Loading'),
                  ),
                ],
              ),
      ),
    );
  }
}

3. 添加GIF资源

确保你在assets文件夹中有一个名为loading.gif的GIF文件,并在pubspec.yaml文件中声明它:

flutter:
  assets:
    - assets/loading.gif

4. 运行应用

现在你可以运行你的Flutter应用,点击按钮后应该能看到自定义的GIF加载动画。

注意事项

  • 确保GIF文件的路径正确无误。
  • CustomGifLoading组件的属性可以根据你的需求进行调整,例如widthheightbackgroundColorprogressIndicatorColor等。

这个示例展示了如何使用custom_gif_loading插件在Flutter应用中显示自定义的GIF加载动画。希望这对你有帮助!

回到顶部