Flutter加载动画进度展示插件animation_load_progress的使用

Flutter加载动画进度展示插件animation_load_progress的使用

使用

Animation Load Progress 是一个用于在 Flutter 应用中显示加载动画进度的插件。以下是一个简单的示例,演示如何使用该插件。

首先,确保你已经在 pubspec.yaml 文件中添加了依赖项:

dependencies:
  animation_load_progress: ^版本号

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

接下来,我们来看一下具体的使用方法。首先创建一个 HomeView 类,该类继承自 StatefulWidget。在这个类中,我们定义了一个布尔变量 isAsync 用于控制是否显示加载动画。同时,我们定义了一个 signIn 方法来模拟异步操作,并在操作完成后将 isAsync 设置为 false

class HomeView extends StatefulWidget {
  const HomeView({super.key});

  [@override](/user/override)
  State<HomeView> createState() => _HomeViewState();
}

class _HomeViewState extends State<HomeView> {
  bool isAsync = false; // 添加变量

  void signIn() {
    isAsync = true;
    setState(() {});
    Future.delayed(
      const Duration(
        seconds: 3,
      ),
      () {
        isAsync = false;
        setState(() {});
      },
    );
  }

  [@override](/user/override)
  void initState() {
    signIn();
    super.initState();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return AnimationLoadProgress(
        inAsyncCall: isAsync,
        colorProgress: Colors.red, // 选择你的颜色
        child: Scaffold(
          backgroundColor: Colors.white,
          appBar: AppBar(
            backgroundColor: Colors.white,
            title: const Text(
              'Loading ...',
              style: TextStyle(color: Colors.white),
            ),
            centerTitle: true,
          ),
          body: const Center(
            child: Text('Loading Example'),
          ),
        ));
  }
}

在这个示例中,AnimationLoadProgress 组件根据 inAsyncCall 的值来决定是否显示加载动画。当 isAsynctrue 时,会显示加载动画;反之,则显示普通内容。

colorProgress 参数用于设置加载动画的颜色。你可以根据需要更改它以适应你的应用主题。

最后,我们通过 MaterialAppScaffold 构建了基本的应用界面。当 isAsynctrue 时,用户会看到一个红色的加载动画,而当 isAsyncfalse 时,会看到 “Loading Example” 文本。

完整示例代码

以下是完整的示例代码:

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

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

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        floatingActionButtonTheme: FloatingActionButtonThemeData(
          backgroundColor: Colors.blue.shade800,
        ),
        colorScheme: ColorScheme.light(primary: Colors.blue.shade800),
      ),
      home: const HomeView(),
    );
  }
}

class HomeView extends StatefulWidget {
  const HomeView({super.key});

  [@override](/user/override)
  State<HomeView> createState() => _HomeViewState();
}

class _HomeViewState extends State<HomeView> {
  bool isAsync = false; // 添加变量

  void signIn() {
    isAsync = true;
    setState(() {});
    Future.delayed(
      const Duration(
        seconds: 3,
      ),
      () {
        isAsync = false;
        setState(() {});
      },
    );
  }

  [@override](/user/override)
  void initState() {
    signIn();
    super.initState();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return AnimationLoadProgress(
      inAsyncCall: isAsync,
      colorProgress: Colors.red, // 选择你的颜色
      child: Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          backgroundColor: Colors.white,
          title: const Text(
            'Loading ...',
            style: TextStyle(color: Colors.white),
          ),
          centerTitle: true,
        ),
        body: const Center(
          child: Text('Loading Example'),
        ),
      ),
    );
  }
}

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

1 回复

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


在 Flutter 中,animation_load_progress 是一个用于展示加载动画和进度的插件。它可以帮助你在加载数据或执行耗时操作时,向用户展示一个带有动画效果的进度条或加载指示器。

安装插件

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

dependencies:
  flutter:
    sdk: flutter
  animation_load_progress: ^1.0.0  # 请确保使用最新版本

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

基本使用

animation_load_progress 插件提供了 AnimationLoadProgress 组件,你可以通过它来展示加载动画和进度。

1. 简单加载动画

如果你只需要一个简单的加载动画,可以使用 AnimationLoadProgress 组件:

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

class LoadingScreen extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Loading Example'),
      ),
      body: Center(
        child: AnimationLoadProgress(
          size: 50.0,
          color: Colors.blue,
        ),
      ),
    );
  }
}

在这个例子中,AnimationLoadProgress 会显示一个旋转的加载动画。

2. 带进度的加载动画

如果你需要展示加载进度,可以使用 progress 参数:

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

class LoadingScreen extends StatefulWidget {
  [@override](/user/override)
  _LoadingScreenState createState() => _LoadingScreenState();
}

class _LoadingScreenState extends State<LoadingScreen> {
  double _progress = 0.0;

  void _updateProgress() {
    setState(() {
      _progress += 0.1;
      if (_progress > 1.0) {
        _progress = 0.0;
      }
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Loading Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            AnimationLoadProgress(
              size: 100.0,
              color: Colors.blue,
              progress: _progress,
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: _updateProgress,
              child: Text('Update Progress'),
            ),
          ],
        ),
      ),
    );
  }
}

在这个例子中,AnimationLoadProgress 会根据 _progress 的值显示加载进度。点击按钮可以更新进度。

3. 自定义动画

你还可以通过 animation 参数来自定义动画效果:

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

class LoadingScreen extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Loading Example'),
      ),
      body: Center(
        child: AnimationLoadProgress(
          size: 100.0,
          color: Colors.blue,
          animation: AnimationType.bounce, // 自定义动画类型
        ),
      ),
    );
  }
}
回到顶部