From fdab8546a0f652061ce633043efd6b06ee14191c Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Wed, 6 Jan 2021 16:15:05 -0800 Subject: [PATCH] Minor doc, style, and perf updates to Navigator/Routes (#71689) * Minor doc, style, and perf updates to Navigator/Routes These are minor fixes I ended up making while working on a larger project that never went anywhere. - Used a ColoredBox instead of a DecoratedBox for ModalBarrier (probably a trivial memory/perf win). - A bunch of Navigator documentation fixes around when things rebuild. - Mark routes dirty when the Navigator has a dependency change. I cannot find a way to test this because as far as I can tell it makes no actual difference to when things rebuild because whenever the Navigator rebuilds the Overlay rebuilds and whenever that happens every OverlayEntry rebuilds, but in theory that's not guaranteed so this is sort of a correctness fix. It may even be a perf loss. We do something similar in didUpdateWidget already. I could be convinced to maybe remove these... - Make ModalRoute.filter public like everything else. - Made ModalRoute update its barrier when it gets an update, in case e.g. the modal barrier depends on inherited widgets via the navigator context. Again, not sure of any way to detect this, it might actually be moot, but it seems to be the technically correct solution? - Minor style fixes. All in all I couldn't figure out a way to test any of this (I wrote multiple large tests but it turns out they all already pass on master and are effectively redundant with existing tests). * Remove extraneous blank line --- .../lib/src/services/message_codecs.dart | 1 - .../lib/src/widgets/modal_barrier.dart | 7 +- .../flutter/lib/src/widgets/navigator.dart | 30 +++++--- packages/flutter/lib/src/widgets/routes.dart | 72 +++++++++++++------ .../flutter/test/widgets/overlay_test.dart | 2 +- 5 files changed, 73 insertions(+), 39 deletions(-) diff --git a/packages/flutter/lib/src/services/message_codecs.dart b/packages/flutter/lib/src/services/message_codecs.dart index e1784c5223..92f9d722e8 100644 --- a/packages/flutter/lib/src/services/message_codecs.dart +++ b/packages/flutter/lib/src/services/message_codecs.dart @@ -2,7 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. - import 'dart:convert'; import 'dart:typed_data'; diff --git a/packages/flutter/lib/src/widgets/modal_barrier.dart b/packages/flutter/lib/src/widgets/modal_barrier.dart index 3d1b693785..31538bdcf6 100644 --- a/packages/flutter/lib/src/widgets/modal_barrier.dart +++ b/packages/flutter/lib/src/widgets/modal_barrier.dart @@ -8,7 +8,6 @@ import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; import 'basic.dart'; -import 'container.dart'; import 'debug.dart'; import 'framework.dart'; import 'gesture_detector.dart'; @@ -113,10 +112,8 @@ class ModalBarrier extends StatelessWidget { opaque: true, child: ConstrainedBox( constraints: const BoxConstraints.expand(), - child: color == null ? null : DecoratedBox( - decoration: BoxDecoration( - color: color, - ), + child: color == null ? null : ColoredBox( + color: color!, ), ), ), diff --git a/packages/flutter/lib/src/widgets/navigator.dart b/packages/flutter/lib/src/widgets/navigator.dart index 88bae80072..deef670448 100644 --- a/packages/flutter/lib/src/widgets/navigator.dart +++ b/packages/flutter/lib/src/widgets/navigator.dart @@ -399,20 +399,30 @@ abstract class Route { /// /// See also: /// - /// * [changedExternalState], which is called when the [Navigator] rebuilds. + /// * [changedExternalState], which is called when the [Navigator] has + /// updated in some manner that might affect the routes. @protected @mustCallSuper void changedInternalState() { } - /// Called whenever the [Navigator] has its widget rebuilt, to indicate that - /// the route may wish to rebuild as well. + /// Called whenever the [Navigator] has updated in some manner that might + /// affect routes, to indicate that the route may wish to rebuild as well. /// - /// This is called by the [Navigator] whenever the [NavigatorState]'s - /// [State.widget] changes, for example because the [MaterialApp] has been rebuilt. - /// This ensures that routes that directly refer to the state of the widget - /// that built the [MaterialApp] will be notified when that widget rebuilds, - /// since it would otherwise be difficult to notify the routes that state they - /// depend on may have changed. + /// This is called by the [Navigator] whenever the + /// [NavigatorState]'s [State.widget] changes (as in [State.didUpdateWidget]), + /// for example because the [MaterialApp] has been rebuilt. This + /// ensures that routes that directly refer to the state of the + /// widget that built the [MaterialApp] will be notified when that + /// widget rebuilds, since it would otherwise be difficult to notify + /// the routes that state they depend on may have changed. + /// + /// It is also called whenever the [Navigator]'s dependencies change + /// (as in [State.didChangeDependencies]). This allows routes to use the + /// [Navigator]'s context ([NavigatorState.context]), for example in + /// [ModalRoute.barrierColor], and update accordingly. + /// + /// The [ModalRoute] subclass overrides this to force the barrier + /// overlay to rebuild. /// /// See also: /// @@ -3442,6 +3452,8 @@ class NavigatorState extends State with TickerProviderStateMixin, Res void didChangeDependencies() { super.didChangeDependencies(); _updateHeroController(HeroControllerScope.of(context)); + for (final _RouteEntry entry in _history) + entry.route.changedExternalState(); } void _updateHeroController(HeroController? newHeroController) { diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index 17ef544e8f..dc41b755ff 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -871,15 +871,14 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute extends TransitionRoute with LocalHistoryRoute extends TransitionRoute with LocalHistoryRoute true; /// {@template flutter.widgets.ModalRoute.barrierColor} @@ -1174,22 +1187,24 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute Theme.of(navigator.context).backgroundColor; /// ``` /// - /// The [Navigator] causes the [ModalRoute]'s modal barrier overlay entry - /// to rebuild any time its dependencies change. - /// /// {@end-tool} /// /// See also: @@ -1213,9 +1228,14 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute extends TransitionRoute with LocalHistoryRoute extends TransitionRoute with LocalHistoryRoute extends TransitionRoute with LocalHistoryRoute