關於flutter生命週期,相信你們都有所瞭解,但從一個Android開發過來的,徹底不大適應flutter的這種狀態。好在寫寫demo或者搞些小東西用到的還很少,開始也沒太在乎。後來要開發一個小應用須要上線,而後要加一些統計點,因而把pub上別人集成的umeng統計加進來,但不幸的是並不能用其中的頁面統計,由於友盟統計是在onResume和onPause時去統計一個頁面的狀況的,但flutter並無這樣的方法,只有initState和dispose,加在這兩個地方確定是不許確的了,並且看開發umeng插件的做者也說Android端會統計不許確。因而開始研究有沒有方法也能像Android那樣明確的生命週期。查看了很多實現方式,好在仍是有辦法的,因而綜合各位的方式本身從新寫了個LifecycleState,若是錯誤,請你們指正。git
github:github.com/tianyu704/l…
pub:pub.dev/packages/li…github
A flutter package similar to Android's lifecycle.
給flutter添加了相似於Android生命週期的方法,其中LifecycleState
適用於普通頁面,只要把咱們用的State替換成LifecycleState便可,LifecycleInnerState
適用於PageView中的頁面bash
lifecycle_state
lifecycle_state:
git:
url: "https://github.com/tianyu704/lifecycle_state.git"
複製代碼
oride
lifecycle_state: ^0.0.1
複製代碼
navigatorObservers: [routeObserver]
import 'package:lifecycle_state/lifecycle_state.dart';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Test',
navigatorObservers: [routeObserver],
...
);
}
複製代碼
routeObserver 是在lifecycle_state中定義的ui
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");
}
}
複製代碼
LifecycleInnerState
,但這個稍微麻煩的是得手動通知頁面狀態變化,這裏暫時使用event_bus
通訊,就是在PageView的onPageChanged中導包並調用eventBus.fire(LifecycleInnerEvent(index, "tag"));
,"tag"是用來標記每一個pageview的,避免一個pageview改變,通知到別的pageview。LifecycleInnerState
中加入了AutomaticKeepAliveClientMixin
防止頁面銷燬,你本身的類中就不要加了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";
}
}
複製代碼
參考:this
綜合這兩個的優缺點本身改了一個版本,在想用的時候就用LifecycleState
或fecycleInnerState
,不想用也不要緊,不影響用的頁面url