From dbb1ffc83fd38fefef4caf27a740237b1153a71a Mon Sep 17 00:00:00 2001 From: chenenyu Date: Thu, 23 Sep 2021 16:25:02 +0800 Subject: [PATCH 1/2] v0.3.2 --- CHANGELOG.md | 5 ++ README.md | 8 +++- analysis_options.yaml | 1 + example/analysis_options.yaml | 1 + example/lib/home_page.dart | 26 +++++----- example/lib/main.dart | 18 +++---- example/lib/nav2_home.dart | 2 +- example/lib/nav2_page1.dart | 6 +-- example/lib/nav2_page2.dart | 6 +-- example/lib/nested_pageview.dart | 17 +++---- example/lib/overlay_log.dart | 8 ++-- example/lib/pageview.dart | 19 ++++---- example/lib/sub1_page.dart | 14 +++--- example/lib/sub2_page.dart | 14 +++--- example/lib/tabview.dart | 6 +-- example/pubspec.yaml | 1 + .../child_page_dispatch_lifecycle_mixin.dart | 4 +- lib/src/child_page_lifecycle_wrapper.dart | 2 +- .../child_page_subscribe_lifecycle_mixin.dart | 6 +-- lib/src/lifecycle_aware.dart | 4 +- lib/src/lifecycle_mixin.dart | 10 ++-- lib/src/lifecycle_observer.dart | 47 ++++++++++--------- lib/src/lifecycle_wrapper.dart | 2 +- lib/src/log.dart | 2 +- .../parent_page_dispatch_lifecycle_mixin.dart | 2 +- lib/src/parent_page_lifecycle_wrapper.dart | 11 +++-- ...parent_page_subscribe_lifecycle_mixin.dart | 10 ++-- lib/src/route_entry.dart | 4 +- pubspec.yaml | 3 ++ 29 files changed, 138 insertions(+), 121 deletions(-) create mode 100644 analysis_options.yaml create mode 100644 example/analysis_options.yaml diff --git a/CHANGELOG.md b/CHANGELOG.md index 20626ea..97f219f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## [0.3.2] - 2021/09/23. + +* NEW API: `findRoute(String routeName)`. +* Add `flutter_lints`. + ## [0.3.1] - 2021/09/01. * Bug fixes. diff --git a/README.md b/README.md index bf6f506..0ccfbee 100644 --- a/README.md +++ b/README.md @@ -310,7 +310,13 @@ class _NestedPageViewState extends State with SingleTickerProvid ``` -### Other API +### Other APIs + +* Find a route according to a specific name. + +``` +defaultLifecycleObserver.findRoute('route_name'); +``` * Remove a route according to a specific name. diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 0000000..f9b3034 --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1 @@ +include: package:flutter_lints/flutter.yaml diff --git a/example/analysis_options.yaml b/example/analysis_options.yaml new file mode 100644 index 0000000..f9b3034 --- /dev/null +++ b/example/analysis_options.yaml @@ -0,0 +1 @@ +include: package:flutter_lints/flutter.yaml diff --git a/example/lib/home_page.dart b/example/lib/home_page.dart index bba3a67..19799f2 100644 --- a/example/lib/home_page.dart +++ b/example/lib/home_page.dart @@ -4,7 +4,7 @@ import 'package:lifecycle/lifecycle.dart'; import 'overlay_log.dart'; class HomePage extends StatefulWidget { - HomePage({Key? key}) : super(key: key); + const HomePage({Key? key}) : super(key: key); @override _HomePageState createState() { @@ -33,7 +33,7 @@ class _HomePageState extends State Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: Text( + title: const Text( 'Home Page', ), ), @@ -41,41 +41,41 @@ class _HomePageState extends State child: Column( children: [ ElevatedButton( - child: Text('Open Sub1Page'), + child: const Text('Open Sub1Page'), onPressed: () { Navigator.of(context).pushNamed('sub1'); }, ), ElevatedButton( - child: Text('Open Sub2Page'), + child: const Text('Open Sub2Page'), onPressed: () { Navigator.of(context).pushNamed('sub2'); }, ), ElevatedButton( - child: Text('Open Dialog'), + child: const Text('Open Dialog'), onPressed: () { showDialog( context: context, - routeSettings: RouteSettings(name: 'dialog'), + routeSettings: const RouteSettings(name: 'dialog'), builder: (context) { return LifecycleWrapper( onLifecycleEvent: (event) { log.add('Dialog#${event.toString()}'); }, child: AlertDialog( - content: Text( + content: const Text( 'This is a dialog.', ), actions: [ TextButton( - child: Text('Dismiss'), + child: const Text('Dismiss'), onPressed: () { Navigator.of(context).pop(); }, ), TextButton( - child: Text('Open Sub1Page'), + child: const Text('Open Sub1Page'), onPressed: () { Navigator.of(context).pushNamed('sub1'); }, @@ -88,25 +88,25 @@ class _HomePageState extends State }, ), ElevatedButton( - child: Text('Open MyPageView'), + child: const Text('Open MyPageView'), onPressed: () { Navigator.of(context).pushNamed('pageview'); }, ), ElevatedButton( - child: Text('Open MyTabView'), + child: const Text('Open MyTabView'), onPressed: () { Navigator.of(context).pushNamed('tabview'); }, ), ElevatedButton( - child: Text('Open NestedPageView'), + child: const Text('Open NestedPageView'), onPressed: () { Navigator.of(context).pushNamed('nested'); }, ), ElevatedButton( - child: Text('Open Nav2.0'), + child: const Text('Open Nav2.0'), onPressed: () { Navigator.of(context).pushNamed('nav2'); }, diff --git a/example/lib/main.dart b/example/lib/main.dart index 50bdb97..a81a0b4 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -11,10 +11,12 @@ import 'sub2_page.dart'; import 'tabview.dart'; void main() { - runApp(MyApp()); + runApp(const MyApp()); } class MyApp extends StatelessWidget { + const MyApp({Key? key}) : super(key: key); + @override Widget build(BuildContext context) { return MaterialApp( @@ -25,17 +27,17 @@ class MyApp extends StatelessWidget { ), navigatorObservers: [defaultLifecycleObserver], routes: { - 'sub1': (_) => Sub1Page(), - 'sub2': (_) => Sub2Page(), - 'pageview': (_) => MyPageView(), - 'tabview': (_) => MyTabView(), - 'nested': (_) => NestedPageView(), - 'nav2': (_) => Nav2Home(), + 'sub1': (_) => const Sub1Page(), + 'sub2': (_) => const Sub2Page(), + 'pageview': (_) => const MyPageView(), + 'tabview': (_) => const MyTabView(), + 'nested': (_) => const NestedPageView(), + 'nav2': (_) => const Nav2Home(), }, home: Builder( builder: (context) { LogEntry.init(context); - return HomePage(); + return const HomePage(); }, ), ); diff --git a/example/lib/nav2_home.dart b/example/lib/nav2_home.dart index 04a4ac5..1cced4e 100644 --- a/example/lib/nav2_home.dart +++ b/example/lib/nav2_home.dart @@ -6,7 +6,7 @@ import 'nav2_page2.dart'; import 'overlay_log.dart'; class Nav2Home extends StatefulWidget { - Nav2Home({Key? key}) : super(key: key); + const Nav2Home({Key? key}) : super(key: key); @override _Nav2HomeState createState() { diff --git a/example/lib/nav2_page1.dart b/example/lib/nav2_page1.dart index eaa64a5..64573be 100644 --- a/example/lib/nav2_page1.dart +++ b/example/lib/nav2_page1.dart @@ -6,7 +6,7 @@ import 'overlay_log.dart'; class Nav2Page1 extends StatefulWidget { final VoidCallback onShowPage2; - Nav2Page1({Key? key, required this.onShowPage2}) : super(key: key); + const Nav2Page1({Key? key, required this.onShowPage2}) : super(key: key); @override _Nav2Page1State createState() { @@ -38,7 +38,7 @@ class _Nav2Page1State extends State Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: Text('Page1'), + title: const Text('Page1'), leading: BackButton( onPressed: () { Navigator.of(context, rootNavigator: true).maybePop(); @@ -48,7 +48,7 @@ class _Nav2Page1State extends State body: Center( child: TextButton( onPressed: widget.onShowPage2, - child: Text('Open Page2'), + child: const Text('Open Page2'), ), ), ); diff --git a/example/lib/nav2_page2.dart b/example/lib/nav2_page2.dart index 8380d1c..33a6dac 100644 --- a/example/lib/nav2_page2.dart +++ b/example/lib/nav2_page2.dart @@ -6,7 +6,7 @@ import 'overlay_log.dart'; class Nav2Page2 extends StatefulWidget { final VoidCallback onHidePage1; - Nav2Page2({Key? key, required this.onHidePage1}) : super(key: key); + const Nav2Page2({Key? key, required this.onHidePage1}) : super(key: key); @override _Nav2Page2State createState() { @@ -38,7 +38,7 @@ class _Nav2Page2State extends State Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: Text('Page2'), + title: const Text('Page2'), // leading: BackButton( // onPressed: () { // Navigator.of(context, rootNavigator: true).maybePop(); @@ -48,7 +48,7 @@ class _Nav2Page2State extends State body: Center( child: TextButton( onPressed: widget.onHidePage1, - child: Text('Hide Page1'), + child: const Text('Hide Page1'), ), ), ); diff --git a/example/lib/nested_pageview.dart b/example/lib/nested_pageview.dart index 770fe15..613b738 100644 --- a/example/lib/nested_pageview.dart +++ b/example/lib/nested_pageview.dart @@ -4,8 +4,9 @@ import 'package:lifecycle/lifecycle.dart'; import 'overlay_log.dart'; class NestedPageView extends StatefulWidget { - NestedPageView({Key? key}) : super(key: key); + const NestedPageView({Key? key}) : super(key: key); + @override _NestedPageViewState createState() => _NestedPageViewState(); } @@ -15,8 +16,8 @@ class _NestedPageViewState extends State late TabController _tabController; final List myTabs = [ - Tab(text: 'left'), - Tab(text: 'right'), + const Tab(text: 'left'), + const Tab(text: 'right'), ]; @override @@ -37,7 +38,7 @@ class _NestedPageViewState extends State Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: Text('NestedPageView'), + title: const Text('NestedPageView'), bottom: TabBar( controller: _tabController, tabs: myTabs, @@ -57,10 +58,10 @@ class _NestedPageViewState extends State log.add('OuterPage@0#${event.toString()}'); }, wantKeepAlive: true, - child: Center( + child: const Center( child: Text( 'This is the first tab', - style: const TextStyle(fontSize: 20), + style: TextStyle(fontSize: 20), ), ), ), @@ -97,7 +98,7 @@ class _NestedPageViewState extends State ); } }, - child: Text('Next'), + child: const Text('Next'), ), ), ), @@ -121,7 +122,7 @@ class _NestedPageViewState extends State ); } }, - child: Text('Previous'), + child: const Text('Previous'), ), ), ), diff --git a/example/lib/overlay_log.dart b/example/lib/overlay_log.dart index f9a8f6f..4727df8 100644 --- a/example/lib/overlay_log.dart +++ b/example/lib/overlay_log.dart @@ -24,7 +24,7 @@ class LogEntry extends StatefulWidget { } class LogEntryState extends State { - ScrollController _controller = ScrollController(); + final ScrollController _controller = ScrollController(); @override void initState() { @@ -63,13 +63,13 @@ class LogEntryState extends State { ListView.builder( controller: _controller, shrinkWrap: true, - padding: EdgeInsets.all(16), + padding: const EdgeInsets.all(16), itemCount: _logs.length, itemBuilder: (context, index) { final String log = _logs[index]; return Text( log, - style: TextStyle( + style: const TextStyle( color: Colors.white70, fontSize: 14, ), @@ -82,7 +82,7 @@ class LogEntryState extends State { child: IconButton( iconSize: 16, padding: EdgeInsets.zero, - icon: Icon( + icon: const Icon( Icons.clear, color: Colors.white, ), diff --git a/example/lib/pageview.dart b/example/lib/pageview.dart index cf0e410..df728f5 100644 --- a/example/lib/pageview.dart +++ b/example/lib/pageview.dart @@ -4,8 +4,9 @@ import 'package:lifecycle/lifecycle.dart'; import 'overlay_log.dart'; class MyPageView extends StatefulWidget { - MyPageView({Key? key}) : super(key: key); + const MyPageView({Key? key}) : super(key: key); + @override _MyPageViewState createState() => _MyPageViewState(); } @@ -28,7 +29,7 @@ class _MyPageViewState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: Text('MyPageView'), + title: const Text('MyPageView'), ), body: ParentPageLifecycleWrapper( controller: _pageController, @@ -59,31 +60,31 @@ class _MyPageViewState extends State { ); } }, - child: Text('Next'), + child: const Text('Next'), ), ElevatedButton( onPressed: () { showDialog( context: context, - routeSettings: RouteSettings(name: 'dialog'), + routeSettings: const RouteSettings(name: 'dialog'), builder: (context) { return LifecycleWrapper( onLifecycleEvent: (event) { log.add('Dialog#${event.toString()}'); }, child: AlertDialog( - content: Text( + content: const Text( 'This is a dialog.', ), actions: [ TextButton( - child: Text('Dismiss'), + child: const Text('Dismiss'), onPressed: () { Navigator.of(context).pop(); }, ), TextButton( - child: Text('Open Sub1Page'), + child: const Text('Open Sub1Page'), onPressed: () { Navigator.of(context).pushNamed('sub1'); }, @@ -94,7 +95,7 @@ class _MyPageViewState extends State { }, ); }, - child: Text('Open dialog'), + child: const Text('Open dialog'), ), ], ), @@ -122,7 +123,7 @@ class _MyPageViewState extends State { ); } }, - child: Text('Previous'), + child: const Text('Previous'), ), ], ), diff --git a/example/lib/sub1_page.dart b/example/lib/sub1_page.dart index f67cd38..5bcf8f4 100644 --- a/example/lib/sub1_page.dart +++ b/example/lib/sub1_page.dart @@ -4,7 +4,7 @@ import 'package:lifecycle/lifecycle.dart'; import 'overlay_log.dart'; class Sub1Page extends StatefulWidget { - Sub1Page({Key? key}) : super(key: key); + const Sub1Page({Key? key}) : super(key: key); @override _Sub1PageState createState() { @@ -33,7 +33,7 @@ class _Sub1PageState extends State Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: Text( + title: const Text( 'Sub1Page', ), ), @@ -44,31 +44,31 @@ class _Sub1PageState extends State onPressed: () { Navigator.of(context).pop(); }, - child: Text("pop()"), + child: const Text("pop()"), ), ElevatedButton( onPressed: () { Navigator.of(context).popUntil(ModalRoute.withName('/')); }, - child: Text("popUntil('/')"), + child: const Text("popUntil('/')"), ), ElevatedButton( onPressed: () { Navigator.of(context).pushNamed('sub2'); }, - child: Text("push('sub2')"), + child: const Text("push('sub2')"), ), ElevatedButton( onPressed: () { Navigator.of(context).popAndPushNamed('sub2'); }, - child: Text("popAndPushNamed('sub2')"), + child: const Text("popAndPushNamed('sub2')"), ), ElevatedButton( onPressed: () { Navigator.of(context).pushReplacementNamed('sub2'); }, - child: Text("pushReplacementNamed('sub2')"), + child: const Text("pushReplacementNamed('sub2')"), ), ], ), diff --git a/example/lib/sub2_page.dart b/example/lib/sub2_page.dart index facacb2..58501ae 100644 --- a/example/lib/sub2_page.dart +++ b/example/lib/sub2_page.dart @@ -4,7 +4,7 @@ import 'package:lifecycle/lifecycle.dart'; import 'overlay_log.dart'; class Sub2Page extends StatelessWidget { - Sub2Page({Key? key}) : super(key: key); + const Sub2Page({Key? key}) : super(key: key); @override Widget build(BuildContext context) { @@ -14,7 +14,7 @@ class Sub2Page extends StatelessWidget { }, child: Scaffold( appBar: AppBar( - title: Text( + title: const Text( 'Sub2Page', ), ), @@ -25,34 +25,34 @@ class Sub2Page extends StatelessWidget { onPressed: () { Navigator.of(context).pop(); }, - child: Text("pop()"), + child: const Text("pop()"), ), ElevatedButton( onPressed: () { Navigator.of(context).removeRoute(ModalRoute.of(context)!); }, - child: Text("removeRoute(current)"), + child: const Text("removeRoute(current)"), ), ElevatedButton( onPressed: () { Navigator.of(context) .removeRouteBelow(ModalRoute.of(context)!); }, - child: Text("removeRouteBelow(current)"), + child: const Text("removeRouteBelow(current)"), ), ElevatedButton( onPressed: () { Navigator.of(context).pushNamedAndRemoveUntil( 'sub1', ModalRoute.withName('/')); }, - child: Text( + child: const Text( "pushNamedAndRemoveUntil('sub1', ModalRoute.withName('/'))"), ), ElevatedButton( onPressed: () { defaultLifecycleObserver.removeNamed('sub1'); }, - child: Text("removeNamed('sub1')"), + child: const Text("removeNamed('sub1')"), ), ], ), diff --git a/example/lib/tabview.dart b/example/lib/tabview.dart index 0f3823e..d1a96ac 100644 --- a/example/lib/tabview.dart +++ b/example/lib/tabview.dart @@ -13,8 +13,8 @@ class MyTabView extends StatefulWidget { class _MyTabViewState extends State with SingleTickerProviderStateMixin { final List myTabs = [ - Tab(text: 'LEFT'), - Tab(text: 'RIGHT'), + const Tab(text: 'LEFT'), + const Tab(text: 'RIGHT'), ]; late TabController _tabController; @@ -35,7 +35,7 @@ class _MyTabViewState extends State Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: Text('MyTabView'), + title: const Text('MyTabView'), bottom: TabBar( controller: _tabController, tabs: myTabs, diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 6bb527c..823dc36 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -29,6 +29,7 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter + flutter_lints: ^1.0.4 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec diff --git a/lib/src/child_page_dispatch_lifecycle_mixin.dart b/lib/src/child_page_dispatch_lifecycle_mixin.dart index d45d078..a45ea82 100644 --- a/lib/src/child_page_dispatch_lifecycle_mixin.dart +++ b/lib/src/child_page_dispatch_lifecycle_mixin.dart @@ -24,8 +24,8 @@ mixin ChildPageDispatchLifecycleMixin /// Dispatch [events] to subscribers. void dispatchEvents(List events) { - _lifecycleSubscribers.forEach((lifecycleAware) { + for (LifecycleAware lifecycleAware in _lifecycleSubscribers) { lifecycleAware.handleLifecycleEvents(events); - }); + } } } diff --git a/lib/src/child_page_lifecycle_wrapper.dart b/lib/src/child_page_lifecycle_wrapper.dart index 1046920..b52b4af 100644 --- a/lib/src/child_page_lifecycle_wrapper.dart +++ b/lib/src/child_page_lifecycle_wrapper.dart @@ -13,7 +13,7 @@ class ChildPageLifecycleWrapper extends StatefulWidget { final bool wantKeepAlive; final Widget child; - ChildPageLifecycleWrapper({ + const ChildPageLifecycleWrapper({ Key? key, required this.index, this.onLifecycleEvent, diff --git a/lib/src/child_page_subscribe_lifecycle_mixin.dart b/lib/src/child_page_subscribe_lifecycle_mixin.dart index 7926a40..33cd27c 100644 --- a/lib/src/child_page_subscribe_lifecycle_mixin.dart +++ b/lib/src/child_page_subscribe_lifecycle_mixin.dart @@ -21,10 +21,8 @@ mixin ChildPageSubscribeLifecycleMixin super.didChangeDependencies(); final route = ModalRoute.of(context); if (route == null) return; - if (_parentPageLifecycleWrapperState == null) { - _parentPageLifecycleWrapperState = - ParentPageLifecycleWrapper.maybeOf(context); - } + _parentPageLifecycleWrapperState ??= + ParentPageLifecycleWrapper.maybeOf(context); _parentPageLifecycleWrapperState?.subscribe(widget.index, this); } diff --git a/lib/src/lifecycle_aware.dart b/lib/src/lifecycle_aware.dart index 6b975a3..ffc31de 100644 --- a/lib/src/lifecycle_aware.dart +++ b/lib/src/lifecycle_aware.dart @@ -35,12 +35,12 @@ enum LifecycleEvent { pop, } -const List lifecycle_events_visible_and_active = [ +const List lifecycleEventsVisibleAndActive = [ LifecycleEvent.visible, LifecycleEvent.active, ]; -const List lifecycle_events_inactive_and_invisible = [ +const List lifecycleEventsInactiveAndInvisible = [ LifecycleEvent.inactive, LifecycleEvent.invisible, ]; diff --git a/lib/src/lifecycle_mixin.dart b/lib/src/lifecycle_mixin.dart index 13eaf3b..cef5115 100644 --- a/lib/src/lifecycle_mixin.dart +++ b/lib/src/lifecycle_mixin.dart @@ -21,13 +21,9 @@ mixin LifecycleMixin on State, LifecycleAware { super.didChangeDependencies(); final route = ModalRoute.of(context); if (route == null) return; - if (_lifecycleObserver == null) { - _lifecycleObserver = LifecycleObserver.internalGet(context); - } - if (_childPageLifecycleWrapperState == null) { - _childPageLifecycleWrapperState = - ChildPageLifecycleWrapper.maybeOf(context); - } + _lifecycleObserver ??= LifecycleObserver.internalGet(context); + _childPageLifecycleWrapperState ??= + ChildPageLifecycleWrapper.maybeOf(context); if (_childPageLifecycleWrapperState != null) { _childPageLifecycleWrapperState!.subscribe(this); diff --git a/lib/src/lifecycle_observer.dart b/lib/src/lifecycle_observer.dart index 2e73773..981b8db 100644 --- a/lib/src/lifecycle_observer.dart +++ b/lib/src/lifecycle_observer.dart @@ -1,5 +1,4 @@ import 'package:flutter/widgets.dart'; -import 'package:lifecycle/lifecycle.dart'; import 'lifecycle_aware.dart'; import 'log.dart'; @@ -54,7 +53,7 @@ class LifecycleObserver extends NavigatorObserver with WidgetsBindingObserver { } if (entry.lifecycleSubscribers.add(lifecycleAware)) { log('LifecycleObserver($hashCode)#subscribe (${lifecycleAware.toString()})'); - entry.emitEvents(lifecycleAware, lifecycle_events_visible_and_active); + entry.emitEvents(lifecycleAware, lifecycleEventsVisibleAndActive); } } @@ -78,7 +77,7 @@ class LifecycleObserver extends NavigatorObserver with WidgetsBindingObserver { switch (state) { case AppLifecycleState.resumed: // active // Top route trigger active - _sendEventsToLastRoute(lifecycle_events_visible_and_active); + _sendEventsToLastRoute(lifecycleEventsVisibleAndActive); if (_history.last.route is! PageRoute) { // Previous PageRoute trigger visible _sendEventsToLastPageRoute([LifecycleEvent.visible]); @@ -115,7 +114,7 @@ class LifecycleObserver extends NavigatorObserver with WidgetsBindingObserver { if (route is PageRoute) { // Previous route trigger invisible _sendEventsToGivenRoute( - previousEntry, lifecycle_events_inactive_and_invisible); + previousEntry, lifecycleEventsInactiveAndInvisible); if (previousRoute is PopupRoute) { // Previous PageRoute trigger invisible _sendEventsToLastPageRoute([LifecycleEvent.invisible]); @@ -141,15 +140,14 @@ class LifecycleObserver extends NavigatorObserver with WidgetsBindingObserver { RouteEntry entry = _getRouteEntry(route); // Current route trigger pop - _sendEventsToGivenRoute(entry, lifecycle_events_inactive_and_invisible); + _sendEventsToGivenRoute(entry, lifecycleEventsInactiveAndInvisible); _history.remove(entry); if (previousRoute != null) { RouteEntry previousEntry = _getRouteEntry(previousRoute); if (route is PageRoute) { // Previous Route trigger active - _sendEventsToGivenRoute( - previousEntry, lifecycle_events_visible_and_active); + _sendEventsToGivenRoute(previousEntry, lifecycleEventsVisibleAndActive); if (previousRoute is PopupRoute) { // Previous PageRoute trigger visible _sendEventsToLastPageRoute([LifecycleEvent.visible]); @@ -175,7 +173,7 @@ class LifecycleObserver extends NavigatorObserver with WidgetsBindingObserver { 'newRoute: ${newRoute.settings.name}, ' 'oldRoute: ${oldRoute.settings.name}, isLast: $isLast)'); - _sendEventsToGivenRoute(oldEntry, lifecycle_events_inactive_and_invisible); + _sendEventsToGivenRoute(oldEntry, lifecycleEventsInactiveAndInvisible); _history.remove(oldEntry); if (isLast) { @@ -185,7 +183,7 @@ class LifecycleObserver extends NavigatorObserver with WidgetsBindingObserver { } else if (oldRoute is PopupRoute && newRoute is PageRoute) { // todo: Previous PopupRoute trigger invisible ? // Previous PageRoute trigger invisible - _sendEventsToLastPageRoute(lifecycle_events_inactive_and_invisible); + _sendEventsToLastPageRoute(lifecycleEventsInactiveAndInvisible); } } @@ -193,7 +191,8 @@ class LifecycleObserver extends NavigatorObserver with WidgetsBindingObserver { } /// [route] route being removed. - /// [previousRoute] 被移除route下面的route,移除多个route时,该参数值不变, 可能为null + /// [previousRoute] A route below the removed route. When removing + /// multiple routes, the [previousRoute] is unchanged, and may be null. @override void didRemove(Route route, Route? previousRoute) { super.didRemove(route, previousRoute); @@ -202,11 +201,10 @@ class LifecycleObserver extends NavigatorObserver with WidgetsBindingObserver { 'previousRoute: ${previousRoute?.settings.name})'); RouteEntry entry = _getRouteEntry(route); - _sendEventsToGivenRoute(entry, lifecycle_events_inactive_and_invisible); + _sendEventsToGivenRoute(entry, lifecycleEventsInactiveAndInvisible); if (previousRoute?.isCurrent ?? false) { RouteEntry previousEntry = _getRouteEntry(previousRoute!); - _sendEventsToGivenRoute( - previousEntry, lifecycle_events_visible_and_active); + _sendEventsToGivenRoute(previousEntry, lifecycleEventsVisibleAndActive); } _history.remove(entry); @@ -236,15 +234,22 @@ class LifecycleObserver extends NavigatorObserver with WidgetsBindingObserver { } } + Route? findRoute(String routeName) { + try { + Route route = + _history.firstWhere((r) => r.route.settings.name == routeName).route; + return route; + } catch (_) { + return null; + } + } + /// Remove a route according to the [routeName]. void removeNamed(String routeName, [T? result]) { - // try { - Route route = - _history.firstWhere((r) => r.route.settings.name == routeName).route; - route.didPop(result); - navigator?.removeRoute(route); - // } catch (e) { - // log(e); - // } + Route? route = findRoute(routeName); + if (route != null) { + route.didPop(result); + navigator?.removeRoute(route); + } } } diff --git a/lib/src/lifecycle_wrapper.dart b/lib/src/lifecycle_wrapper.dart index 870b4d1..1fc6b14 100644 --- a/lib/src/lifecycle_wrapper.dart +++ b/lib/src/lifecycle_wrapper.dart @@ -9,7 +9,7 @@ class LifecycleWrapper extends StatefulWidget { final OnLifecycleEvent onLifecycleEvent; final Widget child; - LifecycleWrapper({ + const LifecycleWrapper({ Key? key, required this.onLifecycleEvent, required this.child, diff --git a/lib/src/log.dart b/lib/src/log.dart index f72e472..8d93435 100644 --- a/lib/src/log.dart +++ b/lib/src/log.dart @@ -2,6 +2,6 @@ bool _enable = false; void log(Object object) { if (_enable == true) { - print(object); + print(object); // ignore: avoid_print } } diff --git a/lib/src/parent_page_dispatch_lifecycle_mixin.dart b/lib/src/parent_page_dispatch_lifecycle_mixin.dart index b7cb121..bbbb7b7 100644 --- a/lib/src/parent_page_dispatch_lifecycle_mixin.dart +++ b/lib/src/parent_page_dispatch_lifecycle_mixin.dart @@ -20,7 +20,7 @@ mixin ParentPageDispatchLifecycleMixin // Dispatch [LifecycleEvent.active] to initial page. if (curPage == index) { if (ModalRoute.of(context)!.isCurrent) { - dispatchEvents(lifecycle_events_visible_and_active); + dispatchEvents(lifecycleEventsVisibleAndActive); } else { dispatchEvents([LifecycleEvent.visible]); } diff --git a/lib/src/parent_page_lifecycle_wrapper.dart b/lib/src/parent_page_lifecycle_wrapper.dart index 6d5188b..8f6d106 100644 --- a/lib/src/parent_page_lifecycle_wrapper.dart +++ b/lib/src/parent_page_lifecycle_wrapper.dart @@ -11,7 +11,7 @@ class ParentPageLifecycleWrapper extends StatefulWidget { final OnLifecycleEvent? onLifecycleEvent; final Widget child; - ParentPageLifecycleWrapper({ + const ParentPageLifecycleWrapper({ Key? key, required this.controller, this.onLifecycleEvent, @@ -19,6 +19,7 @@ class ParentPageLifecycleWrapper extends StatefulWidget { }) : assert(controller is PageController || controller is TabController), super(key: key); + // ignore_for_file: no_logic_in_create_state @override ParentPageLifecycleWrapperState createState() { if (controller is PageController) { @@ -83,11 +84,11 @@ class _PageViewLifecycleWrapperState extends ParentPageLifecycleWrapperState { int page = _pageController.page!.round(); if (curPage == page) return; // log('PageController#onPageChanged: from page[$curPage]'); - dispatchEvents(lifecycle_events_inactive_and_invisible); + dispatchEvents(lifecycleEventsInactiveAndInvisible); curPage = page; // log('PageController#onPageChanged: to page[$curPage]'); if (ModalRoute.of(context)?.isCurrent == true) { - dispatchEvents(lifecycle_events_visible_and_active); + dispatchEvents(lifecycleEventsVisibleAndActive); } else { dispatchEvents([LifecycleEvent.visible]); } @@ -109,11 +110,11 @@ class _TabBarViewLifecycleWrapperState extends ParentPageLifecycleWrapperState { int page = _tabController.index; if (curPage == page) return; // log('TabController#onPageChanged: from page[$curPage]'); - dispatchEvents(lifecycle_events_inactive_and_invisible); + dispatchEvents(lifecycleEventsInactiveAndInvisible); curPage = page; // log('TabController#onPageChanged: to page[$curPage]'); if (ModalRoute.of(context)?.isCurrent == true) { - dispatchEvents(lifecycle_events_visible_and_active); + dispatchEvents(lifecycleEventsVisibleAndActive); } else { dispatchEvents([LifecycleEvent.visible]); } diff --git a/lib/src/parent_page_subscribe_lifecycle_mixin.dart b/lib/src/parent_page_subscribe_lifecycle_mixin.dart index 8e66d71..cdad516 100644 --- a/lib/src/parent_page_subscribe_lifecycle_mixin.dart +++ b/lib/src/parent_page_subscribe_lifecycle_mixin.dart @@ -26,13 +26,9 @@ mixin ParentPageSubscribeLifecycleMixin final ModalRoute? route = ModalRoute.of(context); if (route == null) return; - if (_lifecycleObserver == null) { - _lifecycleObserver = LifecycleObserver.internalGet(context); - } - if (_childPageLifecycleWrapperState == null) { - _childPageLifecycleWrapperState = - ChildPageLifecycleWrapper.maybeOf(context); - } + _lifecycleObserver ??= LifecycleObserver.internalGet(context); + _childPageLifecycleWrapperState ??= + ChildPageLifecycleWrapper.maybeOf(context); if (_childPageLifecycleWrapperState != null) { _childPageLifecycleWrapperState?.subscribe(this); diff --git a/lib/src/route_entry.dart b/lib/src/route_entry.dart index bb612af..58dbd44 100644 --- a/lib/src/route_entry.dart +++ b/lib/src/route_entry.dart @@ -10,9 +10,9 @@ class RouteEntry { final Set lifecycleSubscribers = {}; void emitEventsToSubscribers(List events) { - lifecycleSubscribers.forEach((lifecycleAware) { + for (LifecycleAware lifecycleAware in lifecycleSubscribers) { emitEvents(lifecycleAware, events); - }); + } } void emitEvents(LifecycleAware lifecycleAware, List events) { diff --git a/pubspec.yaml b/pubspec.yaml index 597eb18..8209621 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,3 +10,6 @@ environment: dependencies: flutter: sdk: flutter + +dev_dependencies: + flutter_lints: ^1.0.4 From 50c3b5f4cfcff951ee63edad8f8622c3a943ece0 Mon Sep 17 00:00:00 2001 From: chenenyu Date: Thu, 23 Sep 2021 16:26:22 +0800 Subject: [PATCH 2/2] release 0.3.2 --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 8209621..003f9d7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: lifecycle description: Lifecycle support for Flutter widgets. -version: 0.3.1 +version: 0.3.2 homepage: https://github.com/chenenyu/lifecycle publish_to: https://pub.dev