Skip to content

Commit

Permalink
Merge pull request #97 from nubank/dont-rebuild-by-default
Browse files Browse the repository at this point in the history
Update Nuvigator default behavior to not reload when NuRouter changes
  • Loading branch information
leoiacovini authored May 13, 2022
2 parents 9d810b9 + 462cbc7 commit 191e75b
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 19 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 1.7.0-beta.0
- [FIX] [POSSIBLY BREAKING] Makes the default behavior of Nuvigator to not re-build itself and it's state when the NuRouter provided instance changes. This allows for fixing some state reloads/resets and navigation lost, specially on nested flows. If for some reason you were relying in the current behavior for some feature, you can provide a `shouldRebuild` property to the `Nuvigator` that should return `true` when you wish to perform the rebuild.

## 1.6.2
- [FIX] Revert changes from 1.6.1 due to some unexpected behaviors found

Expand Down
1 change: 0 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class MyApp extends StatelessWidget {
child: ChangeNotifierProvider.value(
value: FriendRequestBloc(10),
child: Nuvigator(
shouldRebuild: (_, __) => false,
debug: true,
router: MainAppRouter(),
),
Expand Down
1 change: 0 additions & 1 deletion example/lib/samples/modules/composer/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class ComposerRoute extends NuRoute<NuRouter, void, String> {
@override
Widget build(BuildContext context, NuRouteSettings<Object> settings) {
return Nuvigator.routes(
shouldRebuild: (_, __) => false,
initialRoute: settings.name,
routes: [
_ComposerHelpRoute(),
Expand Down
25 changes: 17 additions & 8 deletions lib/src/next/v1/nu_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ class NuRouterLoader extends StatefulWidget {

class _NuRouterLoaderState extends State<NuRouterLoader> {
Widget nuvigator;
NuRouter router;
bool loading;
Widget errorWidget;

Expand All @@ -436,16 +437,17 @@ class _NuRouterLoaderState extends State<NuRouterLoader> {
}

Future<void> _initModule() async {
router = widget.router;
setState(() {
loading = widget.router.awaitForInit;
loading = router.awaitForInit;
errorWidget = null;
});
try {
await widget.router._init(context);
await router._init(context);
} catch (error, stackTrace) {
debugPrintStack(stackTrace: stackTrace, label: error.toString());
final errorWidget =
widget.router.onError(error, NuRouterController(reload: _reload));
router.onError(error, NuRouterController(reload: _reload));
if (errorWidget != null) {
setState(() {
this.errorWidget = errorWidget;
Expand All @@ -458,11 +460,14 @@ class _NuRouterLoaderState extends State<NuRouterLoader> {
}
}

bool get _shouldRebuildProvided => widget.shouldRebuild != null;

@override
void didUpdateWidget(covariant NuRouterLoader oldWidget) {
if (widget.shouldRebuild(oldWidget.router, widget.router)) {
if (_shouldRebuildProvided &&
widget.shouldRebuild(oldWidget.router, widget.router)) {
_initModule();
nuvigator = widget.builder(widget.router);
nuvigator = widget.builder(router);
}
super.didUpdateWidget(oldWidget);
}
Expand All @@ -476,11 +481,15 @@ class _NuRouterLoaderState extends State<NuRouterLoader> {
@override
Widget build(BuildContext context) {
if (loading) {
return widget.router.loadingWidget;
return router.loadingWidget;
} else if (errorWidget != null) {
return errorWidget;
}
nuvigator ??= widget.builder(widget.router);
return nuvigator;
if (_shouldRebuildProvided) {
nuvigator ??= widget.builder(router);
return nuvigator;
} else {
return widget.builder(router);
}
}
}
5 changes: 1 addition & 4 deletions lib/src/nuvigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,6 @@ class NuvigatorState<T extends INuRouter> extends NavigatorState
}
}

bool _defaultShouldRebuild(NuRouter previousRouter, NuRouter newRouter) =>
previousRouter != newRouter;

/// Creates a new Nuvigator. When using the Next API, several of those options
/// are provided by the [NuRouter]. Providing them here will thrown an assertion
/// error.
Expand Down Expand Up @@ -622,7 +619,7 @@ class Nuvigator<T extends INuRouter> extends StatelessWidget {
return NuRouterLoader(
// ignore: avoid_as
router: router as NuRouter,
shouldRebuild: shouldRebuild ?? _defaultShouldRebuild,
shouldRebuild: shouldRebuild,
builder: (moduleRouter) => _NuvigatorInner(
router: moduleRouter,
debug: debug,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: nuvigator
description: A powerful routing abstraction over Flutter navigator, providing some new features and an easy way to define routers.
version: 1.6.2
version: 1.7.0-beta.0

homepage: https://github.com/nubank/nuvigator

Expand Down
11 changes: 7 additions & 4 deletions test/nuvigator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,12 @@ void main() {
});

testWidgets(
'Should rebuild Nuvigator by default when NuRouter instance changes',
'Should Nuvigator when shouldRebuild is provided',
(tester) async {
final tracker = await pumpApp(tester);
final tracker = await pumpApp(
tester,
shouldRebuild: (oldRouter, newRouter) => oldRouter != newRouter,
);

// Go to Screen3
unawaited(tracker.rootNuvigator.pushNamed('screen3'));
Expand Down Expand Up @@ -580,9 +583,9 @@ void main() {
);

testWidgets(
'Should not rebuild Nuvigator when shouldRebuild returns false',
'Should not rebuild Nuvigator by default when shouldRebuild is not provided',
(tester) async {
final tracker = await pumpApp(tester, shouldRebuild: (_, __) => false);
final tracker = await pumpApp(tester);

// Go to Screen3
unawaited(tracker.rootNuvigator.pushNamed('screen3'));
Expand Down

0 comments on commit 191e75b

Please sign in to comment.