From b70ea307f2cf319b3fd0a90cfb20bd3f9f21467c Mon Sep 17 00:00:00 2001 From: Rodrigo Perez Date: Wed, 28 Aug 2024 17:52:08 -0300 Subject: [PATCH] Add Nuvigator.maybeOf --- .../screens/text_composer_screen.dart | 2 +- .../screens/success_screen.dart | 4 +-- example/lib/samples/screens/home_screen.dart | 4 +-- lib/src/nuvigator.dart | 29 +++++++++---------- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/example/lib/samples/modules/composer/screens/text_composer_screen.dart b/example/lib/samples/modules/composer/screens/text_composer_screen.dart index b09a6fe..b65ab84 100644 --- a/example/lib/samples/modules/composer/screens/text_composer_screen.dart +++ b/example/lib/samples/modules/composer/screens/text_composer_screen.dart @@ -55,7 +55,7 @@ class _TextComposerScreenState extends State { minLines: 5, maxLines: null, ), - RaisedButton( + ElevatedButton( onPressed: () => widget.submitText(_controller.text), child: const Text('Publish'), ), diff --git a/example/lib/samples/modules/friend_request/screens/success_screen.dart b/example/lib/samples/modules/friend_request/screens/success_screen.dart index c71ab8d..648e78e 100644 --- a/example/lib/samples/modules/friend_request/screens/success_screen.dart +++ b/example/lib/samples/modules/friend_request/screens/success_screen.dart @@ -40,11 +40,11 @@ class SuccessScreen extends StatelessWidget { textAlign: TextAlign.center, ), const SizedBox(height: 48), - RaisedButton( + ElevatedButton( onPressed: closeFlow, child: const Text('Close flow'), ), - RaisedButton( + ElevatedButton( onPressed: toComposeText, child: const Text('Compose a message'), ), diff --git a/example/lib/samples/screens/home_screen.dart b/example/lib/samples/screens/home_screen.dart index e458157..cacafc0 100644 --- a/example/lib/samples/screens/home_screen.dart +++ b/example/lib/samples/screens/home_screen.dart @@ -30,7 +30,7 @@ class HomeScreen extends StatelessWidget { textAlign: TextAlign.center, ), const SizedBox(height: 48), - RaisedButton( + ElevatedButton( onPressed: () { // final r = NuRouter.of(context); // r.toListRequests(); @@ -40,7 +40,7 @@ class HomeScreen extends StatelessWidget { }, child: const Text('Review friend requests'), ), - RaisedButton( + ElevatedButton( onPressed: () async { String text; diff --git a/lib/src/nuvigator.dart b/lib/src/nuvigator.dart index edbe6ca..5e81d2d 100644 --- a/lib/src/nuvigator.dart +++ b/lib/src/nuvigator.dart @@ -131,7 +131,7 @@ class _NuvigatorInner extends Navigator { class NuvigatorState extends NavigatorState with WidgetsBindingObserver { NuvigatorState get rootNuvigator => - Nuvigator.of(context, rootNuvigator: true) ?? this; + Nuvigator.maybeOf(context, rootNuvigator: true) ?? this; List nestedNuvigators = []; @@ -172,7 +172,7 @@ class NuvigatorState extends NavigatorState @override void initState() { - parent = Nuvigator.of(context, nullOk: true); + parent = Nuvigator.maybeOf(context); if (isNested) { parent!.nestedNuvigators.add(this); } @@ -550,11 +550,10 @@ class Nuvigator extends StatelessWidget { final Map? initialArguments; final ShouldRebuildFn? shouldRebuild; - /// Fetches a [NuvigatorState] from the current BuildContext. - static NuvigatorState? of( + /// Maybe fetches a [NuvigatorState] from the current BuildContext. + static NuvigatorState? maybeOf( BuildContext context, { bool rootNuvigator = false, - bool nullOk = false, }) { if (rootNuvigator) { return context.findRootAncestorStateOfType>(); @@ -562,20 +561,20 @@ class Nuvigator extends StatelessWidget { final closestNuvigator = context.findAncestorStateOfType>(); if (closestNuvigator != null) return closestNuvigator; - assert(() { - if (!nullOk) { - throw FlutterError( - 'Nuvigator operation requested with a context that does not include a Nuvigator.\n' - 'The context used to push or pop routes from the Nuvigator must be that of a ' - 'widget that is a descendant of a Nuvigator widget.' - 'Also check if the provided Router [T] type exists withing a the Nuvigator context.'); - } - return true; - }()); + return null; } } + /// Fetches a [NuvigatorState] from the current BuildContext, or throws an + /// error if doesn't find it + static NuvigatorState of( + BuildContext context, { + bool rootNuvigator = false, + }) { + return Nuvigator.maybeOf(context, rootNuvigator: rootNuvigator)!; + } + /// Helper method that allows passing a Nuvigator to a builder function Nuvigator call(BuildContext context, [Widget? child]) { return this;