lifecycle_core 0.1.6
lifecycle_core #
模仿Android Lifecycle组件功能的dart版本生命周期核心组件
Usage #
- 添加
lifecycle_core
到 dependency in your pubspec.yaml file. import 'package:lifecycle_core/lifecycle_core.dart';
LifecycleOwner
用于管理生命周期,需要由具有生命周期的对象实现,在flutter中这应该是State
类。
ViewModelStoreOwner
用于管理ViewModel
,同样具有生命周期感知能力,建议由LifecycleOwner
对象实现。
在ViewModel
中可以管理数据和执行业务相关操作,由ViewModelStoreOwner
管理,
每个ViewModelStoreOwner
实例中可以管理多个不同类型的ViewModel
实例,对于相同类型的ViewModel
只会有一个实例。
在不同的ViewModelStoreOwner
实例中同类型的ViewModel
各自管理不同的实例。
ViewModel
通过getViewModel
方法获取,需要提供一个ViewModelStoreOwner
实例和ViewModelProvider
实例。
ViewModelProvider
与ViewModel
一一对应,同一个类型的ViewModel
应该有且仅有一个对应的ViewModelProvider
,
ViewModelProvider
仅用于首次构建ViewModel
实例使用。
ChangeObservable
特化的LiveData
,仅用于通知事件的发生,比如一些数据发生了改变,这在flutter中特别有用。 通常Widget
直接绑定了ViewModel
中的一些变量,当数据发生变化时我们仅仅希望通知UI刷新而已, 此时已经不需要关心是那些数据发生了怎样的变化。
通过LifecycleObserver
可以实现具有生命周期感知的功能组件,用法请看以下示例。
通过LiveData
可以监听数据的变化,用法请看以下示例。
对于flutter的实现版本请查看 lifecycle_flutter
生命周期注入 #
/// 拥有生命周期的类,在flutter中,它应该是State类
///
/// [LifecycleOwner]负责管理生命周期,[ViewModelStoreOwner]负责管理[ViewModel]
class TestLifecycleOwner implements LifecycleOwner , ViewModelStoreOwner {
/// 正真管理生命周期事件分发
final _lifecycleRegistry = LifecycleRegistry();
/// 正真管理[ViewModel]
final _viewModelStore = ViewModelStore();
@override
Lifecycle get lifecycle => _lifecycleRegistry;
@override
ViewModelStore get viewModelStore => _viewModelStore;
// 假设为生命周期开始
create() {
print("TestLifecycleOwner create");
_lifecycleRegistry.handleLifecycleEvent(LifecycleEvent.onCreate);
}
// 假设为生命周期处于前台或附加到UI树中
resume() {
print("TestLifecycleOwner resume");
_lifecycleRegistry.handleLifecycleEvent(LifecycleEvent.onResume);
}
// 假设为生命周期处于后台或从UI树种移除
pause() {
print("TestLifecycleOwner pause");
_lifecycleRegistry.handleLifecycleEvent(LifecycleEvent.onPause);
}
// 假设为生命周期结束
destroy() {
print("TestLifecycleOwner destroy");
_lifecycleRegistry.handleLifecycleEvent(LifecycleEvent.onDestroy);
// 清理ViewModel
_viewModelStore.clear();
}
}
Lifecycle #
/// 继承方式生命周期监听器
class ExtendsLifecycleObserver extends LifecycleObserver {
@override
onCreate() {
print("ExtendsLifecycleObserver onCreate");
}
@override
onPause() {
print("ExtendsLifecycleObserver onPause");
}
@override
onResume() {
print("ExtendsLifecycleObserver onResume");
}
}
/// 完整实现方式生命周期监听器
class ImplementsLifecycleObserver implements LifecycleObserver {
@override
onCreate() {
print("ImplementsLifecycleObserver onCreate");
}
@override
onPause() {
print("ImplementsLifecycleObserver onPause");
}
@override
onResume() {
print("ImplementsLifecycleObserver onResume");
}
@override
onDestroy() {
print("ImplementsLifecycleObserver onDestroy");
}
}
/// Mixin方式生命周期监听器
class MXLifecycleObserver extends Object with LifecycleObserverMixin {
@override
onCreate() {
print("MXLifecycleObserver onCreate");
}
}
void main(){
var lifecycleOwner = TestLifecycleOwner();
lifecycleOwner.lifecycle.addObserver(ExtendsLifecycleObserver());
lifecycleOwner.lifecycle.addObserver(ImplementsLifecycleObserver());
lifecycleOwner.lifecycle.addObserver(MXLifecycleObserver());
// 直接创建实例的方式生命周期监听器
lifecycleOwner.lifecycle.addObserver(LifecycleObserver(
onCreate: () => print("onCreate"),
onResume: () => print("onResume"),
onPause: () => print("onPause"),
onDestroy:() => print("onDestroy"),);
// 模拟生命周期
lifecycleOwner.create();
lifecycleOwner.resume();
lifecycleOwner.pause();
lifecycleOwner.resume();
lifecycleOwner.pause();
lifecycleOwner.destroy();
}
ViewModel #
/// ViewModel实现类,用于提供数据和完成业务逻辑,与ui无直接关联,仅与生命周期相关
class TestViewModel extends ViewModel {
var count = 0;
TestViewModel() {
print("TestViewModel init $count");
}
add() {
print("TestViewModel ${++count}");
}
@override
void onCleared() {
print("TestViewModel onCleared $count");
}
}
/// 与[TestViewModel]对应的构建器,由于flutter不允许反射,所以使用构造器模式
class TestViewModelProvider extends ViewModelProvider<TestViewModel> {
@override
TestViewModel createViewModel() => TestViewModel();
}
class Test2ViewModel extends ViewModel {
var count = 0;
Test2ViewModel() {
print("Test2ViewModel init $count");
}
add() {
print("Test2ViewModel ${++count}");
}
@override
void onCleared() {
print("Test2ViewModel onCleared $count");
}
}
class Test2ViewModelProvider extends ViewModelProvider<Test2ViewModel> {
@override
Test2ViewModel createViewModel() => Test2ViewModel();
}
void main(){
var lifecycleOwner = TestLifecycleOwner();
lifecycleOwner.lifecycle.addObserver(LifecycleObserver(onCreate: () {
// 获取viewModel对象,通过对应类型的ViewModelProvider实例
var viewModel = getViewModel(lifecycleOwner, TestViewModelProvider());
viewModel.add();
}, onResume: () {
// 第二次获取viewModel对象,在这个lifecycleOwner中返回上次创建的对象
var viewModel = getViewModel(lifecycleOwner, TestViewModelProvider());
viewModel.add();
// 获取新的viewModel对象
var viewModel2 = getViewModel(lifecycleOwner, Test2ViewModelProvider());
viewModel2.add();
}));
lifecycleOwner.create();
lifecycleOwner.resume();
lifecycleOwner.pause();
lifecycleOwner.destroy();
var lifecycleOwner2 = TestLifecycleOwner();
lifecycleOwner2.lifecycle.addObserver(LifecycleObserver(onCreate: () {
// 在新的lifecycleOwner2中获取的viewModel为一个新对象
var viewModel = getViewModel(lifecycleOwner2, TestViewModelProvider());
viewModel.add();
}, onResume: () {
var viewModel = getViewModel(lifecycleOwner2, TestViewModelProvider());
viewModel.add();
}));
lifecycleOwner2.create();
lifecycleOwner2.resume();
lifecycleOwner2.pause();
lifecycleOwner2.destroy();
}
LiveData #
void main(){
var liveData = MutableLiveData<int>();
var lifecycleOwner = TestLifecycleOwner();
lifecycleOwner.lifecycle.addObserver(LifecycleObserver(onCreate: () {
liveData.of(lifecycleOwner).listen((value) {
print("liveData new value:$value");
});
}, onResume: () {
print("onResume");
}));
lifecycleOwner.create();
// 由于liveData基于[Stream]实现,所以这个测试只能打印一次值2
liveData.value = 2;
lifecycleOwner.resume();
liveData.value = 3;
liveData.value = 4;
lifecycleOwner.pause();
liveData.value = 5;
lifecycleOwner.destroy();
liveData.value = 6;
}
ChangeObservable #
// 该方法的签名与flutter中[State.setState]一致
var setState = (VoidCallback) {
print("change");
};
var changeObservable = ChangeObservable();
// 直接传入[setState]方法,简化代码量
changeObservable.of(lifecycleOwner).listen(setState);
// 通知变化
changeObservable.notify();
[0.1.6] - 2018/11/8
- 使用新dart语法实现mixin类
[0.1.5] - 2018/10/9
- 修改注释
[0.1.4] - 2018/9/19
- 增加
ChangeObservable
类
[0.1.3] - 2018/9/18
- 修改文档
[0.1.2] - 2018/9/18
- 修改文档
[0.1.1] - 2018/9/18
- 修改文档
[0.1.0] - 2018/9/18
- 首次发布,初步完成
Lifecycle
ViewModel
LiveData
核心组件
Use this package as a library
1. Depend on it
Add this to your package's pubspec.yaml file:
dependencies:
lifecycle_core: ^0.1.6
2. Install it
You can install packages from the command line:
with pub:
$ pub get
with Flutter:
$ flutter pub get
Alternatively, your editor might support pub get
or flutter pub get
.
Check the docs for your editor to learn more.
3. Import it
Now in your Dart code, you can use:
import 'package:lifecycle_core/lifecycle_core.dart';
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
15
|
Health:
Code health derived from static analysis.
[more]
|
98
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
42
|
Overall:
Weighted score of the above.
[more]
|
45
|
We analyzed this package on Dec 9, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
- Dart: 2.6.1
- pana: 0.12.21
Health suggestions
Fix lib/src/lifecycle.dart
. (-1.99 points)
Analysis of lib/src/lifecycle.dart
reported 4 hints:
line 69 col 8: Don't explicitly initialize variables to null.
line 70 col 7: Don't explicitly initialize variables to null.
line 71 col 7: Don't explicitly initialize variables to null.
line 72 col 7: Don't explicitly initialize variables to null.
Fix lib/src/live_data.dart
. (-0.50 points)
Analysis of lib/src/live_data.dart
reported 1 hint:
line 40 col 5: Don't explicitly initialize variables to null.
Maintenance issues and suggestions
The description contains too many non-ASCII characters. (-20 points)
The site uses English as its primary language. The value of the description
field in your package's pubspec.yaml
field should primarily contain characters used in English.
The package description is too short. (-20 points)
Add more detail to the description
field of pubspec.yaml
. Use 60 to 180 characters to describe the package, what it does, and its target use case.
Maintain an example. (-10 points)
Create a short demo in the example/
directory to show how to use this package.
Common filename patterns include main.dart
, example.dart
, and lifecycle_core.dart
. Packages with multiple examples should provide example/README.md
.
For more information see the pub package layout conventions.
Package is getting outdated. (-8.49 points)
The package was last published 56 weeks ago.