From d127f2c94aefde24e966757dad381a47c2bd2b29 Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Thu, 8 Oct 2020 15:56:31 -0700 Subject: [PATCH] Fix nullability for some routing related stuff (#67675) --- packages/flutter/lib/src/rendering/layer.dart | 4 +- .../lib/src/services/raw_keyboard.dart | 3 +- .../flutter/lib/src/widgets/drag_target.dart | 1 - packages/flutter/lib/src/widgets/routes.dart | 4 +- .../flutter/test/widgets/routes_test.dart | 188 ++++++++++-------- 5 files changed, 109 insertions(+), 91 deletions(-) diff --git a/packages/flutter/lib/src/rendering/layer.dart b/packages/flutter/lib/src/rendering/layer.dart index 08d6bb5b48..dc00a35a71 100644 --- a/packages/flutter/lib/src/rendering/layer.dart +++ b/packages/flutter/lib/src/rendering/layer.dart @@ -2225,7 +2225,7 @@ class FollowerLayer extends ContainerLayer { Matrix4? _invertedTransform; bool _inverseDirty = true; - Offset? _transformOffset(Offset localPosition) { + Offset? _transformOffset(Offset localPosition) { if (_inverseDirty) { _invertedTransform = Matrix4.tryInvert(getLastTransform()!); _inverseDirty = false; @@ -2245,7 +2245,7 @@ class FollowerLayer extends ContainerLayer { } return false; } - final Offset? transformedOffset = _transformOffset(localPosition); + final Offset? transformedOffset = _transformOffset(localPosition); if (transformedOffset == null) { return false; } diff --git a/packages/flutter/lib/src/services/raw_keyboard.dart b/packages/flutter/lib/src/services/raw_keyboard.dart index 3abb6497a5..085a695e17 100644 --- a/packages/flutter/lib/src/services/raw_keyboard.dart +++ b/packages/flutter/lib/src/services/raw_keyboard.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:io'; import 'dart:ui'; @@ -737,7 +736,7 @@ class RawKeyboard { } @immutable -class _ModifierSidePair extends Object { +class _ModifierSidePair { const _ModifierSidePair(this.modifier, this.side); final ModifierKey modifier; diff --git a/packages/flutter/lib/src/widgets/drag_target.dart b/packages/flutter/lib/src/widgets/drag_target.dart index 12e8da4c00..4182b65c74 100644 --- a/packages/flutter/lib/src/widgets/drag_target.dart +++ b/packages/flutter/lib/src/widgets/drag_target.dart @@ -131,7 +131,6 @@ class Draggable extends StatefulWidget { assert(maxSimultaneousDrags == null || maxSimultaneousDrags >= 0), super(key: key); - /// The data that will be dropped by this draggable. final T? data; diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index c1f6d4bdf5..a91d8d3441 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -881,7 +881,7 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute? of(BuildContext context) { + static ModalRoute? of(BuildContext context) { final _ModalScopeStatus? widget = context.dependOnInheritedWidgetOfExactType<_ModalScopeStatus>(); return widget?.route as ModalRoute?; } @@ -1818,7 +1818,7 @@ class _DialogRoute extends PopupRoute { /// /// * [showDialog], which displays a Material-style dialog. /// * [showCupertinoDialog], which displays an iOS-style dialog. -Future showGeneralDialog({ +Future showGeneralDialog({ required BuildContext context, required RoutePageBuilder pageBuilder, bool barrierDismissible = false, diff --git a/packages/flutter/test/widgets/routes_test.dart b/packages/flutter/test/widgets/routes_test.dart index 6517d1e486..8e7703b2f4 100644 --- a/packages/flutter/test/widgets/routes_test.dart +++ b/packages/flutter/test/widgets/routes_test.dart @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// @dart = 2.8 - import 'dart:collection'; import 'package:flutter/foundation.dart'; @@ -19,7 +17,7 @@ final List results = []; Set routes = HashSet(); -class TestRoute extends Route with LocalHistoryRoute { +class TestRoute extends Route with LocalHistoryRoute { TestRoute(this.name); final String name; @@ -57,7 +55,7 @@ class TestRoute extends Route with LocalHistoryRoute { } @override - void didReplace(Route oldRoute) { + void didReplace(Route? oldRoute) { expect(oldRoute, isA()); final TestRoute castRoute = oldRoute as TestRoute; log('didReplace ${castRoute.name}'); @@ -65,11 +63,11 @@ class TestRoute extends Route with LocalHistoryRoute { } @override - bool didPop(String result) { + bool didPop(String? result) { log('didPop $result'); bool returnValue; if (returnValue = super.didPop(result)) - navigator.finalizeRoute(this); + navigator!.finalizeRoute(this); return returnValue; } @@ -82,9 +80,9 @@ class TestRoute extends Route with LocalHistoryRoute { } @override - void didChangeNext(Route nextRoute) { + void didChangeNext(Route? nextRoute) { expect(nextRoute, anyOf(isNull, isA())); - final TestRoute castRoute = nextRoute as TestRoute; + final TestRoute? castRoute = nextRoute as TestRoute?; log('didChangeNext ${castRoute?.name}'); super.didChangeNext(castRoute); } @@ -148,7 +146,7 @@ void main() { ), ), ); - final NavigatorState host = navigatorKey.currentState; + final NavigatorState host = navigatorKey.currentState!; await runNavigatorTest( tester, host, @@ -159,7 +157,7 @@ void main() { 'initial: didChangeNext null', ], ); - TestRoute second; + late TestRoute second; await runNavigatorTest( tester, host, @@ -231,7 +229,7 @@ void main() { ), ), ); - final NavigatorState host = navigatorKey.currentState; + final NavigatorState host = navigatorKey.currentState!; await runNavigatorTest( tester, host, @@ -242,7 +240,7 @@ void main() { 'first: didChangeNext null', ], ); - TestRoute second; + late TestRoute second; await runNavigatorTest( tester, host, @@ -294,7 +292,7 @@ void main() { 'second: didChangeNext three', ], ); - TestRoute four; + late TestRoute four; await runNavigatorTest( tester, host, @@ -342,7 +340,7 @@ void main() { ), ), ); - final NavigatorState host = navigatorKey.currentState; + final NavigatorState host = navigatorKey.currentState!; await runNavigatorTest( tester, host, @@ -364,7 +362,7 @@ void main() { 'A: didChangeNext B', ], ); - TestRoute routeC; + late TestRoute routeC; await runNavigatorTest( tester, host, @@ -377,7 +375,7 @@ void main() { ], ); expect(routeC.isActive, isTrue); - TestRoute routeB; + late TestRoute routeB; await runNavigatorTest( tester, host, @@ -424,7 +422,7 @@ void main() { ), ), ); - final NavigatorState host = navigatorKey.currentState; + final NavigatorState host = navigatorKey.currentState!; await runNavigatorTest( tester, host, @@ -552,7 +550,7 @@ void main() { builder: (BuildContext context) { return ElevatedButton( onPressed: () { - Navigator.of(context).push( + Navigator.of(context)!.push( PageRouteBuilder( settings: settings, pageBuilder: (BuildContext context, Animation input, Animation out) { @@ -601,7 +599,7 @@ void main() { builder: (BuildContext context) { return ElevatedButton( onPressed: () { - Navigator.of(context).push( + Navigator.of(context)!.push( PageRouteBuilder( settings: settings, pageBuilder: (BuildContext context, Animation input, Animation out) { @@ -656,9 +654,9 @@ void main() { ); // Push page one, its secondary animation is kAlwaysDismissedAnimation. - ProxyAnimation secondaryAnimationProxyPageOne; - ProxyAnimation animationPageOne; - navigator.currentState.push( + late ProxyAnimation secondaryAnimationProxyPageOne; + late ProxyAnimation animationPageOne; + navigator.currentState!.push( PageRouteBuilder( pageBuilder: (_, Animation animation, Animation secondaryAnimation) { secondaryAnimationProxyPageOne = secondaryAnimation as ProxyAnimation; @@ -675,9 +673,9 @@ void main() { // Push page two, the secondary animation of page one is the primary // animation of page two. - ProxyAnimation secondaryAnimationProxyPageTwo; - ProxyAnimation animationPageTwo; - navigator.currentState.push( + late ProxyAnimation secondaryAnimationProxyPageTwo; + late ProxyAnimation animationPageTwo; + navigator.currentState!.push( PageRouteBuilder( pageBuilder: (_, Animation animation, Animation secondaryAnimation) { secondaryAnimationProxyPageTwo = secondaryAnimation as ProxyAnimation; @@ -695,7 +693,7 @@ void main() { // Pop page two, the secondary animation of page one becomes // kAlwaysDismissedAnimation. - navigator.currentState.pop(); + navigator.currentState!.pop(); await tester.pump(); await tester.pump(const Duration(milliseconds: 100)); expect(secondaryAnimationPageOne.parent, animationPageTwo.parent); @@ -714,9 +712,9 @@ void main() { ); // Push page one, its secondary animation is kAlwaysDismissedAnimation. - ProxyAnimation secondaryAnimationProxyPageOne; - ProxyAnimation animationPageOne; - navigator.currentState.push( + late ProxyAnimation secondaryAnimationProxyPageOne; + late ProxyAnimation animationPageOne; + navigator.currentState!.push( PageRouteBuilder( pageBuilder: (_, Animation animation, Animation secondaryAnimation) { secondaryAnimationProxyPageOne = secondaryAnimation as ProxyAnimation; @@ -733,10 +731,10 @@ void main() { // Push page two, the secondary animation of page one is the primary // animation of page two. - ProxyAnimation secondaryAnimationProxyPageTwo; - ProxyAnimation animationPageTwo; + late ProxyAnimation secondaryAnimationProxyPageTwo; + late ProxyAnimation animationPageTwo; Route secondRoute; - navigator.currentState.push( + navigator.currentState!.push( secondRoute = PageRouteBuilder( pageBuilder: (_, Animation animation, Animation secondaryAnimation) { secondaryAnimationProxyPageTwo = secondaryAnimation as ProxyAnimation; @@ -754,7 +752,7 @@ void main() { // Remove the second route, the secondary animation of page one is // kAlwaysDismissedAnimation again. - navigator.currentState.removeRoute(secondRoute); + navigator.currentState!.removeRoute(secondRoute); await tester.pump(); expect(secondaryAnimationPageOne.parent, kAlwaysDismissedAnimation); }); @@ -769,9 +767,9 @@ void main() { ); // Push page one, its secondary animation is kAlwaysDismissedAnimation. - ProxyAnimation secondaryAnimationProxyPageOne; - ProxyAnimation animationPageOne; - navigator.currentState.push( + late ProxyAnimation secondaryAnimationProxyPageOne; + late ProxyAnimation animationPageOne; + navigator.currentState!.push( PageRouteBuilder( pageBuilder: (_, Animation animation, Animation secondaryAnimation) { secondaryAnimationProxyPageOne = secondaryAnimation as ProxyAnimation; @@ -788,8 +786,8 @@ void main() { // Push page two, the secondary animation of page one is the primary // animation of page two. - ProxyAnimation animationPageTwo; - navigator.currentState.push( + late ProxyAnimation animationPageTwo; + navigator.currentState!.push( PageRouteBuilder( pageBuilder: (_, Animation animation, Animation secondaryAnimation) { animationPageTwo = animation as ProxyAnimation; @@ -803,8 +801,8 @@ void main() { // Replace with a different route while push is ongoing to trigger // TrainHopping. - ProxyAnimation animationPageThree; - navigator.currentState.pushReplacement( + late ProxyAnimation animationPageThree; + navigator.currentState!.pushReplacement( TestPageRouteBuilder( pageBuilder: (_, Animation animation, Animation secondaryAnimation) { animationPageThree = animation as ProxyAnimation; @@ -825,7 +823,7 @@ void main() { expect(secondaryAnimationPageOne.parent, animationPageThree.parent); // Pop page three. - navigator.currentState.pop(); + navigator.currentState!.pop(); await tester.pump(); await tester.pumpAndSettle(); expect(secondaryAnimationPageOne.parent, kAlwaysDismissedAnimation); @@ -841,9 +839,9 @@ void main() { ); // Push page one, its secondary animation is kAlwaysDismissedAnimation. - ProxyAnimation secondaryAnimationProxyPageOne; - ProxyAnimation animationPageOne; - navigator.currentState.push( + late ProxyAnimation secondaryAnimationProxyPageOne; + late ProxyAnimation animationPageOne; + navigator.currentState!.push( PageRouteBuilder( pageBuilder: (_, Animation animation, Animation secondaryAnimation) { secondaryAnimationProxyPageOne = secondaryAnimation as ProxyAnimation; @@ -860,8 +858,8 @@ void main() { // Push page two, the secondary animation of page one is the primary // animation of page two. - ProxyAnimation animationPageTwo; - navigator.currentState.push( + late ProxyAnimation animationPageTwo; + navigator.currentState!.push( PageRouteBuilder( pageBuilder: (_, Animation animation, Animation secondaryAnimation) { animationPageTwo = animation as ProxyAnimation; @@ -875,7 +873,7 @@ void main() { // Replace with a different route while push is ongoing to trigger // TrainHopping. - navigator.currentState.pushReplacement( + navigator.currentState!.pushReplacement( TestPageRouteBuilder( pageBuilder: (_, Animation animation, Animation secondaryAnimation) { return const Text('Page Three'); @@ -889,7 +887,7 @@ void main() { expect(trainHopper.currentTrain, animationPageTwo.parent); // Pop page three while replacement push is ongoing. - navigator.currentState.pop(); + navigator.currentState!.pop(); await tester.pump(); expect(secondaryAnimationPageOne.parent, isA()); final TrainHoppingAnimation trainHopper2 = secondaryAnimationPageOne.parent as TrainHoppingAnimation; @@ -902,8 +900,8 @@ void main() { testWidgets('secondary animation is triggered when pop initial route', (WidgetTester tester) async { final GlobalKey navigator = GlobalKey(); - Animation secondaryAnimationOfRouteOne; - Animation primaryAnimationOfRouteTwo; + late Animation secondaryAnimationOfRouteOne; + late Animation primaryAnimationOfRouteTwo; await tester.pumpWidget( MaterialApp( navigatorKey: navigator, @@ -927,7 +925,7 @@ void main() { expect(secondaryAnimationOfRouteOne.value, 1.0); expect(secondaryAnimationOfRouteOne.value, primaryAnimationOfRouteTwo.value); // Pops the top most route and verifies two routes are still chained. - navigator.currentState.pop(); + navigator.currentState!.pop(); await tester.pump(); await tester.pump(const Duration(milliseconds: 30)); expect(secondaryAnimationOfRouteOne.value, 0.9); @@ -1127,7 +1125,7 @@ void main() { builder: (BuildContext context) { return ElevatedButton( onPressed: () { - Navigator.of(context).push( + Navigator.of(context)!.push( MaterialPageRoute( builder: (BuildContext innerContext) { return Container( @@ -1177,7 +1175,7 @@ void main() { builder: (BuildContext context) { return ElevatedButton( onPressed: () { - Navigator.of(context).push( + Navigator.of(context)!.push( ModifiedReverseTransitionDurationRoute( builder: (BuildContext innerContext) { return Container( @@ -1236,7 +1234,7 @@ void main() { builder: (BuildContext context) { return ElevatedButton( onPressed: () { - Navigator.of(context).push( + Navigator.of(context)!.push( ModifiedReverseTransitionDurationRoute( builder: (BuildContext innerContext) { return Container( @@ -1298,7 +1296,7 @@ void main() { child: ElevatedButton( child: const Text('X'), onPressed: () { - Navigator.of(context).push( + Navigator.of(context)!.push( _TestDialogRouteWithCustomBarrierCurve( child: const Text('Hello World'), ) @@ -1360,7 +1358,7 @@ void main() { child: ElevatedButton( child: const Text('X'), onPressed: () { - Navigator.of(context).push( + Navigator.of(context)!.push( _TestDialogRouteWithCustomBarrierCurve( child: const Text('Hello World'), barrierCurve: Curves.linear, @@ -1425,7 +1423,7 @@ void main() { child: ElevatedButton( child: const Text('X'), onPressed: () { - Navigator.of(context).push( + Navigator.of(context)!.push( _TestDialogRouteWithCustomBarrierCurve( child: const Text('Hello World'), barrierLabel: 'test label', @@ -1499,7 +1497,7 @@ void main() { expect(focusNodeOnPageOne.hasFocus, isTrue); // Pushes one page. - navigatorKey.currentState.push( + navigatorKey.currentState!.push( MaterialPageRoute( builder: (BuildContext context) => const Text('dummy2'), ) @@ -1513,7 +1511,7 @@ void main() { expect(focusNodeOnPageTwo.hasFocus, isTrue); // Pushes another page. - navigatorKey.currentState.push( + navigatorKey.currentState!.push( MaterialPageRoute( builder: (BuildContext context) => const Text('dummy3'), ) @@ -1527,7 +1525,7 @@ void main() { expect(focusNodeOnPageThree.hasFocus, isTrue); // Pops two pages simultaneously. - navigatorKey.currentState.popUntil((Route route) => route.isFirst); + navigatorKey.currentState!.popUntil((Route route) => route.isFirst); await tester.pumpAndSettle(); // It should refocus page one after pops. expect(focusNodeOnPageOne.hasFocus, isTrue); @@ -1545,7 +1543,7 @@ void main() { expect(focusNodeOnPageOne.hasFocus, isTrue); // Pushes one page. - navigatorKey.currentState.push( + navigatorKey.currentState!.push( MaterialPageRoute( builder: (BuildContext context) => const Material(child: TextField()), ) @@ -1565,7 +1563,7 @@ void main() { expect(focusNodeOnPageTwo.hasPrimaryFocus, isFalse); // Pushes another page. - navigatorKey.currentState.push( + navigatorKey.currentState!.push( MaterialPageRoute( builder: (BuildContext context) => const Text('dummy3'), ) @@ -1579,7 +1577,7 @@ void main() { expect(focusNodeOnPageThree.hasFocus, isTrue); // Pops two pages simultaneously. - navigatorKey.currentState.popUntil((Route route) => route.isFirst); + navigatorKey.currentState!.popUntil((Route route) => route.isFirst); await tester.pumpAndSettle(); // It should refocus page one after pops. expect(focusNodeOnPageOne.hasFocus, isTrue); @@ -1629,6 +1627,28 @@ void main() { await tester.pumpAndSettle(); expect(find.text('dialog1'), findsNothing); }); + + testWidgets('ModalRoute.of works for void routes', (WidgetTester tester) async { + final GlobalKey navigatorKey = GlobalKey(); + await tester.pumpWidget(MaterialApp( + navigatorKey: navigatorKey, + home: const Text('home'), + )); + expect(find.text('page2'), findsNothing); + + navigatorKey.currentState!.push(MaterialPageRoute( + builder: (BuildContext context) { + return const Text('page2'); + } + )); + + await tester.pumpAndSettle(); + expect(find.text('page2'), findsOneWidget); + + final ModalRoute? parentRoute = ModalRoute.of(tester.element(find.text('page2'))); + expect(parentRoute, isNotNull); + expect(parentRoute, isA>()); + }); } double _getOpacity(GlobalKey key, WidgetTester tester) { @@ -1644,9 +1664,9 @@ double _getOpacity(GlobalKey key, WidgetTester tester) { class ModifiedReverseTransitionDurationRoute extends MaterialPageRoute { ModifiedReverseTransitionDurationRoute({ - @required WidgetBuilder builder, - RouteSettings settings, - this.reverseTransitionDuration, + required WidgetBuilder builder, + RouteSettings? settings, + required this.reverseTransitionDuration, bool fullscreenDialog = false, }) : super( builder: builder, @@ -1690,7 +1710,7 @@ class MockRouteAware extends Fake implements RouteAware { } class TestPageRouteBuilder extends PageRouteBuilder { - TestPageRouteBuilder({RoutePageBuilder pageBuilder}) : super(pageBuilder: pageBuilder); + TestPageRouteBuilder({required RoutePageBuilder pageBuilder}) : super(pageBuilder: pageBuilder); @override Animation createAnimation() { @@ -1703,7 +1723,7 @@ class DialogObserver extends NavigatorObserver { int dialogCount = 0; @override - void didPush(Route route, Route previousRoute) { + void didPush(Route route, Route? previousRoute) { if (route.toString().contains('_DialogRoute')) { dialogRoutes.add(route as ModalRoute); dialogCount++; @@ -1712,7 +1732,7 @@ class DialogObserver extends NavigatorObserver { } @override - void didPop(Route route, Route previousRoute) { + void didPop(Route route, Route? previousRoute) { if (route.toString().contains('_DialogRoute')) { dialogRoutes.removeLast(); dialogCount--; @@ -1723,9 +1743,9 @@ class DialogObserver extends NavigatorObserver { class _TestDialogRouteWithCustomBarrierCurve extends PopupRoute { _TestDialogRouteWithCustomBarrierCurve({ - @required Widget child, + required Widget child, this.barrierLabel, - Curve barrierCurve, + Curve? barrierCurve, }) : _barrierCurve = barrierCurve, _child = child; @@ -1735,7 +1755,7 @@ class _TestDialogRouteWithCustomBarrierCurve extends PopupRoute { bool get barrierDismissible => true; @override - final String barrierLabel; + final String? barrierLabel; @override Color get barrierColor => Colors.black; // easier value to test against @@ -1745,9 +1765,9 @@ class _TestDialogRouteWithCustomBarrierCurve extends PopupRoute { if (_barrierCurve == null) { return super.barrierCurve; } - return _barrierCurve; + return _barrierCurve!; } - final Curve _barrierCurve; + final Curve? _barrierCurve; @override Duration get transitionDuration => const Duration(milliseconds: 100); // easier value to test against @@ -1768,10 +1788,10 @@ class WidgetWithLocalHistory extends StatefulWidget { } class WidgetWithLocalHistoryState extends State { - LocalHistoryEntry _localHistory; + late LocalHistoryEntry _localHistory; void addLocalHistory() { - final ModalRoute route = ModalRoute.of(context); + final ModalRoute route = ModalRoute.of(context)!; _localHistory = LocalHistoryEntry(); route.addLocalHistoryEntry(_localHistory); } @@ -1792,9 +1812,9 @@ class TransitionDetector extends DefaultTransitionDelegate { bool hasTransition = false; @override Iterable resolve({ - List newPageRouteHistory, - Map locationToExitingPageRoute, - Map> pageRouteToPagelessRoutes + required List newPageRouteHistory, + required Map locationToExitingPageRoute, + required Map> pageRouteToPagelessRoutes }) { hasTransition = true; return super.resolve( @@ -1806,13 +1826,13 @@ class TransitionDetector extends DefaultTransitionDelegate { } Widget buildNavigator({ - List> pages, - PopPageCallback onPopPage, - GlobalKey key, - TransitionDelegate transitionDelegate + required List> pages, + required PopPageCallback onPopPage, + GlobalKey? key, + TransitionDelegate? transitionDelegate }) { return MediaQuery( - data: MediaQueryData.fromWindow(WidgetsBinding.instance.window), + data: MediaQueryData.fromWindow(WidgetsBinding.instance!.window), child: Localizations( locale: const Locale('en', 'US'), delegates: const >[