Skip to content

Commit 4dc905c

Browse files
committed
fix lifecycle in nested page view.
1 parent d9269eb commit 4dc905c

7 files changed

+61
-43
lines changed

lib/src/dispatch_lifecycle_to_parent_page_mixin.dart renamed to lib/src/child_page_dispatch_lifecycle_mixin.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'child_page_lifecycle_wrapper.dart';
44
import 'lifecycle_aware.dart';
55

66
/// Lifecycle dispatcher for child page.
7-
mixin DispatchLifecycleToParentPageMixin
7+
mixin ChildPageDispatchLifecycleMixin
88
on State<ChildPageLifecycleWrapper>, LifecycleAware {
99
Set<LifecycleAware> _subscribers = <LifecycleAware>{};
1010

lib/src/child_page_lifecycle_wrapper.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import 'package:flutter/material.dart';
22

3-
import 'dispatch_lifecycle_to_parent_page_mixin.dart';
3+
import 'child_page_dispatch_lifecycle_mixin.dart';
4+
import 'child_page_subscribe_lifecycle_mixin.dart';
45
import 'lifecycle_aware.dart';
56
import 'parent_page_lifecycle_wrapper.dart';
6-
import 'subscribe_lifecycle_from_parent_page_mixin.dart';
77

88
/// Lifecycle wrapper for children of [PageView] and [TabBarView].
99
/// See [ParentPageLifecycleWrapper].
@@ -37,8 +37,8 @@ class ChildPageLifecycleWrapper extends StatefulWidget {
3737
class ChildPageLifecycleWrapperState extends State<ChildPageLifecycleWrapper>
3838
with
3939
LifecycleAware,
40-
DispatchLifecycleToParentPageMixin,
41-
SubscribeLifecycleFromParentPageMixin,
40+
ChildPageDispatchLifecycleMixin,
41+
ChildPageSubscribeLifecycleMixin,
4242
AutomaticKeepAliveClientMixin {
4343
@override
4444
bool get wantKeepAlive => widget.wantKeepAlive ?? false;

lib/src/subscribe_lifecycle_from_parent_page_mixin.dart renamed to lib/src/child_page_subscribe_lifecycle_mixin.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'parent_page_lifecycle_wrapper.dart';
66

77
/// Subscribe lifecycle event from [ParentPageLifecycleWrapper].
88
/// This is used in child page of PageView.
9-
mixin SubscribeLifecycleFromParentPageMixin
9+
mixin ChildPageSubscribeLifecycleMixin
1010
on State<ChildPageLifecycleWrapper>, LifecycleAware {
1111
ParentPageLifecycleWrapperState _basePageViewLifecycleWrapperState;
1212

lib/src/dispatch_lifecycle_to_child_page_mixin.dart renamed to lib/src/parent_page_dispatch_lifecycle_mixin.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'lifecycle_aware.dart';
44
import 'parent_page_lifecycle_wrapper.dart';
55

66
/// Dispatch lifecycle event to child page.
7-
mixin DispatchLifecycleToChildPageMixin
7+
mixin ParentPageDispatchLifecycleMixin
88
on State<ParentPageLifecycleWrapper>, LifecycleAware {
99
/// Current page.
1010
int curPage;
@@ -24,6 +24,8 @@ mixin DispatchLifecycleToChildPageMixin
2424

2525
/// Dispatch event to stream subscription
2626
void dispatchEvent(LifecycleEvent event) {
27-
_subscribers[curPage].onLifecycleEvent(event);
27+
if (_subscribers[curPage] != null) {
28+
_subscribers[curPage].onLifecycleEvent(event);
29+
}
2830
}
2931
}

lib/src/parent_page_lifecycle_wrapper.dart

+16-10
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import 'dart:async';
22

33
import 'package:flutter/material.dart';
44

5-
import 'dispatch_lifecycle_to_child_page_mixin.dart';
65
import 'lifecycle_aware.dart';
7-
import 'lifecycle_mixin.dart';
8-
import 'subscribe_lifecycle_from_child_page_mixin.dart';
6+
import 'parent_page_dispatch_lifecycle_mixin.dart';
7+
import 'parent_page_subscribe_lifecycle_mixin.dart';
98

109
/// Lifecycle wrapper for [PageView]/[TabBarView].
1110
class ParentPageLifecycleWrapper extends StatefulWidget {
@@ -44,9 +43,8 @@ abstract class ParentPageLifecycleWrapperState
4443
extends State<ParentPageLifecycleWrapper>
4544
with
4645
LifecycleAware,
47-
LifecycleMixin,
48-
DispatchLifecycleToChildPageMixin,
49-
SubscribeLifecycleFromChildPageMixin {
46+
ParentPageDispatchLifecycleMixin,
47+
ParentPageSubscribeLifecycleMixin {
5048
void onPageChanged();
5149

5250
@override
@@ -95,8 +93,12 @@ class _PageViewLifecycleWrapperState extends ParentPageLifecycleWrapperState {
9593
// print('_PageViewLifecycleWrapperState#initState');
9694
_pageController = widget.controller;
9795
curPage = _pageController.initialPage;
98-
// 补发第一个page的 active 事件
99-
Future.microtask(() => dispatchEvent(LifecycleEvent.active));
96+
// 补发第一个page的 active 事件, 异步是让 active 事件在 push 事件之后
97+
Future.microtask(() {
98+
if (widget.onLifecycleEvent != null) {
99+
widget.onLifecycleEvent(LifecycleEvent.active);
100+
}
101+
});
100102
}
101103

102104
/// 页面切换监听
@@ -119,8 +121,12 @@ class _TabBarViewLifecycleWrapperState extends ParentPageLifecycleWrapperState {
119121
// print('_TabBarViewLifecycleWrapperState#initState');
120122
_tabController = widget.controller;
121123
curPage = _tabController.index;
122-
// 补发第一个page的 active 事件
123-
Future.microtask(() => dispatchEvent(LifecycleEvent.active));
124+
// 补发第一个page的 active 事件, 异步是让 active 事件在 push 事件之后
125+
Future.microtask(() {
126+
if (widget.onLifecycleEvent != null) {
127+
widget.onLifecycleEvent(LifecycleEvent.active);
128+
}
129+
});
124130
}
125131

126132
@override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import 'package:flutter/widgets.dart';
2+
3+
import 'child_page_lifecycle_wrapper.dart';
4+
import 'lifecycle_aware.dart';
5+
import 'lifecycle_observer.dart';
6+
import 'lifecycle_observer_provider.dart';
7+
import 'parent_page_lifecycle_wrapper.dart';
8+
9+
/// Subscribe lifecycle event from [ChildPageLifecycleWrapper] or [LifecycleObserver].
10+
mixin ParentPageSubscribeLifecycleMixin
11+
on State<ParentPageLifecycleWrapper>, LifecycleAware {
12+
ChildPageLifecycleWrapperState _pageLifecycleWrapperState;
13+
LifecycleObserver _lifecycleObserver;
14+
15+
@override
16+
void didChangeDependencies() {
17+
super.didChangeDependencies();
18+
_pageLifecycleWrapperState = ChildPageLifecycleWrapper.of(context);
19+
if (_pageLifecycleWrapperState != null) {
20+
// 如果是嵌套的PageView,则从上层Page订阅event
21+
_pageLifecycleWrapperState.subscribe(this);
22+
} else {
23+
// 如果不是嵌套的PageView,则从LifecycleObserver订阅event
24+
_lifecycleObserver = LifecycleObserverProvider.of(context);
25+
_lifecycleObserver.subscribe(this, ModalRoute.of(context));
26+
}
27+
}
28+
29+
@override
30+
void dispose() {
31+
_pageLifecycleWrapperState?.unsubscribe(this);
32+
_lifecycleObserver?.unsubscribe(this);
33+
super.dispose();
34+
}
35+
}

lib/src/subscribe_lifecycle_from_child_page_mixin.dart

-25
This file was deleted.

0 commit comments

Comments
 (0)