StatelessWidget.build
方法中:创建StatelessWidget
的build
方法State
对象中:创建StatefulWidget
的State对象的build方法中,另一个是State的成员变量
有关BuildContext更深入的探讨不在此文的探讨范围内,如果使用showDialog
实现弹窗操作,那么我们所考虑的问题便是,如何方便快捷的在任意地方去获取BuildContext,从而实现弹窗。如果有同学恰巧也用了showDialog
这种方式的话,我相信,你也会发现,在任意地方获取BuildContext并不是那么简单,而且会产生很多不必要的代码量。
那么,我们就只能使用这种体验极其不友好的方法么?
当然不是的,请继续看。
Flutter EasyLoading 介绍
Flutter EasyLoading是一个简单易用的Flutter插件,包含23种loading动画效果、进度条展示、Toast展示。纯Flutter端实现,兼容性好,支持iOS、Android。先简单看下如何使用Flutter EasyLoading
。
安装
将以下代码添加到您项目中的 pubspec.yaml
文件:
dependencies:
flutter_easyloading: ^1.1.0 // 请使用最新版
导入
import ‘package:flutter_easyloading/flutter_easyloading.dart’;
如何使用
首先, 使用 FlutterEasyLoading 组件包裹您的App组件:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
/// 子组件通常为 [MaterialApp] 或者 [CupertinoApp].
/// 这样做是为了确保 loading 组件能覆盖在其他组件之上.
return FlutterEasyLoading(
child: MaterialApp(
title: ‘Flutter EasyLoading’,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: ‘Flutter EasyLoading’),
),
);
}
}
然后, 请尽情使用吧:
EasyLoading.show(status: ‘loading…’);
EasyLoading.showProgress(0.3, status: ‘downloading…’);
EasyLoading.showSuccess(‘Great Success!’);
EasyLoading.showError(‘Failed with Error’);
EasyLoading.showInfo(‘Useful Information.’);
EasyLoading.dismiss();
自定义样式
首先,我们看下Flutter EasyLoading目前支持的自定义属性:
/// loading的样式, 默认[EasyLoadingStyle.dark].
EasyLoadingStyle loadingStyle;
/// loading的指示器类型, 默认[EasyLoadin