Skip to content

Commit

Permalink
Add example showing an unintuitive situation
Browse files Browse the repository at this point in the history
where `MappedNavigatableSource` instances can be created in `build()` methods, which would then be recreated (and thus loose all state) on higher-level rebuilds/hot reloads
  • Loading branch information
tp committed Sep 4, 2024
1 parent 7031df3 commit 1e5195f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
8 changes: 6 additions & 2 deletions example/lib/src/examples/example_selection_navigator.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:fdr/fdr.dart';
import 'package:fdr_example/src/examples/dynamic_pop_navigator.dart';
import 'package:fdr_example/src/examples/hot_reloadable_navigator.dart';
import 'package:fdr_example/src/examples/hot_reloadable_mapped_navigator.dart';
import 'package:fdr_example/src/examples/hot_reloadable_stateful_navigator.dart';
import 'package:fdr_example/src/examples/hot_reloadable_stateless_navigator.dart';
import 'package:fdr_example/src/examples/list_detail_navigator.dart';
import 'package:fdr_example/src/examples/overlay_portal_navigator.dart';
import 'package:flutter/cupertino.dart';
Expand Down Expand Up @@ -29,7 +31,9 @@ class ExampleSelectionNavigator
examples: {
'List Detail': () => ListDetailNavigator(),
'Dynamic back behavior': () => DynamicPopNavigator(),
'Stateful Navigator w/ hot reload': () => HotReloadableNavigator(),
'Stateful Navigator': () => HotReloadableStatefulNavigator(),
'Stateless Navigator': () => HotReloadableStatelessNavigator(),
'Mapped Navigator': () => HotReloadableMappedNavigator(),
'Overlay Portal': () => OverlayPortalNavigator(),
},
onExampleSelect: (exampleFactory) => this.state = exampleFactory(),
Expand Down
39 changes: 39 additions & 0 deletions example/lib/src/examples/hot_reloadable_mapped_navigator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'package:fdr/fdr.dart';
import 'package:flutter/material.dart';

class HotReloadableMappedNavigator extends StatelessNavigator {
@override
List<DeclarativeNavigatable> build() {
return [
MappedNavigatorDemo(),
];
}
}

class MappedNavigatorDemo extends MappedNavigatableSource<int> {
MappedNavigatorDemo() : super(initialState: 0);

@override
List<DeclarativeNavigatable> build() {
return [
Scaffold(
appBar: AppBar(
title: const Text('Stateless Navigator'),
),
body: Center(
child: Column(
children: [
Text('Count: $state'),
FilledButton(
onPressed: () {
state++;
},
child: const Text('Increment'),
)
],
),
),
).page(onPop: null),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:fdr/fdr.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class HotReloadableNavigator extends StatelessNavigator {
class HotReloadableStatefulNavigator extends StatelessNavigator {
@override
List<DeclarativeNavigatable> build() {
return [
Expand Down
34 changes: 34 additions & 0 deletions example/lib/src/examples/hot_reloadable_stateless_navigator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:fdr/fdr.dart';
import 'package:flutter/material.dart';

class HotReloadableStatelessNavigator extends StatelessNavigator {
@override
List<DeclarativeNavigatable> build() {
return [
StatelessNavigatorDemo(greeting: 'hello, world!'),
];
}
}

class StatelessNavigatorDemo extends StatelessNavigator {
@visibleForTesting
StatelessNavigatorDemo({
required this.greeting,
});

final String greeting;

@override
List<DeclarativeNavigatable> build() {
return [
Scaffold(
appBar: AppBar(
title: const Text('Stateless Navigator'),
),
body: Center(
child: Text(greeting),
),
).page(onPop: null),
];
}
}

0 comments on commit 1e5195f

Please sign in to comment.