關(guān)于flutter生命周期澄惊,相信大家都有所了解紊馏,但從一個(gè)Android開(kāi)發(fā)過(guò)來(lái)的牺弄,完全不大適應(yīng)flutter的這種狀態(tài)姻几。好在寫寫demo或者搞些小東西用到的還不多,開(kāi)始也沒(méi)太在意势告。后來(lái)要開(kāi)發(fā)一個(gè)小應(yīng)用需要上線蛇捌,然后要加一些統(tǒng)計(jì)點(diǎn),于是把pub上別人集成的umeng統(tǒng)計(jì)加進(jìn)來(lái)咱台,但不幸的是并不能用其中的頁(yè)面統(tǒng)計(jì)络拌,因?yàn)橛衙私y(tǒng)計(jì)是在onResume和onPause時(shí)去統(tǒng)計(jì)一個(gè)頁(yè)面的情況的,但flutter并沒(méi)有這樣的方法回溺,只有initState和dispose,加在這兩個(gè)地方肯定是不準(zhǔn)確的了盒音,而且看開(kāi)發(fā)umeng插件的作者也說(shuō)Android端會(huì)統(tǒng)計(jì)不準(zhǔn)確。于是開(kāi)始研究有沒(méi)有方法也能像Android那樣明確的生命周期馅而。查看了不少實(shí)現(xiàn)方式,好在還是有辦法的譬圣,于是綜合各位的方式自己重新寫了個(gè)LifecycleState瓮恭,如果錯(cuò)誤,請(qǐng)大家指正厘熟。
lifecycle_state
github:https://github.com/tianyu704/lifecycle_state
pub:https://pub.dev/packages/lifecycle_state
A flutter package similar to Android's lifecycle.
給flutter添加了類似于Android生命周期的方法屯蹦,其中LifecycleState
適用于普通頁(yè)面维哈,只要把我們用的State替換成LifecycleState即可,LifecycleInnerState
適用于PageView中的頁(yè)面
Getting Started
導(dǎo)入lifecycle_state
lifecycle_state:
git:
url: "https://github.com/tianyu704/lifecycle_state.git"
or
lifecycle_state: ^0.0.1
MaterialApp中添加navigatorObservers: [routeObserver]
import 'package:lifecycle_state/lifecycle_state.dart';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Test',
navigatorObservers: [routeObserver],
...
);
}
routeObserver 是在lifecycle_state中定義的
頁(yè)面中的使用
- 直接繼承
LifecycleState
class TestPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _TestPageState();
}
}
class _TestPageState extends LifecycleState<TestPage> {
PageController _controller;
@override
void onCreate() {
// TODO: implement onCreate
super.onCreate();
_controller = PageController();
// log("onCreate");
}
@override
Widget build(BuildContext context) {
// TODO: implement buildWidget
return Scaffold(
body: PageView(
controller: _controller,
onPageChanged: (index) {
eventBus.fire(LifecycleInnerEvent(index, "tag"));
},
children: <Widget>[
ItemPage(0, "tag"),
ItemPage(1, "tag"),
ItemPage(2, "tag"),
],
),
floatingActionButton: FloatingActionButton(onPressed: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) {
return SecondPage();
}));
}),
);
}
@override
void onResume() {
// TODO: implement onResume
super.onResume();
log("onResume");
}
@override
void onPause() {
// TODO: implement onPause
super.onPause();
log("onPause");
}
@override
void onDestroy() {
// TODO: implement onDestroy
super.onDestroy();
log("onDestroy");
}
@override
void onLoadData() {
// TODO: implement onLoadData
super.onLoadData();
log("onLoadData");
}
@override
void onBackground() {
// TODO: implement onBackground
super.onBackground();
log("onBackground");
}
@override
void onForeground() {
// TODO: implement onForeground
super.onForeground();
log("onForeground");
}
}
- 如果要在PageView中使用,flutter默認(rèn)每次切換page登澜,舊的page是銷毀的阔挠,如果你的頁(yè)面可以這樣就可以直接用LifecycleState,但如果你不想每次都銷毀重建脑蠕,就用
LifecycleInnerState
购撼,但這個(gè)稍微麻煩的是得手動(dòng)通知頁(yè)面狀態(tài)變化,這里暫時(shí)使用event_bus
通信谴仙,就是在PageView的onPageChanged中導(dǎo)包并調(diào)用eventBus.fire(LifecycleInnerEvent(index, "tag"));
,"tag"是用來(lái)標(biāo)記每個(gè)pageview的迂求,避免一個(gè)pageview改變,通知到別的pageview晃跺。LifecycleInnerState
中加入了AutomaticKeepAliveClientMixin
防止頁(yè)面銷毀,你自己的類中就不要加了
import 'package:schulte_grid_flutter/src/lifecycle_inner_state.dart';
...
PageView(
controller: _controller,
onPageChanged: (index) {
eventBus.fire(LifecycleInnerEvent(index, "tag"));
},
children: <Widget>[
ItemPage(0),
ItemPage(1),
ItemPage(2),
],
),
...
class ItemPage extends StatefulWidget {
final int index;
ItemPage(this.index);
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _ItemPageState();
}
}
class _ItemPageState extends LifecycleInnerState<ItemPage> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
body: Center(
child: Text("${widget.index}"),
),
);
}
@override
int setPosition() {
// TODO: implement setPosition
return widget.index;
}
@override
void onLoadData() {
// TODO: implement onLoadData
super.onLoadData();
log("onLoadData");
}
@override
void onResume() {
// TODO: implement onResume
super.onResume();
log("onResume");
}
@override
void onPause() {
// TODO: implement onPause
super.onPause();
log("onPause");
}
@override
void onDestroy() {
// TODO: implement onDestroy
super.onDestroy();
log("onDestroy");
}
@override
void onCreate() {
// TODO: implement onCreate
super.onCreate();
log("onCreate");
}
@override
// TODO: implement wa
// ntKeepAlive
bool get wantKeepAlive => true;
@override
String setTag() {
// TODO: implement setTag
return "tag";
}
}
參考:
- LifecycleState參考于 https://github.com/tinyvampirepudge/flutter_lifecycle_state 揩局,但是他的這個(gè)我用著是有bug的,當(dāng)跳轉(zhuǎn)到繼承原生State的頁(yè)面中再回來(lái)時(shí)掀虎,方法調(diào)用會(huì)出錯(cuò)凌盯,有的生命周期方法會(huì)不調(diào)用了
- LifecycleInnerState參考于 https://github.com/385841539/flutter_BaseWidget 中的一部分,他這個(gè)有一個(gè)通用的頁(yè)面生命周期的基類烹玉,采用一個(gè)map記錄了所有頁(yè)面驰怎,然后通過(guò)判斷當(dāng)前頁(yè)面是棧頂或第二個(gè)來(lái)調(diào)用相應(yīng)的生命周期方法,因此你必須所有頁(yè)面都繼承他的基類春霍,如果跳轉(zhuǎn)到不繼承基類的頁(yè)面中那么那些方法調(diào)用就是錯(cuò)誤的
綜合這兩個(gè)的優(yōu)缺點(diǎn)自己改了一個(gè)版本砸西,在想用的時(shí)候就用LifecycleState
或fecycleInnerState
,不想用也沒(méi)關(guān)系,不影響用的頁(yè)面