forked from chuck-lee/sysui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
suggestion_list.dart
478 lines (434 loc) · 16.9 KB
/
suggestion_list.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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// Copyright 2016 The Fuchsia 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 'dart:math' as math;
import 'package:collection/collection.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'elevations.dart';
import 'size_model.dart';
import 'suggestion.dart';
import 'suggestion_layout.dart';
import 'suggestion_model.dart';
import 'suggestion_widget.dart';
const String _kLogoSmall = 'packages/armadillo/res/logo_googleg_24dpx4.png';
const String _kLogoLarge =
'packages/armadillo/res/googlelogo_color_62x24dp.png';
const String _kMicImage = 'packages/armadillo/res/googlemic_color_24dp.png';
const Duration _kFadeInDuration = const Duration(milliseconds: 500);
/// The gap between suggestions.
const double _kSuggestionGap = 16.0;
const double _kThreeColumnWidthThreshold =
kSuggestionWidth * 3 + _kSuggestionGap * 4;
const double _kTwoColumnWidthThreshold =
kSuggestionWidth * 2 + _kSuggestionGap * 3;
const double _kOneColumnWidthThreshold = kSuggestionWidth + _kSuggestionGap * 2;
const double _kThreeColumnWidth = kSuggestionWidth * 3 + _kSuggestionGap * 2;
const double _kTwoColumnWidth = kSuggestionWidth * 2 + _kSuggestionGap;
const double _kOneColumnWidth = kSuggestionWidth;
const double _kSuggestionListBottomPadding = 32.0;
const ListEquality<Suggestion> _kSuggestionListEquality =
const ListEquality<Suggestion>();
/// Called when a suggestion is selected. [globalBounds] indicates the location
/// of the widget representing [suggestion] was on screen when it was selected.
typedef void OnSuggestionSelected(Suggestion suggestion, Rect globalBounds);
/// Displays a list of suggestions and provides a mechanism for asking for
/// new things to do.
class SuggestionList extends StatefulWidget {
/// The controller to use for scrolling the list.
final ScrollController scrollController;
/// Called when the user begins asking.
final VoidCallback onAskingStarted;
/// Called when the user ends asking.
final VoidCallback onAskingEnded;
/// Called when a suggestion is selected.
final OnSuggestionSelected onSuggestionSelected;
/// The [FocusNode] to be used by the ask text field.
final FocusNode askFocusNode;
/// Constructor.
const SuggestionList({
Key key,
this.scrollController,
this.onAskingStarted,
this.onAskingEnded,
this.onSuggestionSelected,
this.askFocusNode,
})
: super(key: key);
@override
SuggestionListState createState() => new SuggestionListState();
}
/// Manages the asking state for the [SuggestionList].
class SuggestionListState extends State<SuggestionList>
with TickerProviderStateMixin {
final TextEditingController _askTextController = new TextEditingController();
Suggestion _selectedSuggestion;
DateTime _lastBuildTime;
AnimationController _fadeInAnimation;
CurvedAnimation _curvedFadeInAnimation;
@override
void initState() {
super.initState();
_fadeInAnimation = new AnimationController(
vsync: this,
value: 0.0,
duration: _kFadeInDuration,
);
_curvedFadeInAnimation = new CurvedAnimation(
parent: _fadeInAnimation,
curve: Curves.fastOutSlowIn,
);
_askTextController.addListener(() {
if (_askTextController.text.isNotEmpty) {
_startAsking();
}
});
}
@override
void dispose() {
_fadeInAnimation.dispose();
super.dispose();
}
void _startAsking() {
SuggestionModel.of(context).asking = true;
widget.onAskingStarted?.call();
}
/// Clears the ask text.
void _clear() {
_askTextController.clear();
SuggestionModel.of(context).askText = null;
}
void _onTranscriptUpdate(String spokenText) {
_askTextController
..text = spokenText
..selection = new TextSelection.collapsed(offset: spokenText.length);
SuggestionModel.of(context).askText = spokenText;
}
/// Clears the last selected suggestion. The selected suggestion isn't drawn
/// in favor of a splash transition drawing it.
void resetSelection() {
setState(() {
_selectedSuggestion = null;
});
}
/// Stops asking and clears the the ask text.
void stopAsking() {
_clear();
SuggestionModel.of(context).asking = false;
widget.onAskingEnded?.call();
}
/// Selects the first suggestion in the list as if it had been tapped.
void selectFirstSuggestions() {
List<Suggestion> suggestions = SuggestionModel.of(context).asking
? SuggestionModel.of(context).askSuggestions
: SuggestionModel.of(context).nextSuggestions;
if (suggestions.isNotEmpty) {
_onSuggestionSelected(suggestions[0]);
}
}
Widget _buildSuggestionGrid() => new ScopedModelDescendant<SuggestionModel>(
builder: (
BuildContext context,
Widget child,
SuggestionModel suggestionModel,
) {
_lastBuildTime = new DateTime.now();
_fadeInAnimation
..value = 0.0
..forward();
List<Suggestion> suggestions = suggestionModel.asking
? suggestionModel.askSuggestions
: suggestionModel.nextSuggestions;
return new Container(
color: const Color(0xFFDBE2E5),
padding: const EdgeInsets.only(top: 32.0),
child: new CustomScrollView(
controller: widget.scrollController,
slivers: <Widget>[
new SliverGrid(
gridDelegate: new _SuggestionListSliverGridDelegate(
suggestions: suggestions,
textDirection: Directionality.of(context),
),
delegate: new SliverChildBuilderDelegate(
(BuildContext context, int index) => _createSuggestion(
suggestions[index],
),
childCount: suggestions.length,
),
),
],
),
);
},
);
@override
Widget build(BuildContext context) => new Overlay(
initialEntries: <OverlayEntry>[
new OverlayEntry(
builder: (BuildContext context) => new LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) =>
constraints.maxWidth == 0.0
? const Offstage()
: new ScopedModelDescendant<SizeModel>(
builder: (
BuildContext context,
Widget child,
SizeModel sizeModel,
) =>
new PhysicalModel(
elevation: Elevations.suggestionList,
color: Colors.white,
borderRadius: const BorderRadius.only(
topLeft: const Radius.circular(8.0),
topRight: const Radius.circular(8.0),
),
child: new Stack(
children: <Widget>[
// We overlap a little to avoid aliasing issues.
new Positioned.fill(
top: sizeModel.askHeight - 8.0,
child: _buildSuggestionGrid(),
),
new Positioned(
left: 0.0,
right: 0.0,
top: 0.0,
height: sizeModel.askHeight,
child: new Container(
color: Colors.white,
padding: new EdgeInsets.symmetric(
horizontal: _getLeftOffset(
constraints.maxWidth,
),
),
child: _buildAsk(context),
),
),
],
),
),
),
),
),
],
);
Widget _buildAsk(BuildContext context) => new GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: _startAsking,
child: new Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
new ScopedModelDescendant<SuggestionModel>(
builder: (
BuildContext context,
Widget child,
SuggestionModel suggestionModel,
) =>
new Image.asset(
suggestionModel.asking ? _kLogoSmall : _kLogoLarge,
height: 24.0,
fit: BoxFit.fitHeight,
),
),
new Container(width: 16.0),
// Ask Anything text field.
new Expanded(
child: new ScopedModelDescendant<SuggestionModel>(
builder: (
BuildContext context,
Widget child,
SuggestionModel suggestionModel,
) =>
new Offstage(
offstage: !suggestionModel.asking,
child: child,
),
child: new Material(
color: Colors.transparent,
child: new TextField(
decoration: const InputDecoration(hideDivider: true),
style: new TextStyle(
fontSize: 16.0,
color: Colors.grey[600],
),
focusNode: widget.askFocusNode,
controller: _askTextController,
onChanged: (String text) {
SuggestionModel.of(context).askText = text;
},
onSubmitted: (String text) {
// Select the first suggestion on text commit (ie.
// Pressing enter or tapping 'Go').
selectFirstSuggestions();
},
),
),
),
),
new GestureDetector(
onTap: () {
SuggestionModel.of(context).beginSpeechCapture(
onTranscriptUpdate: _onTranscriptUpdate,
);
},
child: new Image.asset(
_kMicImage,
height: 24.0,
fit: BoxFit.fitHeight,
),
),
],
),
);
void _onSuggestionSelected(Suggestion suggestion) {
if (new DateTime.now().difference(_lastBuildTime) < _kFadeInDuration) {
return;
}
switch (suggestion.selectionType) {
case SelectionType.launchStory:
case SelectionType.modifyStory:
case SelectionType.closeSuggestions:
setState(() {
_selectedSuggestion = suggestion;
});
// We pass the bounds of the suggestion w.r.t.
// global coordinates so it can be mapped back to
// local coordinates when it's displayed in the
// SelectedSuggestionOverlay.
RenderBox box = suggestion.globalKey.currentContext.findRenderObject();
widget.onSuggestionSelected(
suggestion,
box.localToGlobal(Offset.zero) & box.size,
);
break;
case SelectionType.doNothing:
default:
break;
}
}
Widget _createSuggestion(Suggestion suggestion) => new RepaintBoundary(
child: new FadeTransition(
opacity: _curvedFadeInAnimation,
child: new SuggestionWidget(
key: suggestion.globalKey,
visible: _selectedSuggestion?.id != suggestion.id,
suggestion: suggestion,
onSelected: () => _onSuggestionSelected(suggestion),
),
),
);
/// Determines the width of a suggestion in the suggestion list.
static double getSuggestionWidth(double maxWidth) =>
math.min(kSuggestionWidth, maxWidth - 2 * _kSuggestionGap);
/// Determines the horizontal margin of suggestions in the suggestion list.
static double getSuggestionHorizontalMargin() => _kSuggestionGap;
}
class _SuggestionListSliverGridDelegate extends SliverGridDelegate {
final List<Suggestion> suggestions;
final TextDirection textDirection;
_SuggestionListSliverGridDelegate({this.suggestions, this.textDirection});
@override
SliverGridLayout getLayout(SliverConstraints constraints) =>
new _SuggestionListSliverGridLayout(
suggestions: suggestions,
width: constraints.crossAxisExtent,
textDirection: textDirection,
);
@override
bool shouldRelayout(_SuggestionListSliverGridDelegate oldDelegate) =>
!_kSuggestionListEquality.equals(suggestions, oldDelegate.suggestions);
}
class _SuggestionListSliverGridLayout extends SliverGridLayout {
final List<Suggestion> suggestions;
final double width;
final TextDirection textDirection;
const _SuggestionListSliverGridLayout({
this.suggestions,
this.width,
this.textDirection,
});
/// The minimum child index that is visible at (or after) this scroll offset.
@override
int getMinChildIndexForScrollOffset(double scrollOffset) =>
suggestions.isEmpty ? -1 : 0;
/// The maximum child index that is visible at (or before) this scroll offset.
@override
int getMaxChildIndexForScrollOffset(double scrollOffset) =>
suggestions.isEmpty ? -1 : suggestions.length - 1;
/// The size and position of the child with the given index.
@override
SliverGridGeometry getGeometryForChildIndex(int index) {
if (index < 0 || index >= suggestions.length) {
return const SliverGridGeometry(
scrollOffset: 0.0,
crossAxisOffset: 0.0,
mainAxisExtent: 0.0,
crossAxisExtent: 0.0,
);
}
int columnCount = _getColumnCount(width);
double leftOffset = _getLeftOffset(width);
double crossAxisExtent = width >= kSuggestionWidth + 2 * _kSuggestionGap
? kSuggestionWidth
: width - 2 * _kSuggestionGap;
double crossAxisOffset = (columnCount == 1)
? leftOffset
: (columnCount == 2 && ((index % 2) == 0))
? leftOffset
: (columnCount == 2 && ((index % 2) == 1))
? leftOffset + _kSuggestionGap + kSuggestionWidth
: ((index % 3) == 0)
? leftOffset
: ((index % 3) == 1)
? leftOffset + _kSuggestionGap + kSuggestionWidth
: leftOffset +
_kSuggestionGap * 2 +
kSuggestionWidth * 2;
suggestions[index].suggestionLayout.layout(width, textDirection);
double mainAxisExtent =
suggestions[index].suggestionLayout.suggestionHeight;
double scrollOffset = 0.0;
for (int i = index - columnCount; i >= 0; i -= columnCount) {
scrollOffset += _kSuggestionGap;
suggestions[i].suggestionLayout.layout(width, textDirection);
scrollOffset += suggestions[i].suggestionLayout.suggestionHeight;
}
return new SliverGridGeometry(
scrollOffset: scrollOffset,
crossAxisOffset: crossAxisOffset,
mainAxisExtent: mainAxisExtent,
crossAxisExtent: math.max(0.0, crossAxisExtent),
);
}
/// An estimate of the scroll extent needed to fully display all the tiles if
/// there are `childCount` children in total.
@override
double estimateMaxScrollOffset(int childCount) {
int columnCount = _getColumnCount(width);
double maxScrollOffset = 0.0;
for (int i = 0; i < math.min(childCount, columnCount); i++) {
SliverGridGeometry geometry =
getGeometryForChildIndex(childCount - 1 - i);
maxScrollOffset = math.max(
maxScrollOffset,
geometry.scrollOffset +
geometry.mainAxisExtent +
_kSuggestionListBottomPadding,
);
}
return maxScrollOffset;
}
}
double _getLeftOffset(double width) => width >= _kThreeColumnWidthThreshold
? (width - _kThreeColumnWidth) / 2.0
: width >= _kTwoColumnWidthThreshold
? (width - _kTwoColumnWidth) / 2.0
: width >= _kOneColumnWidthThreshold
? (width - _kOneColumnWidth) / 2.0
: _kSuggestionGap;
int _getColumnCount(double width) => width >= _kThreeColumnWidthThreshold
? 3
: width >= _kTwoColumnWidthThreshold ? 2 : 1;