page.dart
4.51 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:sheet/route.dart';
class Book {
const Book(this.title, this.author);
final String title;
final String author;
}
class BooksApp extends StatefulWidget {
@override
State<StatefulWidget> createState() => _BooksAppState();
}
class _BooksAppState extends State<BooksApp> {
Book? _selectedBook;
List<Book> books = <Book>[
const Book('Stranger in a Strange Land', 'Robert A. Heinlein'),
const Book('Foundation', 'Isaac Asimov'),
const Book('Fahrenheit 451', 'Ray Bradbury'),
];
Brightness brightness = Brightness.light;
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme:
brightness == Brightness.light ? ThemeData.light() : ThemeData.dark(),
title: 'Books App',
builder: (BuildContext context, Widget? child) {
return CupertinoTheme(
data: CupertinoThemeData(brightness: brightness),
child: child!,
);
},
home: Navigator(
pages: <Page<dynamic>>[
MaterialPage<bool>(
key: const ValueKey<String>('BooksListPage'),
child: BooksListScreen(
books: books,
onTapped: _handleBookTapped,
onBrigthnessChanged: (Brightness brightness) {
setState(() {
this.brightness = brightness;
});
},
),
),
if (_selectedBook != null)
SheetPage<void>(
key: ValueKey<Book?>(_selectedBook),
child: BookDetailsScreen(book: _selectedBook!),
barrierColor: Colors.black26)
],
onPopPage: (Route<dynamic> route, dynamic result) {
if (!route.didPop(result)) {
return false;
}
// Update the list of pages by setting _selectedBook to null
setState(() {
_selectedBook = null;
});
return true;
},
),
);
}
void _handleBookTapped(Book book) {
setState(() {
_selectedBook = book;
});
}
}
class BooksListScreen extends StatelessWidget {
const BooksListScreen({
required this.books,
required this.onTapped,
required this.onBrigthnessChanged,
});
final List<Book> books;
final ValueChanged<Book> onTapped;
final void Function(Brightness) onBrigthnessChanged;
@override
Widget build(BuildContext context) {
final Brightness brightness = Theme.of(context).brightness;
return Scaffold(
appBar: CupertinoNavigationBar(
leading: BackButton(onPressed: () {
Navigator.of(context, rootNavigator: true).pop();
}),
middle: const Text('Book'),
trailing: IconButton(
icon: Icon(brightness == Brightness.light
? Icons.nightlight_round
: Icons.wb_sunny),
onPressed: () {
onBrigthnessChanged(
brightness == Brightness.light
? Brightness.dark
: Brightness.light,
);
},
),
),
body: SafeArea(
child: ListView(
children: <Widget>[
for (Book book in books)
ListTile(
title: Text(book.title),
subtitle: Text(book.author),
onTap: () => onTapped(book),
)
],
),
),
);
}
}
class BookDetailsScreen extends StatelessWidget {
const BookDetailsScreen({
required this.book,
});
final Book book;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const CupertinoNavigationBar(
middle: Text('Book'),
),
body: Padding(
padding: const EdgeInsets.all(8.0) + const EdgeInsets.only(top: 52.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(book.title, style: Theme.of(context).textTheme.titleLarge),
Text(book.author, style: Theme.of(context).textTheme.titleMedium),
],
),
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.add),
onPressed: () {
Navigator.of(context)
.push(MaterialPageRoute<void>(builder: (BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Invalid'),
),
);
}));
},
),
);
}
}