Skip to content

Commit

Permalink
Merge pull request #95 from nubank/fix-should-rebuild-renders
Browse files Browse the repository at this point in the history
Fix NuRouteLoader by caching the NuRouter instead of the Widget
  • Loading branch information
leoiacovini authored May 2, 2022
2 parents dbf9983 + a5fe247 commit aecb79b
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2

job_defaults: &defaults
docker:
- image: cirrusci/flutter
- image: cirrusci/flutter:2.2.1

jobs:
format-check:
Expand All @@ -21,7 +21,7 @@ jobs:
steps:
- checkout
- run: flutter pub get
- run: flutter test
- run: flutter test

workflows:
version: 2
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 1.6.1
- [FIX] Fix re-renders due to other state changes when `Nuvigator.shouldRebuild` returns false

## 1.6.0
- [ENHANCEMENT] Add `Nuvigator.shouldRebuild` property for controlling when a Nuvigator should be rebuild.

Expand Down
1 change: 1 addition & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class MyApp extends StatelessWidget {
child: ChangeNotifierProvider.value(
value: FriendRequestBloc(10),
child: Nuvigator(
shouldRebuild: (_, __) => false,
debug: true,
router: MainAppRouter(),
),
Expand Down
1 change: 1 addition & 0 deletions example/lib/samples/modules/composer/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ 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
18 changes: 10 additions & 8 deletions example/lib/samples/modules/composer/screens/help_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ class HelpScreen extends StatelessWidget {
body: const Padding(
padding: EdgeInsets.all(8),
child: Text(
'You can use the text composer to compose text. It will return the '
'text to whoever called this screen when you tap the publish button, '
'on the bottom of the Text Field. If you came from the Home screen, '
'it will pop up a message on the Home Screen with your composition. '
'If you decide to go back instead, it will return null and nothing '
'will be displayed. If you did not come from the Home screen, '
'nothing will happen either way. Contrary to what it looks like, '
'the text will not actually be posted anywhere :)'),
'You can use the text composer to compose text. It will return the '
'text to whoever called this screen when you tap the publish button, '
'on the bottom of the Text Field. If you came from the Home screen, '
'it will pop up a message on the Home Screen with your composition. '
'If you decide to go back instead, it will return null and nothing '
'will be displayed. If you did not come from the Home screen, '
'nothing will happen either way. Contrary to what it looks like, '
'the text will not actually be posted anywhere :)',
style: TextStyle(fontSize: 20),
),
),
);
}
Expand Down
15 changes: 7 additions & 8 deletions lib/src/next/v1/nu_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ class NuRouterLoader extends StatefulWidget {
}

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

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

Future<void> _initModule() async {
setState(() {
loading = widget.router.awaitForInit;
router = widget.router;
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 @@ -462,7 +463,6 @@ class _NuRouterLoaderState extends State<NuRouterLoader> {
void didUpdateWidget(covariant NuRouterLoader oldWidget) {
if (widget.shouldRebuild(oldWidget.router, widget.router)) {
_initModule();
nuvigator = widget.builder(widget.router);
}
super.didUpdateWidget(oldWidget);
}
Expand All @@ -476,11 +476,10 @@ 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;
return widget.builder(router);
}
}
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.0
version: 1.6.1

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

Expand Down

0 comments on commit aecb79b

Please sign in to comment.