Flutter加载动画插件excellent_loading的使用

Flutter加载动画插件excellent_loading的使用

本README描述了如何使用excellent_loading插件。如果你将此包发布到pub.dev,此README的内容将出现在你的包的首页。

特性

  • 提供多种加载动画样式。
  • 支持自定义背景颜色和遮罩颜色。
  • 可以设置加载指示器的颜色和类型。

开始使用

要开始使用excellent_loading插件,你需要在pubspec.yaml文件中添加以下依赖:

dependencies:
  excellent_loading: ^版本号

然后运行flutter pub get来获取最新的依赖项。

使用方法

首先,初始化excellent_loading实例并进行配置。以下是一个简单的示例:

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

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      builder: ExcellentLoading.init(),
      home: const Home(),
    );
  }
}

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

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  List<Album> albums = [];

  Future<void> init() async {
    ExcellentLoading.show();
    final response = await Dio().get('https://jsonplaceholder.typicode.com/albums/');
    if (response.statusCode == 200) {
      for (var element in response.data) {
        Album album = Album.fromJson(element);
        albums.add(album);
      }
      setState(() {});
      ExcellentLoading.dismiss();
    }
  }

  @override
  void initState() {
    init();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          ...albums.map((e) => ListTile(
            title: Text(e.title),
            onTap: () async {
              ExcellentLoading.show();
              final response = await Dio().get('https://jsonplaceholder.typicode.com/albums/${e.id}');
              if (response.statusCode == 200) {
                ExcellentLoading.dismiss();
                final Album album = Album.fromJson(response.data);
                if (mounted) {
                  Navigator.push(context, MaterialPageRoute(builder: (_) => Detail(album: album)));
                }
              }
            },
          ))
        ],
      ),
    );
  }
}

class Detail extends StatelessWidget {
  final Album album;
  const Detail({super.key, required this.album});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: ListTile(
        leading: Text(album.id.toString()),
        title: Text(album.title),
        trailing: Text('userId: ${album.userId}'),
      ),
    );
  }
}

class Album {
  final int userId;
  final int id;
  final String title;

  const Album({
    required this.userId,
    required this.id,
    required this.title,
  });

  factory Album.fromJson(Map<String, dynamic> json) {
    return Album(
      userId: json['userId'],
      id: json['id'],
      title: json['title'],
    );
  }
}

高级配置

你还可以通过配置更多的参数来自定义加载动画的外观和行为。例如:

ExcellentLoading.instance
  ..maskType = ExcellentLoadingMaskType.custom
  ..indicatorType = ExcellentLoadingIndicatorType.ios
  ..backgroundColor = Colors.white
  ..loadingStyle = ExcellentLoadingStyle.custom
  ..maskType = ExcellentLoadingMaskType.black
  ..maskColor = Colors.black
  ..indicatorColor = Colors.black
  ..textColor = Colors.black
  ..boxShadow = []
  // ..contentPadding = EdgeInsets.all(10)
  // ..status = 'Please wait'
  ..indicatorWidget = SpinKitFadingCube(
    color: Colors.purple,
  )
  ..maskColor = Colors.white.withOpacity(0.1);

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

1 回复

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


当然,以下是如何在Flutter项目中使用excellent_loading插件来显示加载动画的一个简单示例。excellent_loading是一个流行的Flutter插件,用于显示加载指示器。

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

dependencies:
  flutter:
    sdk: flutter
  excellent_loading: ^3.0.0  # 请注意版本号,使用最新版本

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

接下来,在你的Flutter项目中,你可以按照以下步骤来使用excellent_loading插件。

1. 导入插件

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

import 'package:excellent_loading/excellent_loading.dart';

2. 初始化Loading实例

在你的主文件(通常是main.dart)中,你可以创建一个全局的ExcellentLoading实例:

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  final ExcellentLoading loading = ExcellentLoading();
  runApp(MyApp(loading: loading));
}

3. 包装你的应用

在你的MyApp类中使用ExcellentLoading包装你的应用:

import 'package:flutter/material.dart';

class MyApp extends StatelessWidget {
  final ExcellentLoading loading;

  MyApp({required this.loading});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ExcellentLoadingProvider(
        child: Scaffold(
          appBar: AppBar(
            title: Text('Excellent Loading Demo'),
          ),
          body: MyHomePage(loading: loading),
        ),
        loading: loading, // 将loading实例传递给provider
      ),
    );
  }
}

4. 在需要的地方显示加载动画

在你的页面或组件中,你可以通过调用loading.show()loading.dismiss()来显示和隐藏加载动画:

class MyHomePage extends StatefulWidget {
  final ExcellentLoading loading;

  MyHomePage({required this.loading});

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  void _showLoading() {
    widget.loading.show(
      context,
      type: ExcellentLoadingType.circle, // 你可以选择其他类型,比如line
      loadingTextStyle: TextStyle(color: Colors.white),
      indicatorColor: Colors.blue,
      backgroundColor: Colors.black.withOpacity(0.5),
    );

    // 模拟一个耗时操作
    Future.delayed(Duration(seconds: 2)).then((_) {
      widget.loading.dismiss();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        onPressed: _showLoading,
        child: Text('Show Loading'),
      ),
    );
  }
}

在这个示例中,当你点击按钮时,会显示一个加载动画,并在2秒后自动隐藏。

这个示例展示了如何在Flutter项目中使用excellent_loading插件来显示加载动画。你可以根据需要自定义加载动画的样式和行为。

回到顶部