-
Notifications
You must be signed in to change notification settings - Fork 355
/
popback.dart
141 lines (131 loc) · 3.65 KB
/
popback.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(
const App(),
);
}
// Define the name of the route as a constant so that
// you can use it in the Navigator to push the route using
// its name, as well as use it to create the list of your app-routes.
const detailsPageRouteName = '/details';
class App extends StatelessWidget {
const App({
super.key,
});
@override
Widget build(BuildContext context) {
// Return a [CupertinoApp] that, by default,
// has the look and feel of an iOS app.
return CupertinoApp(
debugShowCheckedModeBanner: false,
home: const HomePage(),
// Define your routes using a Map where the keys
// are the route names and the values are
// a function that receives a BuildContext and returns
// the corresponding Widget.
routes: {
detailsPageRouteName: (context) => const DetailsPage(),
},
);
}
}
// Create a class that holds each person's data.
@immutable
class Person {
final String name;
final int age;
const Person({
required this.name,
required this.age,
});
}
// Next, create a list of 100 persons.
final mockPersons = Iterable.generate(
100,
(index) => Person(
name: 'Person #${index + 1}',
age: 10 + index,
),
);
// This stateless widget displays the list of persons
// that we get from the mockPersons list and allows the user
// to tap each person to see their details.
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: const CupertinoNavigationBar(
middle: Text(
'Pick a person',
),
),
child: Material(
child: ListView.builder(
itemCount: mockPersons.length,
itemBuilder: (context, index) {
final person = mockPersons.elementAt(index);
final age = '${person.age} years old';
return ListTile(
title: Text(person.name),
subtitle: Text(age),
trailing: const Icon(
Icons.arrow_forward_ios,
),
onTap: () {
// When a [ListTile] that represents a person is
// tapped, push the [detailsPageRouteName] route
// to the [Navigator] and pass the person's instance
// to the route.
Navigator.of(context).pushNamed(
detailsPageRouteName,
arguments: person,
);
},
);
},
),
),
);
}
}
class DetailsPage extends StatelessWidget {
const DetailsPage({super.key});
@override
Widget build(BuildContext context) {
final Person person = ModalRoute.of(
context,
)?.settings.arguments as Person;
final age = '${person.age} years old';
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Text(
person.name,
),
),
child: SafeArea(
child: Material(
child: Column(
children: [
ListTile(
title: Text(person.name),
subtitle: Text(age),
),
// #docregion pop-back
TextButton(
onPressed: () {
// This code allows the
// view to pop back to its presenter.
Navigator.of(context).pop();
},
child: const Text('Pop back'),
),
// #enddocregion pop-back
],
),
),
),
);
}
}