-
Notifications
You must be signed in to change notification settings - Fork 26.8k
/
app.dart
215 lines (191 loc) · 6.9 KB
/
app.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
import '../../data/gallery_options.dart';
import '../../gallery_localizations.dart';
import '../../layout/adaptive.dart';
import 'backdrop.dart';
import 'category_menu_page.dart';
import 'expanding_bottom_sheet.dart';
import 'home.dart';
import 'login.dart';
import 'model/app_state_model.dart';
import 'model/product.dart';
import 'page_status.dart';
import 'routes.dart' as routes;
import 'scrim.dart';
import 'supplemental/layout_cache.dart';
import 'theme.dart';
class ShrineApp extends StatefulWidget {
const ShrineApp({super.key});
static const String loginRoute = routes.loginRoute;
static const String homeRoute = routes.homeRoute;
@override
State<ShrineApp> createState() => _ShrineAppState();
}
class _ShrineAppState extends State<ShrineApp>
with TickerProviderStateMixin, RestorationMixin {
// Controller to coordinate both the opening/closing of backdrop and sliding
// of expanding bottom sheet
late AnimationController _controller;
// Animation Controller for expanding/collapsing the cart menu.
late AnimationController _expandingController;
final _RestorableAppStateModel _model = _RestorableAppStateModel();
final RestorableDouble _expandingTabIndex = RestorableDouble(0);
final RestorableDouble _tabIndex = RestorableDouble(1);
final Map<String, List<List<int>>> _layouts = <String, List<List<int>>>{};
@override
String get restorationId => 'shrine_app_state';
@override
void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
registerForRestoration(_model, 'app_state_model');
registerForRestoration(_tabIndex, 'tab_index');
registerForRestoration(
_expandingTabIndex,
'expanding_tab_index',
);
_controller.value = _tabIndex.value;
_expandingController.value = _expandingTabIndex.value;
}
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 450),
value: 1,
);
// Save state restoration animation values only when the cart page
// fully opens or closes.
_controller.addStatusListener((AnimationStatus status) {
if (status == AnimationStatus.completed ||
status == AnimationStatus.dismissed) {
_tabIndex.value = _controller.value;
}
});
_expandingController = AnimationController(
duration: const Duration(milliseconds: 500),
vsync: this,
);
// Save state restoration animation values only when the menu page
// fully opens or closes.
_expandingController.addStatusListener((AnimationStatus status) {
if (status == AnimationStatus.completed ||
status == AnimationStatus.dismissed) {
_expandingTabIndex.value = _expandingController.value;
}
});
}
@override
void dispose() {
_controller.dispose();
_expandingController.dispose();
_tabIndex.dispose();
_expandingTabIndex.dispose();
super.dispose();
}
Widget mobileBackdrop() {
return Backdrop(
frontLayer: const ProductPage(),
backLayer: CategoryMenuPage(onCategoryTap: () => _controller.forward()),
frontTitle: const Text('SHRINE'),
backTitle: Text(GalleryLocalizations.of(context)!.shrineMenuCaption),
controller: _controller,
);
}
Widget desktopBackdrop() {
return const DesktopBackdrop(
frontLayer: ProductPage(),
backLayer: CategoryMenuPage(),
);
}
// Closes the bottom sheet if it is open.
Future<bool> _onWillPop() async {
final AnimationStatus status = _expandingController.status;
if (status == AnimationStatus.completed ||
status == AnimationStatus.forward) {
await _expandingController.reverse();
return false;
}
return true;
}
@override
Widget build(BuildContext context) {
final Widget home = LayoutCache(
layouts: _layouts,
child: PageStatus(
menuController: _controller,
cartController: _expandingController,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) => HomePage(
backdrop: isDisplayDesktop(context)
? desktopBackdrop()
: mobileBackdrop(),
scrim: Scrim(controller: _expandingController),
expandingBottomSheet: ExpandingBottomSheet(
hideController: _controller,
expandingController: _expandingController,
),
),
),
),
);
return ScopedModel<AppStateModel>(
model: _model.value,
// ignore: deprecated_member_use
child: WillPopScope(
onWillPop: _onWillPop,
child: MaterialApp(
// By default on desktop, scrollbars are applied by the
// ScrollBehavior. This overrides that. All vertical scrollables in
// the gallery need to be audited before enabling this feature,
// see https://github.com/flutter/gallery/issues/541
scrollBehavior:
const MaterialScrollBehavior().copyWith(scrollbars: false),
restorationScopeId: 'shrineApp',
title: 'Shrine',
debugShowCheckedModeBanner: false,
initialRoute: ShrineApp.loginRoute,
routes: <String, WidgetBuilder>{
ShrineApp.loginRoute: (BuildContext context) => const LoginPage(),
ShrineApp.homeRoute: (BuildContext context) => home,
},
theme: shrineTheme.copyWith(
platform: GalleryOptions.of(context).platform,
),
// L10n settings.
localizationsDelegates: GalleryLocalizations.localizationsDelegates,
supportedLocales: GalleryLocalizations.supportedLocales,
locale: GalleryOptions.of(context).locale,
),
),
);
}
}
class _RestorableAppStateModel extends RestorableListenable<AppStateModel> {
@override
AppStateModel createDefaultValue() => AppStateModel()..loadProducts();
@override
AppStateModel fromPrimitives(Object? data) {
final AppStateModel appState = AppStateModel()..loadProducts();
final Map<String, dynamic> appData = Map<String, dynamic>.from(data! as Map<dynamic, dynamic>);
// Reset selected category.
final int categoryIndex = appData['category_index'] as int;
appState.setCategory(categories[categoryIndex]);
// Reset cart items.
final Map<dynamic, dynamic> cartItems = appData['cart_data'] as Map<dynamic, dynamic>;
cartItems.forEach((dynamic id, dynamic quantity) {
appState.addMultipleProductsToCart(id as int, quantity as int);
});
return appState;
}
@override
Object toPrimitives() {
return <String, dynamic>{
'cart_data': value.productsInCart,
'category_index': categories.indexOf(value.selectedCategory),
};
}
}