-
Notifications
You must be signed in to change notification settings - Fork 385
Expand file tree
/
Copy pathaudio_waveform.dart
More file actions
487 lines (417 loc) · 14.4 KB
/
Copy pathaudio_waveform.dart
File metadata and controls
487 lines (417 loc) · 14.4 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
import 'dart:math' as math;
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:stream_chat_flutter/src/theme/audio_waveform_slider_theme.dart';
import 'package:stream_chat_flutter/src/theme/audio_waveform_theme.dart';
const _kAudioWaveformSliderThumbWidth = 4.0;
const _kAudioWaveformSliderThumbHeight = 28.0;
/// {@template streamAudioWaveformSlider}
/// A widget that displays an audio waveform and allows the user to interact
/// with it using a slider.
/// {@endtemplate}
class StreamAudioWaveformSlider extends StatefulWidget {
/// {@macro streamAudioWaveformSlider}
const StreamAudioWaveformSlider({
super.key,
required this.waveform,
this.onChangeStart,
required this.onChanged,
this.onChangeEnd,
this.limit = 100,
this.color,
this.progress = 0,
this.progressColor,
this.minBarHeight,
this.spacingRatio,
this.heightScale,
this.inverse = true,
this.thumbColor,
this.thumbBorderColor,
});
/// The waveform data to be drawn.
///
/// Note: The values should be between 0 and 1.
final List<double> waveform;
/// Called when the thumb starts being dragged.
final ValueChanged<double>? onChangeStart;
/// Called while the thumb is being dragged.
final ValueChanged<double>? onChanged;
/// Called when the thumb stops being dragged.
final ValueChanged<double>? onChangeEnd;
/// The color of the wave bars.
///
/// Defaults to [StreamAudioWaveformSliderThemeData.color].
final Color? color;
/// The number of wave bars that will be draw in the screen. When the length
/// of [waveform] is bigger than [limit] only the X last bars will be shown.
///
/// Defaults to 100.
final int limit;
/// The progress of the audio track. Used to show the progress of the audio.
///
/// Defaults to 0.
final double progress;
/// The color of the progressed wave bars.
///
/// Defaults to [StreamAudioWaveformSliderThemeData.progressColor].
final Color? progressColor;
/// The minimum height of the bars.
///
/// Defaults to [StreamAudioWaveformSliderThemeData.minBarHeight].
final double? minBarHeight;
/// The ratio of the spacing between the bars.
///
/// Defaults to [StreamAudioWaveformSliderThemeData.spacingRatio].
final double? spacingRatio;
/// The scale of the height of the bars.
///
/// Defaults to [StreamAudioWaveformSliderThemeData.heightScale].
final double? heightScale;
/// If true, the bars grow from right to left otherwise they grow from left
/// to right.
///
/// Defaults to true.
final bool inverse;
/// The color of the slider thumb.
///
/// Defaults to [StreamAudioWaveformSliderThemeData.thumbColor].
final Color? thumbColor;
/// The color of the slider thumb border.
///
/// Defaults to [StreamAudioWaveformSliderThemeData.thumbBorderColor].
final Color? thumbBorderColor;
@override
State<StreamAudioWaveformSlider> createState() =>
_StreamAudioWaveformSliderState();
}
class _StreamAudioWaveformSliderState extends State<StreamAudioWaveformSlider> {
@override
Widget build(BuildContext context) {
final theme = StreamAudioWaveformSliderTheme.of(context);
final waveformTheme = theme.audioWaveformTheme;
final color = widget.color ?? waveformTheme!.color!;
final progressColor = widget.progressColor ?? waveformTheme!.progressColor!;
final minBarHeight = widget.minBarHeight ?? waveformTheme!.minBarHeight!;
final spacingRatio = widget.spacingRatio ?? waveformTheme!.spacingRatio!;
final heightScale = widget.heightScale ?? waveformTheme!.heightScale!;
final thumbColor = widget.thumbColor ?? theme.thumbColor!;
final thumbBorderColor = widget.thumbBorderColor ?? theme.thumbBorderColor!;
return HorizontalSlider(
onChangeStart: widget.onChangeStart,
onChanged: widget.onChanged,
onChangeEnd: widget.onChangeEnd,
child: LayoutBuilder(
builder: (context, constraints) => Stack(
fit: StackFit.expand,
clipBehavior: Clip.none,
alignment: Alignment.center,
children: [
StreamAudioWaveform(
waveform: widget.waveform,
limit: widget.limit,
color: color,
progress: widget.progress,
progressColor: progressColor,
minBarHeight: minBarHeight,
spacingRatio: spacingRatio,
heightScale: heightScale,
inverse: widget.inverse,
),
Builder(
// Just using it for the calculation of the thumb position.
builder: (context) {
final progressWidth = constraints.maxWidth * widget.progress;
return AnimatedPositioned(
curve: const ElasticOutCurve(1.05),
duration: const Duration(milliseconds: 300),
left: progressWidth - _kAudioWaveformSliderThumbWidth / 2,
child: StreamAudioWaveformSliderThumb(
color: thumbColor,
borderColor: thumbBorderColor,
height: constraints.maxHeight,
),
);
},
),
],
),
),
);
}
}
/// {@template streamAudioWaveformSliderThumb}
/// A widget that represents the thumb of the `StreamAudioWaveformSlider`.
/// {@endtemplate}
class StreamAudioWaveformSliderThumb extends StatelessWidget {
/// {@macro streamAudioWaveformSliderThumb}
const StreamAudioWaveformSliderThumb({
super.key,
this.width = _kAudioWaveformSliderThumbWidth,
this.height = _kAudioWaveformSliderThumbHeight,
this.color = Colors.white,
this.borderColor = const Color(0xffecebeb),
});
/// The width of the thumb.
final double width;
/// The height of the thumb.
final double height;
/// The color of the thumb.
final Color color;
/// The border color of the thumb.
final Color borderColor;
@override
Widget build(BuildContext context) {
return Container(
width: width,
height: height,
decoration: BoxDecoration(
color: color,
border: Border.all(
color: borderColor,
strokeAlign: BorderSide.strokeAlignOutside,
),
borderRadius: BorderRadius.circular(2),
),
);
}
}
/// {@template streamAudioWaveform}
/// A widget that displays an audio waveform.
///
/// The waveform is drawn using the [waveform] data. The waveform is drawn
/// horizontally and the bars grow from right to left.
/// {@endtemplate}
class StreamAudioWaveform extends StatelessWidget {
/// {@macro streamAudioWaveform}
const StreamAudioWaveform({
super.key,
required this.waveform,
this.limit = 100,
this.color,
this.progress = 0,
this.progressColor,
this.minBarHeight,
this.spacingRatio,
this.heightScale,
this.inverse = true,
});
/// The waveform data to be drawn.
///
/// Note: The values should be between 0 and 1.
final List<double> waveform;
/// The color of the wave bars.
///
/// Defaults to [StreamAudioWaveformThemeData.color].
final Color? color;
/// The number of wave bars that will be draw in the screen. When the length
/// of [waveform] is bigger than [limit] only the X last bars will be shown.
///
/// Defaults to 100.
final int limit;
/// The progress of the audio track. Used to show the progress of the audio.
///
/// Defaults to 0.
final double progress;
/// The color of the progressed wave bars.
///
/// Defaults to [StreamAudioWaveformThemeData.progressColor].
final Color? progressColor;
/// The minimum height of the bars.
///
/// Defaults to [StreamAudioWaveformThemeData.minBarHeight].
final double? minBarHeight;
/// The ratio of the spacing between the bars.
///
/// Defaults to [StreamAudioWaveformThemeData.spacingRatio].
final double? spacingRatio;
/// The scale of the height of the bars.
///
/// Defaults to [StreamAudioWaveformThemeData.heightScale].
final double? heightScale;
/// If true, the bars grow from right to left otherwise they grow from left
/// to right.
///
/// Defaults to true.
final bool inverse;
@override
Widget build(BuildContext context) {
final theme = StreamAudioWaveformTheme.of(context);
final color = this.color ?? theme.color!;
final progressColor = this.progressColor ?? theme.progressColor!;
final minBarHeight = this.minBarHeight ?? theme.minBarHeight!;
final spacingRatio = this.spacingRatio ?? theme.spacingRatio!;
final heightScale = this.heightScale ?? theme.heightScale!;
return CustomPaint(
willChange: true,
painter: _WaveformPainter(
waveform: waveform.reversed,
limit: limit,
color: color,
progress: progress,
progressColor: progressColor,
minBarHeight: minBarHeight,
spacingRatio: spacingRatio,
heightScale: heightScale,
inverse: inverse,
),
);
}
}
class _WaveformPainter extends CustomPainter {
_WaveformPainter({
required Iterable<double> waveform,
this.limit = 100,
this.color = const Color(0xff7E828B),
this.progress = 0,
this.progressColor = const Color(0xff005FFF),
this.minBarHeight = 2,
double spacingRatio = 0.3,
this.heightScale = 1,
this.inverse = true,
}) : waveform = [
...waveform.take(limit),
if (waveform.length < limit)
// Fill the remaining bars with 0 value
...List.filled(limit - waveform.length, 0)
],
spacingRatio = spacingRatio.clamp(0, 1);
final List<double> waveform;
final Color color;
final int limit;
final double progress;
final Color progressColor;
final double minBarHeight;
final double spacingRatio;
final bool inverse;
final double heightScale;
@override
void paint(Canvas canvas, Size size) {
final canvasWidth = size.width;
final canvasHeight = size.height;
// The total spacing between the bars in the canvas.
final spacingWidth = canvasWidth * spacingRatio;
final barsWidth = canvasWidth - spacingWidth;
final barWidth = barsWidth / limit;
final barSpacing = spacingWidth / (limit - 1);
final progressWidth = progress * canvasWidth;
void _paintBar(int index, double barValue) {
var dx = index * (barWidth + barSpacing) + barWidth / 2;
if (inverse) dx = canvasWidth - dx;
final dy = canvasHeight / 2;
final barHeight = math.max(barValue * canvasHeight, minBarHeight);
final rect = RRect.fromRectAndRadius(
Rect.fromCenter(
center: Offset(dx, dy),
width: barWidth,
height: barHeight,
),
const Radius.circular(2),
);
final waveColor = switch (dx <= progressWidth) {
true => progressColor,
false => color,
};
final wavePaint = Paint()
..color = waveColor
..strokeCap = StrokeCap.round;
canvas.drawRRect(rect, wavePaint);
}
// Paint all the bars
waveform.forEachIndexed(_paintBar);
}
@override
bool shouldRepaint(covariant _WaveformPainter oldDelegate) =>
!const ListEquality().equals(waveform, oldDelegate.waveform) ||
color != oldDelegate.color ||
limit != oldDelegate.limit ||
progress != oldDelegate.progress ||
progressColor != oldDelegate.progressColor ||
minBarHeight != oldDelegate.minBarHeight ||
spacingRatio != oldDelegate.spacingRatio ||
heightScale != oldDelegate.heightScale ||
inverse != oldDelegate.inverse;
}
/// {@template horizontalSlider}
/// A widget that allows interactive horizontal sliding gestures.
///
/// The `HorizontalSlider` widget wraps a child widget and allows users to
/// perform sliding gestures horizontally. It can be configured with callbacks
/// to notify the parent widget about the changes in the horizontal value.
/// {@endtemplate}
class HorizontalSlider extends StatefulWidget {
/// Creates a horizontal slider.
const HorizontalSlider({
super.key,
required this.child,
required this.onChanged,
this.onChangeStart,
this.onChangeEnd,
});
/// The child widget wrapped by the slider.
final Widget child;
/// Called when the horizontal value starts changing.
final ValueChanged<double>? onChangeStart;
/// Called when the horizontal value changes.
final ValueChanged<double>? onChanged;
/// Called when the horizontal value stops changing.
final ValueChanged<double>? onChangeEnd;
@override
State<HorizontalSlider> createState() => _HorizontalSliderState();
}
class _HorizontalSliderState extends State<HorizontalSlider> {
bool _active = false;
/// Returns true if the slider is interactive.
bool get isInteractive => widget.onChanged != null;
/// Converts the visual position to a value based on the text direction.
double _getValueFromVisualPosition(double visualPosition) {
final textDirection = Directionality.of(context);
final value = switch (textDirection) {
TextDirection.rtl => 1.0 - visualPosition,
TextDirection.ltr => visualPosition,
};
return clampDouble(value, 0, 1);
}
/// Converts the local position to a horizontal value.
double _getValueFromLocalPosition(Offset globalPosition) {
final box = context.findRenderObject()! as RenderBox;
final localPosition = box.globalToLocal(globalPosition);
final visualPosition = localPosition.dx / box.size.width;
return _getValueFromVisualPosition(visualPosition);
}
void _handleDragStart(DragStartDetails details) {
if (!_active && isInteractive) {
_active = true;
final value = _getValueFromLocalPosition(details.globalPosition);
widget.onChangeStart?.call(value);
}
}
void _handleDragUpdate(DragUpdateDetails details) {
_handleHorizontalDrag(details.globalPosition);
}
void _handleDragEnd(DragEndDetails details) {
if (!mounted) return;
if (_active && mounted) {
final value = _getValueFromLocalPosition(details.globalPosition);
widget.onChangeEnd?.call(value);
_active = false;
}
}
/// Handles the sliding gesture.
void _handleHorizontalDrag(Offset globalPosition) {
if (!mounted) return;
if (isInteractive) {
final value = _getValueFromLocalPosition(globalPosition);
widget.onChanged?.call(value);
}
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onHorizontalDragStart: _handleDragStart,
onHorizontalDragUpdate: _handleDragUpdate,
onHorizontalDragEnd: _handleDragEnd,
child: widget.child,
);
}
}