From 17ed4e0b3af6d995686873e6b2f47948dbb5917f Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Mon, 8 Jun 2020 16:18:02 -0700 Subject: [PATCH] Introduce inherited navigator observer and refactor hero controller (#58808) --- packages/flutter/lib/src/cupertino/app.dart | 100 ++++++------- packages/flutter/lib/src/material/app.dart | 137 ++++++++---------- .../flutter/lib/src/widgets/navigator.dart | 117 ++++++++++++--- .../flutter/test/material/debug_test.dart | 2 + .../flutter/test/widgets/navigator_test.dart | 81 +++++++++++ 5 files changed, 288 insertions(+), 149 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/app.dart b/packages/flutter/lib/src/cupertino/app.dart index 46b4a79d7b..20286e0e4b 100644 --- a/packages/flutter/lib/src/cupertino/app.dart +++ b/packages/flutter/lib/src/cupertino/app.dart @@ -289,7 +289,6 @@ class _CupertinoAppState extends State { void initState() { super.initState(); _heroController = CupertinoApp.createCupertinoHeroController(); - _updateNavigator(); } @override @@ -302,21 +301,6 @@ class _CupertinoAppState extends State { // Navigator has a GlobalKey). _heroController = CupertinoApp.createCupertinoHeroController(); } - _updateNavigator(); - } - - List _navigatorObservers; - - void _updateNavigator() { - if (widget.home != null || - widget.routes.isNotEmpty || - widget.onGenerateRoute != null || - widget.onUnknownRoute != null) { - _navigatorObservers = List.from(widget.navigatorObservers) - ..add(_heroController); - } else { - _navigatorObservers = const []; - } } // Combine the default localization for Cupertino with the ones contributed @@ -342,46 +326,50 @@ class _CupertinoAppState extends State { data: effectiveThemeData, child: Builder( builder: (BuildContext context) { - return WidgetsApp( - key: GlobalObjectKey(this), - navigatorKey: widget.navigatorKey, - navigatorObservers: _navigatorObservers, - pageRouteBuilder: (RouteSettings settings, WidgetBuilder builder) => - CupertinoPageRoute(settings: settings, builder: builder), - home: widget.home, - routes: widget.routes, - initialRoute: widget.initialRoute, - onGenerateRoute: widget.onGenerateRoute, - onGenerateInitialRoutes: widget.onGenerateInitialRoutes, - onUnknownRoute: widget.onUnknownRoute, - builder: widget.builder, - title: widget.title, - onGenerateTitle: widget.onGenerateTitle, - textStyle: CupertinoTheme.of(context).textTheme.textStyle, - color: CupertinoDynamicColor.resolve(widget.color ?? effectiveThemeData.primaryColor, context), - locale: widget.locale, - localizationsDelegates: _localizationsDelegates, - localeResolutionCallback: widget.localeResolutionCallback, - localeListResolutionCallback: widget.localeListResolutionCallback, - supportedLocales: widget.supportedLocales, - showPerformanceOverlay: widget.showPerformanceOverlay, - checkerboardRasterCacheImages: widget.checkerboardRasterCacheImages, - checkerboardOffscreenLayers: widget.checkerboardOffscreenLayers, - showSemanticsDebugger: widget.showSemanticsDebugger, - debugShowCheckedModeBanner: widget.debugShowCheckedModeBanner, - inspectorSelectButtonBuilder: (BuildContext context, VoidCallback onPressed) { - return CupertinoButton.filled( - child: const Icon( - CupertinoIcons.search, - size: 28.0, - color: CupertinoColors.white, - ), - padding: EdgeInsets.zero, - onPressed: onPressed, - ); - }, - shortcuts: widget.shortcuts, - actions: widget.actions, + return HeroControllerScope( + controller: _heroController, + child: WidgetsApp( + key: GlobalObjectKey(this), + navigatorKey: widget.navigatorKey, + navigatorObservers: widget.navigatorObservers, + pageRouteBuilder: (RouteSettings settings, WidgetBuilder builder) => + CupertinoPageRoute(settings: settings, builder: builder), + home: widget.home, + routes: widget.routes, + initialRoute: widget.initialRoute, + onGenerateRoute: widget.onGenerateRoute, + onGenerateInitialRoutes: widget.onGenerateInitialRoutes, + onUnknownRoute: widget.onUnknownRoute, + builder: widget.builder, + title: widget.title, + onGenerateTitle: widget.onGenerateTitle, + textStyle: CupertinoTheme.of(context).textTheme.textStyle, + color: CupertinoDynamicColor.resolve(widget.color ?? effectiveThemeData.primaryColor, context), + locale: widget.locale, + localizationsDelegates: _localizationsDelegates, + localeResolutionCallback: widget.localeResolutionCallback, + localeListResolutionCallback: widget.localeListResolutionCallback, + supportedLocales: widget.supportedLocales, + showPerformanceOverlay: widget.showPerformanceOverlay, + checkerboardRasterCacheImages: widget.checkerboardRasterCacheImages, + checkerboardOffscreenLayers: widget.checkerboardOffscreenLayers, + showSemanticsDebugger: widget.showSemanticsDebugger, + debugShowCheckedModeBanner: widget.debugShowCheckedModeBanner, + inspectorSelectButtonBuilder: (BuildContext context, VoidCallback onPressed) { + return CupertinoButton.filled( + child: const Icon( + CupertinoIcons.search, + size: 28.0, + color: CupertinoColors.white, + ), + padding: EdgeInsets.zero, + onPressed: onPressed, + ); + }, + shortcuts: widget.shortcuts, + actions: widget.actions, + + ), ); }, ), diff --git a/packages/flutter/lib/src/material/app.dart b/packages/flutter/lib/src/material/app.dart index ed272ee547..0e02df1b25 100644 --- a/packages/flutter/lib/src/material/app.dart +++ b/packages/flutter/lib/src/material/app.dart @@ -571,7 +571,6 @@ class _MaterialAppState extends State { void initState() { super.initState(); _heroController = HeroController(createRectTween: _createRectTween); - _updateNavigator(); } @override @@ -584,21 +583,6 @@ class _MaterialAppState extends State { // Navigator has a GlobalKey). _heroController = HeroController(createRectTween: _createRectTween); } - _updateNavigator(); - } - - List _navigatorObservers; - - void _updateNavigator() { - if (widget.home != null || - widget.routes.isNotEmpty || - widget.onGenerateRoute != null || - widget.onUnknownRoute != null) { - _navigatorObservers = List.from(widget.navigatorObservers) - ..add(_heroController); - } else { - _navigatorObservers = const []; - } } RectTween _createRectTween(Rect begin, Rect end) { @@ -619,36 +603,38 @@ class _MaterialAppState extends State { @override Widget build(BuildContext context) { - Widget result = WidgetsApp( - key: GlobalObjectKey(this), - navigatorKey: widget.navigatorKey, - navigatorObservers: _navigatorObservers, - pageRouteBuilder: (RouteSettings settings, WidgetBuilder builder) { - return MaterialPageRoute(settings: settings, builder: builder); - }, - home: widget.home, - routes: widget.routes, - initialRoute: widget.initialRoute, - onGenerateRoute: widget.onGenerateRoute, - onGenerateInitialRoutes: widget.onGenerateInitialRoutes, - onUnknownRoute: widget.onUnknownRoute, - builder: (BuildContext context, Widget child) { - // Use a light theme, dark theme, or fallback theme. - final ThemeMode mode = widget.themeMode ?? ThemeMode.system; - ThemeData theme; - if (widget.darkTheme != null) { - final ui.Brightness platformBrightness = MediaQuery.platformBrightnessOf(context); - if (mode == ThemeMode.dark || + Widget result = HeroControllerScope( + controller: _heroController, + child: WidgetsApp( + key: GlobalObjectKey(this), + navigatorKey: widget.navigatorKey, + navigatorObservers: widget.navigatorObservers, + pageRouteBuilder: (RouteSettings settings, WidgetBuilder builder) { + return MaterialPageRoute(settings: settings, builder: builder); + }, + home: widget.home, + routes: widget.routes, + initialRoute: widget.initialRoute, + onGenerateRoute: widget.onGenerateRoute, + onGenerateInitialRoutes: widget.onGenerateInitialRoutes, + onUnknownRoute: widget.onUnknownRoute, + builder: (BuildContext context, Widget child) { + // Use a light theme, dark theme, or fallback theme. + final ThemeMode mode = widget.themeMode ?? ThemeMode.system; + ThemeData theme; + if (widget.darkTheme != null) { + final ui.Brightness platformBrightness = MediaQuery.platformBrightnessOf(context); + if (mode == ThemeMode.dark || (mode == ThemeMode.system && platformBrightness == ui.Brightness.dark)) { - theme = widget.darkTheme; + theme = widget.darkTheme; + } } - } - theme ??= widget.theme ?? ThemeData.fallback(); + theme ??= widget.theme ?? ThemeData.fallback(); - return AnimatedTheme( - data: theme, - isMaterialAppTheme: true, - child: widget.builder != null + return AnimatedTheme( + data: theme, + isMaterialAppTheme: true, + child: widget.builder != null ? Builder( builder: (BuildContext context) { // Why are we surrounding a builder with a builder? @@ -666,38 +652,39 @@ class _MaterialAppState extends State { }, ) : child, - ); - }, - title: widget.title, - onGenerateTitle: widget.onGenerateTitle, - textStyle: _errorTextStyle, - // The color property is always pulled from the light theme, even if dark - // mode is activated. This was done to simplify the technical details - // of switching themes and it was deemed acceptable because this color - // property is only used on old Android OSes to color the app bar in - // Android's switcher UI. - // - // blue is the primary color of the default theme - color: widget.color ?? widget.theme?.primaryColor ?? Colors.blue, - locale: widget.locale, - localizationsDelegates: _localizationsDelegates, - localeResolutionCallback: widget.localeResolutionCallback, - localeListResolutionCallback: widget.localeListResolutionCallback, - supportedLocales: widget.supportedLocales, - showPerformanceOverlay: widget.showPerformanceOverlay, - checkerboardRasterCacheImages: widget.checkerboardRasterCacheImages, - checkerboardOffscreenLayers: widget.checkerboardOffscreenLayers, - showSemanticsDebugger: widget.showSemanticsDebugger, - debugShowCheckedModeBanner: widget.debugShowCheckedModeBanner, - inspectorSelectButtonBuilder: (BuildContext context, VoidCallback onPressed) { - return FloatingActionButton( - child: const Icon(Icons.search), - onPressed: onPressed, - mini: true, - ); - }, - shortcuts: widget.shortcuts, - actions: widget.actions, + ); + }, + title: widget.title, + onGenerateTitle: widget.onGenerateTitle, + textStyle: _errorTextStyle, + // The color property is always pulled from the light theme, even if dark + // mode is activated. This was done to simplify the technical details + // of switching themes and it was deemed acceptable because this color + // property is only used on old Android OSes to color the app bar in + // Android's switcher UI. + // + // blue is the primary color of the default theme + color: widget.color ?? widget.theme?.primaryColor ?? Colors.blue, + locale: widget.locale, + localizationsDelegates: _localizationsDelegates, + localeResolutionCallback: widget.localeResolutionCallback, + localeListResolutionCallback: widget.localeListResolutionCallback, + supportedLocales: widget.supportedLocales, + showPerformanceOverlay: widget.showPerformanceOverlay, + checkerboardRasterCacheImages: widget.checkerboardRasterCacheImages, + checkerboardOffscreenLayers: widget.checkerboardOffscreenLayers, + showSemanticsDebugger: widget.showSemanticsDebugger, + debugShowCheckedModeBanner: widget.debugShowCheckedModeBanner, + inspectorSelectButtonBuilder: (BuildContext context, VoidCallback onPressed) { + return FloatingActionButton( + child: const Icon(Icons.search), + onPressed: onPressed, + mini: true, + ); + }, + shortcuts: widget.shortcuts, + actions: widget.actions, + ), ); assert(() { diff --git a/packages/flutter/lib/src/widgets/navigator.dart b/packages/flutter/lib/src/widgets/navigator.dart index 005400fc76..7e73137971 100644 --- a/packages/flutter/lib/src/widgets/navigator.dart +++ b/packages/flutter/lib/src/widgets/navigator.dart @@ -17,6 +17,7 @@ import 'binding.dart'; import 'focus_manager.dart'; import 'focus_scope.dart'; import 'framework.dart'; +import 'heroes.dart'; import 'overlay.dart'; import 'route_notification_messages.dart'; import 'routes.dart'; @@ -608,6 +609,45 @@ class NavigatorObserver { void didStopUserGesture() { } } +/// An inherited widget to host a hero controller. +/// +/// This class should not be used directly. The [MaterialApp] and [CupertinoApp] +/// use this class to host the [HeroController], and they should be the only +/// exception to use this class. If you want to subscribe your own +/// [HeroController], use the [Navigator.observers] instead. +/// +/// The hosted hero controller will be picked up by the navigator in the +/// [child] subtree. Once a navigator picks up this controller, the navigator +/// will bar any navigator below its subtree from receiving this controller. +/// +/// See also: +/// +/// * [Navigator.observers], which is the standard way of providing a +/// [HeroController]. +class HeroControllerScope extends InheritedWidget { + /// Creates a widget to host the input [controller]. + const HeroControllerScope({ + Key key, + this.controller, + Widget child, + }) : super(key: key, child: child); + + /// The hero controller that is hosted inside this widget. + final HeroController controller; + + /// Retrieves the [HeroController] from the closest [HeroControllerScope] + /// ancestor. + static HeroController of(BuildContext context) { + final HeroControllerScope host = context.dependOnInheritedWidgetOfExactType(); + return host?.controller; + } + + @override + bool updateShouldNotify(HeroControllerScope oldWidget) { + return oldWidget.controller != controller; + } +} + /// A [Route] wrapper interface that can be staged for [TransitionDelegate] to /// decide how its underlying [Route] should transition on or off screen. abstract class RouteTransitionRecord { @@ -2664,6 +2704,10 @@ class NavigatorState extends State with TickerProviderStateMixin { bool _debugLocked = false; // used to prevent re-entrant calls to push, pop, and friends + HeroController _heroControllerFromScope; + + List _effectiveObservers; + @override void initState() { super.initState(); @@ -2675,6 +2719,15 @@ class NavigatorState extends State with TickerProviderStateMixin { assert(observer.navigator == null); observer._navigator = this; } + _effectiveObservers = widget.observers; + + // We have to manually extract the inherited widget in initState because + // the current context is not fully initialized. + final HeroControllerScope heroControllerScope = context + .getElementForInheritedWidgetOfExactType() + ?.widget as HeroControllerScope; + _updateHeroController(heroControllerScope?.controller); + String initialRoute = widget.initialRoute; if (widget.pages.isNotEmpty) { _history.addAll( @@ -2707,6 +2760,28 @@ class NavigatorState extends State with TickerProviderStateMixin { assert(() { _debugLocked = false; return true; }()); } + @override + void didChangeDependencies() { + super.didChangeDependencies(); + _updateHeroController(HeroControllerScope.of(context)); + } + + void _updateHeroController(HeroController newHeroController) { + if (_heroControllerFromScope != newHeroController) { + _heroControllerFromScope?._navigator = null; + newHeroController?._navigator = this; + _heroControllerFromScope = newHeroController; + _updateEffectiveObservers(); + } + } + + void _updateEffectiveObservers() { + if (_heroControllerFromScope != null) + _effectiveObservers = widget.observers + [_heroControllerFromScope]; + else + _effectiveObservers = widget.observers; + } + @override void didUpdateWidget(Navigator oldWidget) { super.didUpdateWidget(oldWidget); @@ -2721,6 +2796,7 @@ class NavigatorState extends State with TickerProviderStateMixin { assert(observer.navigator == null); observer._navigator = this; } + _updateEffectiveObservers(); } if (oldWidget.pages != widget.pages) { assert( @@ -2754,7 +2830,7 @@ class NavigatorState extends State with TickerProviderStateMixin { _debugLocked = true; return true; }()); - for (final NavigatorObserver observer in widget.observers) + for (final NavigatorObserver observer in _effectiveObservers) observer._navigator = null; focusScopeNode.dispose(); for (final _RouteEntry entry in _history) @@ -3181,19 +3257,19 @@ class NavigatorState extends State with TickerProviderStateMixin { } void _flushObserverNotifications() { - if (widget.observers.isEmpty) { + if (_effectiveObservers.isEmpty) { _observedRouteDeletions.clear(); _observedRouteAdditions.clear(); return; } while (_observedRouteAdditions.isNotEmpty) { final _NavigatorObservation observation = _observedRouteAdditions.removeLast(); - widget.observers.forEach(observation.notify); + _effectiveObservers.forEach(observation.notify); } while (_observedRouteDeletions.isNotEmpty) { final _NavigatorObservation observation = _observedRouteDeletions.removeFirst(); - widget.observers.forEach(observation.notify); + _effectiveObservers.forEach(observation.notify); } } @@ -3885,7 +3961,7 @@ class NavigatorState extends State with TickerProviderStateMixin { _RouteEntry.willBePresentPredicate, ).route; } - for (final NavigatorObserver observer in widget.observers) + for (final NavigatorObserver observer in _effectiveObservers) observer.didStartUserGesture(route, previousRoute); } } @@ -3898,7 +3974,7 @@ class NavigatorState extends State with TickerProviderStateMixin { assert(_userGesturesInProgress > 0); _userGesturesInProgress -= 1; if (_userGesturesInProgress == 0) { - for (final NavigatorObserver observer in widget.observers) + for (final NavigatorObserver observer in _effectiveObservers) observer.didStopUserGesture(); } } @@ -3933,18 +4009,23 @@ class NavigatorState extends State with TickerProviderStateMixin { Widget build(BuildContext context) { assert(!_debugLocked); assert(_history.isNotEmpty); - return Listener( - onPointerDown: _handlePointerDown, - onPointerUp: _handlePointerUpOrCancel, - onPointerCancel: _handlePointerUpOrCancel, - child: AbsorbPointer( - absorbing: false, // it's mutated directly by _cancelActivePointers above - child: FocusScope( - node: focusScopeNode, - autofocus: true, - child: Overlay( - key: _overlayKey, - initialEntries: overlay == null ? _allRouteOverlayEntries.toList(growable: false) : const [], + // Hides the HeroControllerScope for the widget subtree so that the other + // nested navigator underneath will not pick up the hero controller above + // this level. + return HeroControllerScope( + child: Listener( + onPointerDown: _handlePointerDown, + onPointerUp: _handlePointerUpOrCancel, + onPointerCancel: _handlePointerUpOrCancel, + child: AbsorbPointer( + absorbing: false, // it's mutated directly by _cancelActivePointers above + child: FocusScope( + node: focusScopeNode, + autofocus: true, + child: Overlay( + key: _overlayKey, + initialEntries: overlay == null ? _allRouteOverlayEntries.toList(growable: false) : const [], + ), ), ), ), diff --git a/packages/flutter/test/material/debug_test.dart b/packages/flutter/test/material/debug_test.dart index 7e0bc1c363..8ed3d7a099 100644 --- a/packages/flutter/test/material/debug_test.dart +++ b/packages/flutter/test/material/debug_test.dart @@ -150,6 +150,7 @@ void main() { ' AbsorbPointer\n' ' _PointerListener\n' ' Listener\n' + ' HeroControllerScope\n' ' Navigator-[GlobalObjectKey _WidgetsAppState#00000]\n' ' IconTheme\n' ' IconTheme\n' @@ -182,6 +183,7 @@ void main() { ' Focus\n' ' Shortcuts\n' ' WidgetsApp-[GlobalObjectKey _MaterialAppState#00000]\n' + ' HeroControllerScope\n' ' ScrollConfiguration\n' ' MaterialApp\n' ' [root]\n' diff --git a/packages/flutter/test/widgets/navigator_test.dart b/packages/flutter/test/widgets/navigator_test.dart index b534e360d7..5b98dcc72c 100644 --- a/packages/flutter/test/widgets/navigator_test.dart +++ b/packages/flutter/test/widgets/navigator_test.dart @@ -1965,6 +1965,77 @@ void main() { expect(popNextOfFirst, secondRoute); }); + testWidgets('hero controller scope works', (WidgetTester tester) async { + final GlobalKey top = GlobalKey(); + final GlobalKey sub = GlobalKey(); + + final List observations = []; + final HeroControllerSpy spy = HeroControllerSpy() + ..onPushed = (Route route, Route previousRoute) { + observations.add( + NavigatorObservation( + current: route?.settings?.name, + previous: previousRoute?.settings?.name, + operation: 'didPush' + ) + ); + }; + await tester.pumpWidget( + HeroControllerScope( + controller: spy, + child: Directionality( + textDirection: TextDirection.ltr, + child: Navigator( + key: top, + initialRoute: 'top1', + onGenerateRoute: (RouteSettings s) { + return MaterialPageRoute( + builder: (BuildContext c) { + return Navigator( + key: sub, + initialRoute: 'sub1', + onGenerateRoute: (RouteSettings s) { + return MaterialPageRoute( + builder: (BuildContext c) { + return const Placeholder(); + }, + settings: s, + ); + }, + ); + }, + settings: s, + ); + }, + ), + ) + ) + ); + // It should only observe the top navigator. + expect(observations.length, 1); + expect(observations[0].current, 'top1'); + expect(observations[0].previous, isNull); + + sub.currentState.push(MaterialPageRoute( + settings: const RouteSettings(name:'sub2'), + builder: (BuildContext context) => const Text('sub2') + )); + await tester.pumpAndSettle(); + + expect(find.text('sub2'), findsOneWidget); + // It should not record sub navigator. + expect(observations.length, 1); + + top.currentState.push(MaterialPageRoute( + settings: const RouteSettings(name:'top2'), + builder: (BuildContext context) => const Text('top2') + )); + await tester.pumpAndSettle(); + expect(observations.length, 2); + expect(observations[1].current, 'top2'); + expect(observations[1].previous, 'top1'); + }); + group('Page api', (){ Widget buildNavigator({ List> pages, @@ -3024,6 +3095,16 @@ class StatefulTestState extends State { } } +class HeroControllerSpy extends HeroController { + OnObservation onPushed; + @override + void didPush(Route route, Route previousRoute) { + if (onPushed != null) { + onPushed(route, previousRoute); + } + } +} + class NavigatorObservation { const NavigatorObservation({this.previous, this.current, this.operation}); final String previous;