Skip to content

Commit

Permalink
Add Nuvigator.maybeOf
Browse files Browse the repository at this point in the history
  • Loading branch information
raapperez committed Aug 28, 2024
1 parent 971fab3 commit b70ea30
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class _TextComposerScreenState extends State<TextComposerScreen> {
minLines: 5,
maxLines: null,
),
RaisedButton(
ElevatedButton(
onPressed: () => widget.submitText(_controller.text),
child: const Text('Publish'),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
),
Expand Down
4 changes: 2 additions & 2 deletions example/lib/samples/screens/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class HomeScreen extends StatelessWidget {
textAlign: TextAlign.center,
),
const SizedBox(height: 48),
RaisedButton(
ElevatedButton(
onPressed: () {
// final r = NuRouter.of<OldFriendRequestRouter>(context);
// r.toListRequests();
Expand All @@ -40,7 +40,7 @@ class HomeScreen extends StatelessWidget {
},
child: const Text('Review friend requests'),
),
RaisedButton(
ElevatedButton(
onPressed: () async {
String text;

Expand Down
29 changes: 14 additions & 15 deletions lib/src/nuvigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class _NuvigatorInner<T extends INuRouter> extends Navigator {
class NuvigatorState<T extends INuRouter> extends NavigatorState
with WidgetsBindingObserver {
NuvigatorState get rootNuvigator =>
Nuvigator.of(context, rootNuvigator: true) ?? this;
Nuvigator.maybeOf(context, rootNuvigator: true) ?? this;

List<NuvigatorState> nestedNuvigators = [];

Expand Down Expand Up @@ -172,7 +172,7 @@ class NuvigatorState<T extends INuRouter> extends NavigatorState

@override
void initState() {
parent = Nuvigator.of(context, nullOk: true);
parent = Nuvigator.maybeOf(context);
if (isNested) {
parent!.nestedNuvigators.add(this);
}
Expand Down Expand Up @@ -550,32 +550,31 @@ class Nuvigator<T extends INuRouter?> extends StatelessWidget {
final Map<String, Object>? initialArguments;
final ShouldRebuildFn? shouldRebuild;

/// Fetches a [NuvigatorState] from the current BuildContext.
static NuvigatorState<T>? of<T extends INuRouter>(
/// Maybe fetches a [NuvigatorState] from the current BuildContext.
static NuvigatorState<T>? maybeOf<T extends INuRouter>(
BuildContext context, {
bool rootNuvigator = false,
bool nullOk = false,
}) {
if (rootNuvigator) {
return context.findRootAncestorStateOfType<NuvigatorState<T>>();
} else {
final closestNuvigator =
context.findAncestorStateOfType<NuvigatorState<T>>();
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<T> of<T extends INuRouter>(
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;
Expand Down

0 comments on commit b70ea30

Please sign in to comment.