-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdrinks_screen.dart
More file actions
419 lines (398 loc) · 14 KB
/
Copy pathdrinks_screen.dart
File metadata and controls
419 lines (398 loc) · 14 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../domain/models/models.dart';
import '../providers/providers.dart';
import '../utils/utils.dart';
import '../widgets/widgets.dart';
/// Main screen showing the list of drinks
class DrinksScreen extends StatefulWidget {
const DrinksScreen({required this.festivalId, super.key});
final String festivalId;
@override
State<DrinksScreen> createState() => _DrinksScreenState();
}
class _DrinksScreenState extends State<DrinksScreen> {
final _searchController = TextEditingController();
bool _showSearch = false;
Timer? _searchDebounceTimer;
void _onSearchChanged(String value) {
_searchDebounceTimer?.cancel();
_searchDebounceTimer = Timer(
const Duration(milliseconds: 300),
() => context.read<BeerProvider>().setSearchQuery(value),
);
}
@override
void dispose() {
_searchDebounceTimer?.cancel();
_searchController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final provider = context.watch<BeerProvider>();
// Festival-flash guard: the router schedules setFestival in a post-frame
// callback, so a URL-driven festival change (cross-festival deep link on a
// warm app, browser back/forward, the post-init redirect in main.dart)
// would otherwise render one frame of the previous festival's name and
// drinks before the provider catches up (issue #397). Keep it first in
// build(), as in MyFestivalScreen.
if (provider.currentFestival.id != widget.festivalId) {
return buildLoadingScaffold();
}
return PageTitle(
pageTitle: provider.currentFestival.name,
child: Scaffold(
body: Column(
children: [
Expanded(
child: RefreshIndicator(
onRefresh: provider.loadDrinks,
child: CustomScrollView(
slivers: [
SliverAppBar(
floating: true,
snap: true,
title: FestivalHeader(provider: provider),
actions: [buildOverflowMenu(context)],
),
SliverToBoxAdapter(
child: FestivalBanner(
provider: provider,
festivalId: widget.festivalId,
),
),
SliverToBoxAdapter(
child: _buildRefreshStatus(context, provider),
),
if (_showSearch)
SliverToBoxAdapter(
child: _buildSearchBar(context, provider),
),
_buildDrinksListSliver(context, provider),
],
),
),
),
// Bottom controls for filtering, sorting, and search - thumb friendly
_buildBottomControls(context, provider),
],
),
),
);
}
Widget _buildSearchBar(BuildContext context, BeerProvider provider) {
final theme = Theme.of(context);
return Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
color: theme.colorScheme.surface,
child: TextField(
controller: _searchController,
autofocus: true,
decoration: InputDecoration(
hintText: 'Search drinks, breweries, styles...',
prefixIcon: const Icon(Icons.search),
suffixIcon: Semantics(
label: 'Clear search',
hint: 'Double tap to clear search and close search bar',
button: true,
excludeSemantics: true,
child: IconButton(
icon: const Icon(Icons.close),
onPressed: () {
_searchDebounceTimer?.cancel();
// setState mutates widget-local state only. The provider call
// stays outside the closure: notifyListeners() marks watching
// elements dirty synchronously, and mixing that with an
// in-progress setState is what produces "setState() or
// markNeedsBuild() called during build" (issue #526).
setState(() {
_showSearch = false;
_searchController.clear();
});
provider.setSearchQuery('');
},
),
),
filled: true,
fillColor: theme.colorScheme.surfaceContainerHighest,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(28),
borderSide: BorderSide.none,
),
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 12,
),
),
onChanged: _onSearchChanged,
),
);
}
Widget _buildBottomControls(BuildContext context, BeerProvider provider) {
final hasStyleFilter = provider.availableStyles.isNotEmpty;
final styleLabel = provider.selectedStyles.isEmpty
? 'Style'
: provider.selectedStyles.length == 1
? provider.selectedStyles.first
: '${provider.selectedStyles.length} styles';
// Formatted and sorted so the screen reader announces the same names a
// sighted user sees, in a deterministic order (a Set has none).
final formattedCategories =
provider.selectedCategories
.map(BeverageTypeHelper.formatBeverageType)
.toList()
..sort();
final categoryLabel = provider.selectedCategories.isEmpty
? 'Category'
: formattedCategories.length == 1
? formattedCategories.first
: '${formattedCategories.length} categories';
return Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
child: Row(
children: [
Expanded(
child: FilterButton(
label: categoryLabel,
semanticLabel: formattedCategories.isEmpty
? 'Filter by category'
: 'Filter by category: ${formattedCategories.join(', ')}',
icon: Icons.filter_list,
onPressed: () => showCategoryFilter(context),
isActive: provider.selectedCategories.isNotEmpty,
),
),
if (hasStyleFilter) ...[
const SizedBox(width: 6),
Expanded(
child: FilterButton(
label: styleLabel,
semanticLabel: provider.selectedStyles.isEmpty
? 'Filter by style'
: 'Filter by style: ${provider.selectedStyles.join(', ')}',
icon: Icons.style,
onPressed: () => showStyleFilter(context),
isActive: provider.selectedStyles.isNotEmpty,
),
),
],
const SizedBox(width: 6),
Expanded(
child: FilterButton(
label: provider.currentSort.label,
semanticLabel: 'Sort drinks by ${provider.currentSort.label}',
icon: Icons.sort,
onPressed: () => showSortOptions(context),
isActive: false,
),
),
const SizedBox(width: 6),
VisibilityFilterButton(
activeCount:
provider.visibilityFilters.length +
provider.excludedAllergens.length,
onPressed: () => showVisibilityFilter(context),
),
const SizedBox(width: 6),
SearchButton(
isActive: _showSearch,
hasQuery: provider.searchQuery.isNotEmpty,
onPressed: () {
// Collapsing the search bar clears the query; expanding it does
// not. As with the clear button, setState keeps only the
// widget-local fields and the provider call runs after it
// (issue #526).
final isCollapsing = _showSearch;
if (isCollapsing) {
_searchDebounceTimer?.cancel();
}
setState(() {
_showSearch = !_showSearch;
if (isCollapsing) {
_searchController.clear();
}
});
if (isCollapsing) {
provider.setSearchQuery('');
}
},
),
],
),
);
}
/// Thin progress bar while a background refresh runs with data on screen, or
/// a dismissible notice when a refresh failed but cached data remains shown.
Widget _buildRefreshStatus(BuildContext context, BeerProvider provider) {
final theme = Theme.of(context);
// Test against the unfiltered list so an active filter (favourites only,
// search query with no matches) doesn't hide the refresh indicator.
final hasData = provider.allDrinks.isNotEmpty;
if (provider.refreshNotice != null && hasData) {
return Material(
color: theme.colorScheme.secondaryContainer,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Row(
children: [
Icon(
Icons.cloud_off,
size: 18,
color: theme.colorScheme.onSecondaryContainer,
),
const SizedBox(width: 8),
Expanded(
child: Text(
provider.refreshNotice!,
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSecondaryContainer,
),
),
),
Semantics(
label: 'Dismiss saved data notice',
hint: 'Double tap to dismiss',
button: true,
excludeSemantics: true,
child: IconButton(
visualDensity: VisualDensity.compact,
icon: Icon(
Icons.close,
size: 18,
color: theme.colorScheme.onSecondaryContainer,
),
onPressed: provider.dismissRefreshNotice,
),
),
],
),
),
);
}
if (provider.isRefreshing && hasData) {
return Semantics(
label: 'Refreshing drinks',
liveRegion: true,
child: const LinearProgressIndicator(minHeight: 2),
);
}
return const SizedBox.shrink();
}
Widget _buildDrinksListSliver(BuildContext context, BeerProvider provider) {
if (provider.isLoading && provider.drinks.isEmpty) {
return SliverFillRemaining(
hasScrollBody: false,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset('assets/app_icon.png', width: 80, height: 80),
const SizedBox(height: 16),
const CircularProgressIndicator(),
],
),
),
);
}
if (provider.error != null && provider.drinks.isEmpty) {
return SliverFillRemaining(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.error_outline,
size: 64,
color: Theme.of(context).colorScheme.error,
),
const SizedBox(height: 16),
Text(
'Error loading drinks',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 8),
Text(provider.error!, textAlign: TextAlign.center),
const SizedBox(height: 16),
Semantics(
label: 'Retry loading drinks',
hint: 'Double tap to reload festival data',
button: true,
excludeSemantics: true,
child: ElevatedButton(
onPressed: provider.loadDrinks,
child: const Text('Retry'),
),
),
],
),
),
);
}
if (provider.drinks.isEmpty) {
return SliverFillRemaining(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Opacity(
opacity: 0.5,
child: Image.asset(
'assets/app_icon.png',
width: 80,
height: 80,
),
),
const SizedBox(height: 16),
Text(
'No drinks found',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 8),
const Text('Try adjusting your filters'),
if (provider.selectedCategories.isNotEmpty) ...[
const SizedBox(height: 16),
Semantics(
label: 'Clear all category filters',
hint: 'Double tap to show every category',
button: true,
excludeSemantics: true,
child: OutlinedButton(
onPressed: () => provider.clearCategories(),
child: const Text('Clear Filters'),
),
),
],
],
),
),
);
}
return SliverPadding(
padding: const EdgeInsets.only(bottom: 16),
sliver: SliverList(
delegate: SliverChildBuilderDelegate((context, index) {
final drink = provider.drinks[index];
return DrinkCard(
key: ValueKey(drink.id),
drink: drink,
searchQuery: provider.searchQuery,
onTap: () => _navigateToDetail(context, drink.id, drink.category),
onFavoriteTap: () => provider.toggleFavorite(drink),
);
}, childCount: provider.drinks.length),
),
);
}
void _navigateToDetail(
BuildContext context,
String drinkId,
String category,
) {
navigateToRoute(
context,
buildDrinkDetailPath(widget.festivalId, category, drinkId),
);
}
}