Skip to content

Commit 71d0de7

Browse files
committed
Support nested navigator
- #1
1 parent e3f1af0 commit 71d0de7

13 files changed

+125
-10
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 0.4.0
2+
3+
- Support nested navigator
4+
- Expose `useRootNavigator`
5+
- It works fine without specifying `useRootNavigator` in most cases
6+
- Check [example/lib/nested_navigator_page.dart](https://github.com/mono0926/adaptive_dialog/blob/master/example/lib/pages/nested_navigator_page.dart)
7+
18
## 0.3.1
29

310
- Add `showDialog2020`, which wraps `showModal` with `FadeScaleTransitionConfiguration()`.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import 'package:adaptive_dialog/adaptive_dialog.dart';
2+
import 'package:example/util/util.dart';
3+
import 'package:flutter/material.dart';
4+
5+
class NestedNavigatorPage extends StatelessWidget {
6+
const NestedNavigatorPage({Key key}) : super(key: key);
7+
8+
static const routeName = '/nested_navigator';
9+
10+
@override
11+
Widget build(BuildContext context) {
12+
final initialRoute = routeName.replaceFirst('/', '');
13+
return Navigator(
14+
initialRoute: initialRoute,
15+
onGenerateRoute: (settings) {
16+
assert(settings.name == initialRoute);
17+
return MaterialPageRoute<void>(
18+
builder: (context) => const _RootPage(),
19+
);
20+
},
21+
);
22+
}
23+
}
24+
25+
class _RootPage extends StatelessWidget {
26+
const _RootPage({Key key}) : super(key: key);
27+
@override
28+
Widget build(BuildContext context) {
29+
return Scaffold(
30+
appBar: AppBar(
31+
leading: BackButton(
32+
onPressed: () => Navigator.of(
33+
context,
34+
rootNavigator: !ModalRoute.of(context).canPop,
35+
).pop(),
36+
),
37+
),
38+
body: ListView(
39+
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
40+
children: <Widget>[
41+
RaisedButton(
42+
child: const Text('Dialog'),
43+
onPressed: () async {
44+
final result = await showOkAlertDialog(
45+
context: context,
46+
title: 'Title',
47+
message: 'This is message.',
48+
);
49+
logger.info(result);
50+
},
51+
),
52+
RaisedButton(
53+
child: const Text('Sheet'),
54+
onPressed: () async {
55+
final result = await showModalActionSheet<String>(
56+
context: context,
57+
actions: [
58+
SheetAction(
59+
icon: Icons.info,
60+
label: 'Hello',
61+
key: 'helloKey',
62+
),
63+
],
64+
);
65+
logger.info(result);
66+
},
67+
),
68+
const Divider(),
69+
RaisedButton(
70+
child: const Text('Next Page'),
71+
onPressed: () {
72+
Navigator.of(context).push<void>(MaterialPageRoute(
73+
builder: (context) => const _RootPage(),
74+
));
75+
},
76+
),
77+
],
78+
),
79+
);
80+
}
81+
}

example/lib/router.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import 'package:flutter/material.dart';
33
import 'package:recase/recase.dart';
44

55
import 'pages/alert_page.dart';
6+
import 'pages/nested_navigator_page.dart';
67
import 'pages/sheet_page.dart';
78

89
class Router {
910
final Map<String, WidgetBuilder> pushRoutes = {
1011
AlertPage.routeName: (context) => const AlertPage(),
1112
SheetPage.routeName: (context) => const SheetPage(),
1213
TextInputDialogPage.routeName: (context) => const TextInputDialogPage(),
14+
NestedNavigatorPage.routeName: (context) => const NestedNavigatorPage(),
1315
};
1416

1517
Route onGenerateRoute(RouteSettings settings) {
@@ -42,5 +44,6 @@ class PageInfo {
4244
AlertPage.routeName,
4345
SheetPage.routeName,
4446
TextInputDialogPage.routeName,
47+
NestedNavigatorPage.routeName,
4548
].map((rn) => PageInfo(routeName: rn)).toList();
4649
}

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages:
77
path: ".."
88
relative: true
99
source: path
10-
version: "0.3.1"
10+
version: "0.4.0"
1111
animations:
1212
dependency: transitive
1313
description:

lib/src/action_callback.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
typedef ActionCallback<T> = void Function(T key);

lib/src/alert_dialog/alert_dialog_action.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
import 'package:adaptive_dialog/src/action_callback.dart';
12
import 'package:adaptive_dialog/src/modal_action_sheet/sheet_action.dart';
23
import 'package:flutter/cupertino.dart';
34
import 'package:flutter/material.dart';
45

5-
typedef ActionCallback<T> = void Function(T key);
6-
76
/// Used for specifying showAlertDialog's actions.
87
@immutable
98
class AlertDialogAction<T> {

lib/src/alert_dialog/show_alert_dialog.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'package:adaptive_dialog/adaptive_dialog.dart';
2-
import 'package:animations/animations.dart';
32
import 'package:flutter/cupertino.dart';
43
import 'package:flutter/material.dart';
54

@@ -16,8 +15,12 @@ Future<T> showAlertDialog<T>({
1615
bool barrierDismissible = true,
1716
AdaptiveStyle style = AdaptiveStyle.adaptive,
1817
bool useActionSheetForCupertino = false,
18+
bool useRootNavigator = true,
1919
}) {
20-
void pop(T key) => Navigator.of(context).pop(key);
20+
void pop(T key) => Navigator.of(
21+
context,
22+
rootNavigator: useRootNavigator,
23+
).pop(key);
2124
final theme = Theme.of(context);
2225
final colorScheme = theme.colorScheme;
2326
final isCupertinoStyle = style.isCupertinoStyle(theme);
@@ -29,13 +32,15 @@ Future<T> showAlertDialog<T>({
2932
cancelLabel: actions.findCancelLabel(),
3033
actions: actions.convertToSheetActions(),
3134
style: style,
35+
useRootNavigator: useRootNavigator,
3236
);
3337
}
3438
final titleText = title == null ? null : Text(title);
3539
final messageText = message == null ? null : Text(message);
3640
return style.isCupertinoStyle(theme)
3741
? showCupertinoDialog(
3842
context: context,
43+
useRootNavigator: useRootNavigator,
3944
builder: (context) => CupertinoAlertDialog(
4045
title: titleText,
4146
content: messageText,
@@ -46,6 +51,7 @@ Future<T> showAlertDialog<T>({
4651
)
4752
: showDialog2020(
4853
context: context,
54+
useRootNavigator: useRootNavigator,
4955
builder: (context) => AlertDialog(
5056
title: titleText,
5157
content: messageText,

lib/src/alert_dialog/show_ok_alert_dialog.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Future<OkCancelResult> showOkAlertDialog({
1414
bool barrierDismissible = true,
1515
AdaptiveStyle alertStyle = AdaptiveStyle.adaptive,
1616
bool useActionSheetForCupertino = false,
17+
bool useRootNavigator = true,
1718
}) async {
1819
final result = await showAlertDialog<OkCancelResult>(
1920
context: context,
@@ -22,6 +23,7 @@ Future<OkCancelResult> showOkAlertDialog({
2223
barrierDismissible: barrierDismissible,
2324
style: alertStyle,
2425
useActionSheetForCupertino: useActionSheetForCupertino,
26+
useRootNavigator: useRootNavigator,
2527
actions: [
2628
AlertDialogAction(
2729
label: okLabel ?? MaterialLocalizations.of(context).okButtonLabel,

lib/src/alert_dialog/show_ok_cancel_alert_dialog.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Future<OkCancelResult> showOkCancelAlertDialog({
2121
bool barrierDismissible = true,
2222
AdaptiveStyle alertStyle = AdaptiveStyle.adaptive,
2323
bool useActionSheetForCupertino = false,
24+
bool useRootNavigator = true,
2425
}) async {
2526
final isCupertinoStyle = Theme.of(context).isCupertinoStyle;
2627
String defaultCancelLabel() {
@@ -35,6 +36,7 @@ Future<OkCancelResult> showOkCancelAlertDialog({
3536
barrierDismissible: barrierDismissible,
3637
style: alertStyle,
3738
useActionSheetForCupertino: useActionSheetForCupertino,
39+
useRootNavigator: useRootNavigator,
3840
actions: [
3941
AlertDialogAction(
4042
label: cancelLabel ?? defaultCancelLabel(),

lib/src/modal_action_sheet/cupertino_modal_action_sheet.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:adaptive_dialog/src/action_callback.dart';
12
import 'package:flutter/cupertino.dart';
23
import 'package:flutter/material.dart';
34
import 'package:adaptive_dialog/src/extensions/extensions.dart';
@@ -6,20 +7,21 @@ import 'sheet_action.dart';
67
class CupertinoModalActionSheet<T> extends StatelessWidget {
78
const CupertinoModalActionSheet({
89
Key key,
10+
@required this.onPressed,
911
this.title,
1012
this.message,
1113
this.actions,
1214
this.cancelLabel,
1315
}) : super(key: key);
1416

17+
final ActionCallback<T> onPressed;
1518
final String title;
1619
final String message;
1720
final List<SheetAction<T>> actions;
1821
final String cancelLabel;
1922

2023
@override
2124
Widget build(BuildContext context) {
22-
void pop(T key) => Navigator.of(context).pop(key);
2325
return CupertinoActionSheet(
2426
title: title == null ? null : Text(title),
2527
message: message == null ? null : Text(message),
@@ -31,14 +33,14 @@ class CupertinoModalActionSheet<T> extends StatelessWidget {
3133
.capitalizedForce,
3234
),
3335
isDefaultAction: !actions.any((a) => a.isDefaultAction),
34-
onPressed: () => pop(null),
36+
onPressed: () => onPressed(null),
3537
),
3638
actions: actions
3739
.map((a) => CupertinoActionSheetAction(
3840
child: Text(a.label),
3941
isDestructiveAction: a.isDestructiveAction,
4042
isDefaultAction: a.isDefaultAction,
41-
onPressed: () => pop(a.key),
43+
onPressed: () => onPressed(a.key),
4244
))
4345
.toList(),
4446
);

0 commit comments

Comments
 (0)