-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindexed_page.dart
104 lines (97 loc) · 3.28 KB
/
indexed_page.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import 'dart:io';
import 'dart:math';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
/// Page that displays its index, flow's title and color.
///
/// Has a button for pushing another one of its kind with an incremented index,
/// and another button for starting a new flow named 'New' with
/// a random background color.
class IndexedPage extends StatelessWidget {
const IndexedPage({
@required this.index,
@required this.backgroundColor,
@required this.containingFlowTitle,
Key key,
}) : assert(index != null),
assert(backgroundColor != null),
super(key: key);
final int index;
final Color backgroundColor;
final String containingFlowTitle;
@override
Widget build(BuildContext context) {
var pageTitle = 'Page $index';
if (containingFlowTitle != null) {
pageTitle += ' of $containingFlowTitle Flow';
}
return Scaffold(
backgroundColor: backgroundColor,
appBar: AppBar(
title: Text(
pageTitle,
maxLines: 1,
),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Horizontally pushes a new screen.
RaisedButton(
onPressed: () {
_pushPage(context, true);
},
child: const Text('NEXT PAGE (HORIZONTALLY)'),
),
// Vertically pushes a new screen / Starts a new flow.
// In a real world scenario, this could be an authentication flow
// where the user can choose to sign in or sign up.
RaisedButton(
onPressed: () {
_pushPage(context, false);
},
child: const Text('NEXT FLOW (VERTICALLY)'),
),
],
),
),
);
}
void _pushPage(BuildContext context, bool isHorizontalNavigation) {
// If it's not horizontal navigation,
// we should use the rootNavigator.
Navigator.of(context, rootNavigator: !isHorizontalNavigation).push(
_buildAdaptivePageRoute(
builder: (context) => IndexedPage(
// If it's a new flow, the displayed index should be 1 again.
index: isHorizontalNavigation ? index + 1 : 1,
// If it's a new flow, we'll randomize its color.
backgroundColor: isHorizontalNavigation
? backgroundColor
: Colors.primaries[Random().nextInt(Colors.primaries.length)],
// If it's starting a new flow let's just call it 'New.'
containingFlowTitle:
isHorizontalNavigation ? containingFlowTitle : 'New',
),
fullscreenDialog: !isHorizontalNavigation,
),
);
}
// Flutter will use the fullscreenDialog property to change the animation
// and the app bar's left icon to an X instead of an arrow.
PageRoute<T> _buildAdaptivePageRoute<T>({
@required WidgetBuilder builder,
bool fullscreenDialog = false,
}) =>
Platform.isAndroid
? MaterialPageRoute(
builder: builder,
fullscreenDialog: fullscreenDialog,
)
: CupertinoPageRoute(
builder: builder,
fullscreenDialog: fullscreenDialog,
);
}