From cd611aedc4a0733f58129052bd4462c9b16a1c31 Mon Sep 17 00:00:00 2001 From: krille-chan Date: Tue, 3 Dec 2024 19:34:28 +0100 Subject: [PATCH] chore: Display normal Slider when no waveform provided in audioplayer --- lib/pages/chat/events/audio_player.dart | 64 ++++++++++++++----------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/lib/pages/chat/events/audio_player.dart b/lib/pages/chat/events/audio_player.dart index 8b7f8b5252..1fb65dc24e 100644 --- a/lib/pages/chat/events/audio_player.dart +++ b/lib/pages/chat/events/audio_player.dart @@ -179,12 +179,12 @@ class AudioPlayerState extends State { return '${duration.inMinutes.toString().padLeft(2, '0')}:${(duration.inSeconds % 60).toString().padLeft(2, '0')}'; } - List _getWaveform() { + List? _getWaveform() { final eventWaveForm = widget.event.content .tryGetMap('org.matrix.msc1767.audio') ?.tryGetList('waveform'); if (eventWaveForm == null || eventWaveForm.isEmpty) { - return List.filled(AudioPlayerWidget.wavesCount, 500); + return null; } while (eventWaveForm.length < AudioPlayerWidget.wavesCount) { for (var i = 0; i < eventWaveForm.length; i = i + 2) { @@ -200,7 +200,7 @@ class AudioPlayerState extends State { return eventWaveForm.map((i) => i > 1024 ? 1024 : i).toList(); } - late final List waveform; + late final List? _waveform; void _toggleSpeed() async { final audioPlayer = this.audioPlayer; @@ -229,12 +229,13 @@ class AudioPlayerState extends State { @override void initState() { super.initState(); - waveform = _getWaveform(); + _waveform = _getWaveform(); } @override Widget build(BuildContext context) { final theme = Theme.of(context); + final waveform = _waveform; final statusText = this.statusText ??= _durationString ?? '00:00'; final audioPlayer = this.audioPlayer; @@ -290,34 +291,35 @@ class AudioPlayerState extends State { Expanded( child: Stack( children: [ - Padding( - padding: const EdgeInsets.symmetric(horizontal: 16.0), - child: Row( - children: [ - for (var i = 0; - i < AudioPlayerWidget.wavesCount; - i++) - Expanded( - child: Container( - height: 32, - alignment: Alignment.center, + if (waveform != null) + Padding( + padding: const EdgeInsets.symmetric(horizontal: 16.0), + child: Row( + children: [ + for (var i = 0; + i < AudioPlayerWidget.wavesCount; + i++) + Expanded( child: Container( - margin: const EdgeInsets.symmetric( - horizontal: 1, + height: 32, + alignment: Alignment.center, + child: Container( + margin: const EdgeInsets.symmetric( + horizontal: 1, + ), + decoration: BoxDecoration( + color: i < wavePosition + ? widget.color + : widget.color.withAlpha(128), + borderRadius: BorderRadius.circular(64), + ), + height: 32 * (waveform[i] / 1024), ), - decoration: BoxDecoration( - color: i < wavePosition - ? widget.color - : widget.color.withAlpha(128), - borderRadius: BorderRadius.circular(64), - ), - height: 32 * (waveform[i] / 1024), ), ), - ), - ], + ], + ), ), - ), SizedBox( height: 32, child: Slider( @@ -325,8 +327,12 @@ class AudioPlayerState extends State { widget.event.room.client.userID ? theme.colorScheme.onPrimary : theme.colorScheme.primary, - activeColor: Colors.transparent, - inactiveColor: Colors.transparent, + activeColor: waveform == null + ? widget.color + : Colors.transparent, + inactiveColor: waveform == null + ? widget.color.withAlpha(128) + : Colors.transparent, max: maxPosition, value: currentPosition, onChanged: (position) => audioPlayer == null