index_bar.dart
15.7 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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
import 'dart:math' as math;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
/// IndexHintBuilder.
typedef IndexHintBuilder = Widget Function(BuildContext context, String tag);
/// IndexBarDragListener.
abstract class IndexBarDragListener {
/// Creates an [IndexBarDragListener] that can be used by a
/// [IndexBar] to return the drag listener.
factory IndexBarDragListener.create() => IndexBarDragNotifier();
/// drag details.
ValueListenable<IndexBarDragDetails> get dragDetails;
}
/// Internal implementation of [ItemPositionsListener].
class IndexBarDragNotifier implements IndexBarDragListener {
@override
final ValueNotifier<IndexBarDragDetails> dragDetails = ValueNotifier(IndexBarDragDetails());
}
/// IndexModel.
class IndexBarDragDetails {
static const int actionDown = 0;
static const int actionUp = 1;
static const int actionUpdate = 2;
static const int actionEnd = 3;
static const int actionCancel = 4;
int? action;
int? index; //current touch index.
String? tag; //current touch tag.
double? localPositionY;
double? globalPositionY;
IndexBarDragDetails({
this.action,
this.index,
this.tag,
this.localPositionY,
this.globalPositionY,
});
}
///Default Index data.
const List<String> kIndexBarData = [
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
'#'
];
const double kIndexBarWidth = 30;
const double kIndexBarItemHeight = 16;
/// IndexBar options.
class IndexBarOptions {
/// Creates IndexBar options.
/// Examples.
/// needReBuild = true
/// ignoreDragCancel = true
/// color = Colors.transparent
/// downColor = Color(0xFFEEEEEE)
/// decoration
/// downDecoration
/// textStyle = TextStyle(fontSize: 12, color: Color(0xFF666666))
/// downTextStyle = TextStyle(fontSize: 12, color: Colors.white)
/// selectTextStyle = TextStyle(fontSize: 12, color: Colors.white)
/// downItemDecoration = BoxDecoration(shape: BoxShape.circle, color: Colors.blueAccent)
/// selectItemDecoration = BoxDecoration(shape: BoxShape.circle, color: Colors.blueAccent)
/// indexHintWidth = 72
/// indexHintHeight = 72
/// indexHintDecoration = BoxDecoration(color: Colors.black87, shape: BoxShape.rectangle, borderRadius: BorderRadius.all(Radius.circular(6)),)
/// indexHintTextStyle = TextStyle(fontSize: 24.0, color: Colors.white)
/// indexHintChildAlignment = Alignment.center
/// indexHintAlignment = Alignment.center
/// indexHintPosition
/// indexHintOffset
/// localImages
const IndexBarOptions({
this.needRebuild = false,
this.ignoreDragCancel = false,
this.hapticFeedback = false,
this.color,
this.downColor,
this.decoration,
this.downDecoration,
this.textStyle = const TextStyle(fontSize: 12, color: Color(0xFF666666)),
this.downTextStyle,
this.selectTextStyle,
this.downItemDecoration,
this.selectItemDecoration,
this.indexHintWidth = 72,
this.indexHintHeight = 72,
this.indexHintDecoration = const BoxDecoration(
color: Colors.black87,
shape: BoxShape.rectangle,
borderRadius: BorderRadius.all(Radius.circular(6)),
),
this.indexHintTextStyle = const TextStyle(fontSize: 24.0, color: Colors.white),
this.indexHintChildAlignment = Alignment.center,
this.indexHintAlignment = Alignment.center,
this.indexHintPosition,
this.indexHintOffset = Offset.zero,
this.localImages = const [],
});
/// need to rebuild.
final bool needRebuild;
/// Ignore DragCancel.
final bool ignoreDragCancel;
/// Haptic feedback.
final bool hapticFeedback;
/// IndexBar background color.
final Color? color;
/// IndexBar down background color.
final Color? downColor;
/// IndexBar decoration.
final Decoration? decoration;
/// IndexBar down decoration.
final Decoration? downDecoration;
/// IndexBar textStyle.
final TextStyle textStyle;
/// IndexBar down textStyle.
final TextStyle? downTextStyle;
/// IndexBar select textStyle.
final TextStyle? selectTextStyle;
/// IndexBar down item decoration.
final Decoration? downItemDecoration;
/// IndexBar select item decoration.
final Decoration? selectItemDecoration;
/// Index hint width.
final double indexHintWidth;
/// Index hint height.
final double indexHintHeight;
/// Index hint decoration.
final Decoration indexHintDecoration;
/// Index hint alignment.
final Alignment indexHintAlignment;
/// Index hint child alignment.
final Alignment indexHintChildAlignment;
/// Index hint textStyle.
final TextStyle indexHintTextStyle;
/// Index hint position.
final Offset? indexHintPosition;
/// Index hint offset.
final Offset indexHintOffset;
/// local images.
final List<String> localImages;
}
/// IndexBarController.
class IndexBarController {
_IndexBarState? _indexBarState;
bool get isAttached => _indexBarState != null;
void updateTagIndex(String tag) {
_indexBarState?._updateTagIndex(tag);
}
void _attach(_IndexBarState state) {
_indexBarState = state;
}
void _detach() {
_indexBarState = null;
}
}
/// IndexBar.
class IndexBar extends StatefulWidget {
const IndexBar({
super.key,
this.data = kIndexBarData,
this.width = kIndexBarWidth,
this.height,
this.itemHeight = kIndexBarItemHeight,
this.margin,
this.indexHintBuilder,
IndexBarDragListener? indexBarDragListener,
this.options = const IndexBarOptions(),
this.controller,
}) : indexBarDragNotifier = indexBarDragListener as IndexBarDragNotifier?;
/// Index data.
final List<String> data;
/// IndexBar width(def:30).
final double width;
/// IndexBar height.
final double? height;
/// IndexBar item height(def:16).
final double itemHeight;
/// Empty space to surround the [decoration] and [child].
final EdgeInsetsGeometry? margin;
/// IndexHint Builder
final IndexHintBuilder? indexHintBuilder;
/// IndexBar drag listener.
final IndexBarDragNotifier? indexBarDragNotifier;
/// IndexBar options.
final IndexBarOptions options;
/// IndexBarController. If non-null, this can be used to control the state of the IndexBar.
final IndexBarController? controller;
@override
createState() => _IndexBarState();
}
class _IndexBarState extends State<IndexBar> {
/// overlay entry.
static OverlayEntry? overlayEntry;
double floatTop = 0;
String indexTag = '';
int selectIndex = 0;
int action = IndexBarDragDetails.actionEnd;
@override
void initState() {
super.initState();
widget.indexBarDragNotifier?.dragDetails.addListener(_valueChanged);
widget.controller?._attach(this);
}
void _valueChanged() {
if (widget.indexBarDragNotifier == null) return;
IndexBarDragDetails details = widget.indexBarDragNotifier!.dragDetails.value;
selectIndex = details.index!;
indexTag = details.tag!;
action = details.action!;
floatTop = details.globalPositionY! + widget.itemHeight / 2 - widget.options.indexHintHeight / 2;
if (_isActionDown()) {
_addOverlay(context);
} else {
_removeOverlay();
}
if (widget.options.needRebuild) {
if (widget.options.ignoreDragCancel && action == IndexBarDragDetails.actionCancel) {
} else {
setState(() {});
}
}
}
bool _isActionDown() {
return action == IndexBarDragDetails.actionDown || action == IndexBarDragDetails.actionUpdate;
}
@override
void dispose() {
widget.controller?._detach();
_removeOverlay();
widget.indexBarDragNotifier?.dragDetails.removeListener(_valueChanged);
super.dispose();
}
Widget _buildIndexHint(BuildContext context, String tag) {
if (widget.indexHintBuilder != null) {
return widget.indexHintBuilder!(context, tag);
}
Widget child;
TextStyle textStyle = widget.options.indexHintTextStyle;
List<String> localImages = widget.options.localImages;
if (localImages.contains(tag)) {
child = Image.asset(
tag,
width: textStyle.fontSize,
height: textStyle.fontSize,
color: textStyle.color,
);
} else {
child = Text(tag, style: textStyle);
}
return Container(
width: widget.options.indexHintWidth,
height: widget.options.indexHintHeight,
alignment: widget.options.indexHintChildAlignment,
decoration: widget.options.indexHintDecoration,
child: child,
);
}
/// add overlay.
void _addOverlay(BuildContext context) {
OverlayState overlayState = Overlay.of(context);
if (overlayEntry == null) {
overlayEntry = OverlayEntry(builder: (BuildContext ctx) {
double left;
double top;
if (widget.options.indexHintPosition != null) {
left = widget.options.indexHintPosition!.dx;
top = widget.options.indexHintPosition!.dy;
} else {
if (widget.options.indexHintAlignment == Alignment.centerRight) {
left = MediaQuery.of(context).size.width -
kIndexBarWidth -
widget.options.indexHintWidth +
widget.options.indexHintOffset.dx;
top = floatTop + widget.options.indexHintOffset.dy;
} else if (widget.options.indexHintAlignment == Alignment.centerLeft) {
left = kIndexBarWidth + widget.options.indexHintOffset.dx;
top = floatTop + widget.options.indexHintOffset.dy;
} else {
left = MediaQuery.of(context).size.width / 2 -
widget.options.indexHintWidth / 2 +
widget.options.indexHintOffset.dx;
top = MediaQuery.of(context).size.height / 2 -
widget.options.indexHintHeight / 2 +
widget.options.indexHintOffset.dy;
}
}
return Positioned(
left: left,
top: top,
child: Material(
color: Colors.transparent,
child: _buildIndexHint(ctx, indexTag),
));
});
overlayState.insert(overlayEntry!);
} else {
//重新绘制UI,类似setState
overlayEntry?.markNeedsBuild();
}
}
/// remove overlay.
void _removeOverlay() {
overlayEntry?.remove();
overlayEntry = null;
}
Widget _buildItem(BuildContext context, int index) {
String tag = widget.data[index];
Decoration? decoration;
TextStyle? textStyle;
if (widget.options.downItemDecoration != null) {
decoration = (_isActionDown() && selectIndex == index) ? widget.options.downItemDecoration : null;
textStyle = (_isActionDown() && selectIndex == index) ? widget.options.downTextStyle : widget.options.textStyle;
} else if (widget.options.selectItemDecoration != null) {
decoration = (selectIndex == index) ? widget.options.selectItemDecoration : null;
textStyle = (selectIndex == index) ? widget.options.selectTextStyle : widget.options.textStyle;
} else {
textStyle =
_isActionDown() ? (widget.options.downTextStyle ?? widget.options.textStyle) : widget.options.textStyle;
}
Widget child;
List<String> localImages = widget.options.localImages;
if (localImages.contains(tag)) {
child = Image.asset(
tag,
width: textStyle?.fontSize,
height: textStyle?.fontSize,
color: textStyle?.color,
);
} else {
child = Text(tag, style: textStyle);
}
return Container(
alignment: Alignment.center,
decoration: decoration,
child: child,
);
}
void _updateTagIndex(String tag) {
if (_isActionDown()) return;
selectIndex = widget.data.indexOf(tag);
setState(() {});
}
@override
Widget build(BuildContext context) {
return Container(
color: _isActionDown() ? widget.options.downColor : widget.options.color,
decoration: _isActionDown() ? widget.options.downDecoration : widget.options.decoration,
width: widget.width,
height: widget.height,
margin: widget.margin,
alignment: Alignment.center,
child: BaseIndexBar(
data: widget.data,
width: widget.width,
itemHeight: widget.itemHeight,
hapticFeedback: widget.options.hapticFeedback,
itemBuilder: (BuildContext context, int index) {
return _buildItem(context, index);
},
indexBarDragNotifier: widget.indexBarDragNotifier,
),
);
}
}
class BaseIndexBar extends StatefulWidget {
const BaseIndexBar({
super.key,
this.data = kIndexBarData,
this.width = kIndexBarWidth,
this.itemHeight = kIndexBarItemHeight,
this.hapticFeedback = false,
this.textStyle = const TextStyle(fontSize: 12.0, color: Color(0xFF666666)),
this.itemBuilder,
this.indexBarDragNotifier,
});
/// index data.
final List<String> data;
/// IndexBar width(def:30).
final double width;
/// IndexBar item height(def:16).
final double itemHeight;
/// Haptic feedback.
final bool hapticFeedback;
/// IndexBar text style.
final TextStyle textStyle;
final IndexedWidgetBuilder? itemBuilder;
final IndexBarDragNotifier? indexBarDragNotifier;
@override
createState() => _BaseIndexBarState();
}
class _BaseIndexBarState extends State<BaseIndexBar> {
int lastIndex = -1;
int _widgetTop = 0;
/// get index.
int _getIndex(double offset) {
int index = offset ~/ widget.itemHeight;
return math.min(index, widget.data.length - 1);
}
/// trigger drag event.
_triggerDragEvent(int action) {
if (widget.hapticFeedback &&
(action == IndexBarDragDetails.actionDown || action == IndexBarDragDetails.actionUpdate)) {
HapticFeedback.vibrate();
}
widget.indexBarDragNotifier?.dragDetails.value = IndexBarDragDetails(
action: action,
index: lastIndex,
tag: widget.data[lastIndex],
localPositionY: lastIndex * widget.itemHeight,
globalPositionY: lastIndex * widget.itemHeight + _widgetTop,
);
}
RenderBox? _getRenderBox(BuildContext context) {
RenderObject? renderObject = context.findRenderObject();
RenderBox? box;
if (renderObject != null) {
box = renderObject as RenderBox;
}
return box;
}
@override
Widget build(BuildContext context) {
List<Widget> children = List.generate(widget.data.length, (index) {
Widget child = widget.itemBuilder == null
? Center(child: Text(widget.data[index], style: widget.textStyle))
: widget.itemBuilder!(context, index);
return SizedBox(
width: widget.width,
height: widget.itemHeight,
child: child,
);
});
return GestureDetector(
onVerticalDragDown: (DragDownDetails details) {
RenderBox? box = _getRenderBox(context);
if (box == null) return;
Offset topLeftPosition = box.localToGlobal(Offset.zero);
_widgetTop = topLeftPosition.dy.toInt();
int index = _getIndex(details.localPosition.dy);
if (index >= 0) {
lastIndex = index;
_triggerDragEvent(IndexBarDragDetails.actionDown);
}
},
onVerticalDragUpdate: (DragUpdateDetails details) {
int index = _getIndex(details.localPosition.dy);
if (index >= 0 && lastIndex != index) {
lastIndex = index;
//HapticFeedback.lightImpact();
//HapticFeedback.vibrate();
_triggerDragEvent(IndexBarDragDetails.actionUpdate);
}
},
onVerticalDragEnd: (DragEndDetails details) {
_triggerDragEvent(IndexBarDragDetails.actionEnd);
},
onVerticalDragCancel: () {
_triggerDragEvent(IndexBarDragDetails.actionCancel);
},
onTapUp: (TapUpDetails details) {
//_triggerDragEvent(IndexBarDragDetails.actionUp);
},
behavior: HitTestBehavior.translucent,
child: Column(
mainAxisSize: MainAxisSize.min,
children: children,
),
);
}
}