RhythmBox/lib/widgets/lyrics/synced_lyrics.dart

250 lines
8.7 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
2024-08-30 04:56:28 +00:00
import 'package:gap/gap.dart';
import 'package:get/get.dart';
import 'package:rhythm_box/providers/audio_player.dart';
import 'package:rhythm_box/services/audio_player/audio_player.dart';
import 'package:rhythm_box/services/lyrics/model.dart';
import 'package:rhythm_box/services/lyrics/provider.dart';
2024-08-29 11:10:54 +00:00
import 'package:rhythm_box/widgets/sized_container.dart';
import 'package:scroll_to_index/scroll_to_index.dart';
class SyncedLyrics extends StatefulWidget {
final int defaultTextZoom;
const SyncedLyrics({
super.key,
required this.defaultTextZoom,
});
@override
State<SyncedLyrics> createState() => _SyncedLyricsState();
}
class _SyncedLyricsState extends State<SyncedLyrics> {
late final AudioPlayerProvider _playback = Get.find();
late final SyncedLyricsProvider _syncedLyrics = Get.find();
final AutoScrollController _autoScrollController = AutoScrollController();
late final int _textZoomLevel = widget.defaultTextZoom;
SubtitleSimple? _lyric;
2024-08-29 08:42:48 +00:00
String? _activeTrackId;
bool get _isLyricSynced =>
_lyric == null ? false : _lyric!.lyrics.any((x) => x.time.inSeconds > 0);
Future<void> _pullLyrics() async {
if (_playback.state.value.activeTrack == null) return;
2024-08-29 08:42:48 +00:00
_activeTrackId = _playback.state.value.activeTrack!.id;
final out = await _syncedLyrics.fetch(_playback.state.value.activeTrack!);
setState(() => _lyric = out);
}
List<StreamSubscription>? _subscriptions;
Color get _unFocusColor =>
2024-08-29 11:10:54 +00:00
Theme.of(context).colorScheme.onSurface.withOpacity(0.5);
2024-08-29 14:39:54 +00:00
void _syncLyricsProgress() {
2024-09-02 13:20:30 +00:00
if (_isLyricSynced) {
for (var idx = 0; idx < _lyric!.lyrics.length; idx++) {
final lyricSlice = _lyric!.lyrics[idx];
final lyricNextSlice =
idx + 1 < _lyric!.lyrics.length ? _lyric!.lyrics[idx + 1] : null;
final isActive = _playback.durationCurrent.value.inSeconds >=
lyricSlice.time.inSeconds &&
(lyricNextSlice == null ||
lyricNextSlice.time.inSeconds >
_playback.durationCurrent.value.inSeconds);
if (isActive) {
_autoScrollController.scrollToIndex(
idx,
preferPosition: AutoScrollPosition.middle,
);
return;
}
2024-08-29 14:39:54 +00:00
}
}
2024-08-29 15:03:41 +00:00
2024-09-02 13:20:30 +00:00
if (_lyric!.lyrics.isNotEmpty || !_isLyricSynced) {
2024-08-29 15:03:41 +00:00
_autoScrollController.scrollToIndex(
0,
preferPosition: AutoScrollPosition.begin,
);
}
2024-08-29 14:39:54 +00:00
}
@override
void initState() {
super.initState();
2024-08-29 14:39:54 +00:00
_pullLyrics().then((_) {
_syncLyricsProgress();
});
_subscriptions = [
2024-08-29 08:42:48 +00:00
_playback.state.listen((value) {
if (value.activeTrack == null) return;
if (value.activeTrack!.id != _activeTrackId) {
2024-08-29 15:03:41 +00:00
_pullLyrics().then((_) {
_syncLyricsProgress();
});
2024-08-29 08:42:48 +00:00
}
}),
];
}
@override
void dispose() {
_autoScrollController.dispose();
if (_subscriptions != null) {
for (final subscription in _subscriptions!) {
subscription.cancel();
}
}
super.dispose();
}
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
return CustomScrollView(
2024-08-29 17:38:02 +00:00
cacheExtent: 10000,
controller: _autoScrollController,
slivers: [
2024-08-30 04:56:28 +00:00
const SliverGap(16),
2024-08-29 11:10:54 +00:00
if (_lyric == null)
const SliverFillRemaining(
child: Center(
child: CircularProgressIndicator(),
),
),
2024-09-02 13:20:30 +00:00
if (_lyric != null && _lyric!.lyrics.isNotEmpty && !_isLyricSynced)
SliverToBoxAdapter(
child: Text(
'Lyrics isn\'t synced',
textAlign: MediaQuery.of(context).size.width >= 720
? TextAlign.center
: TextAlign.left,
).paddingSymmetric(
horizontal: 24,
vertical: 8,
),
),
if (_lyric != null && _lyric!.lyrics.isNotEmpty)
SliverList.builder(
itemCount: _lyric!.lyrics.length,
2024-08-29 08:42:48 +00:00
itemBuilder: (context, idx) => Obx(() {
final lyricSlice = _lyric!.lyrics[idx];
final lyricNextSlice = idx + 1 < _lyric!.lyrics.length
? _lyric!.lyrics[idx + 1]
: null;
2024-08-29 08:42:48 +00:00
final isActive = _playback.durationCurrent.value.inSeconds >=
lyricSlice.time.inSeconds &&
(lyricNextSlice == null ||
lyricNextSlice.time.inSeconds >
2024-09-02 13:20:30 +00:00
_playback.durationCurrent.value.inSeconds) &&
_isLyricSynced;
2024-08-29 08:42:48 +00:00
if (_playback.durationCurrent.value.inSeconds ==
lyricSlice.time.inSeconds &&
_isLyricSynced) {
_autoScrollController.scrollToIndex(
idx,
preferPosition: AutoScrollPosition.middle,
);
}
2024-09-02 13:20:30 +00:00
return AutoScrollTag(
key: ValueKey(idx),
index: idx,
controller: _autoScrollController,
child: lyricSlice.text.isEmpty
? Container(
padding: idx == _lyric!.lyrics.length - 1
? EdgeInsets.only(bottom: size.height / 2)
: null,
)
: Padding(
padding: idx == _lyric!.lyrics.length - 1
2024-08-29 11:10:54 +00:00
? const EdgeInsets.symmetric(vertical: 8)
.copyWith(bottom: 80)
: const EdgeInsets.symmetric(vertical: 8),
child: AnimatedDefaultTextStyle(
duration: const Duration(milliseconds: 250),
style: TextStyle(
fontWeight:
isActive ? FontWeight.w500 : FontWeight.normal,
fontSize:
(isActive ? 28 : 26) * (_textZoomLevel / 100),
),
textAlign: TextAlign.center,
child: InkWell(
2024-08-29 17:38:02 +00:00
borderRadius: const BorderRadius.all(
Radius.circular(8),
),
onTap: () async {
final time = Duration(
seconds: lyricSlice.time.inSeconds -
_syncedLyrics.delay.value,
);
if (time > audioPlayer.duration ||
time.isNegative) {
return;
}
audioPlayer.seek(time);
},
child: Builder(builder: (context) {
2024-08-29 11:10:54 +00:00
return AnimatedDefaultTextStyle(
style: TextStyle(
2024-08-29 11:10:54 +00:00
fontSize: isActive ? 20 : 16,
color: isActive
? Theme.of(context).colorScheme.onSurface
: _unFocusColor,
),
2024-08-29 11:10:54 +00:00
duration: 500.ms,
2024-08-29 17:38:02 +00:00
curve: Curves.decelerate,
2024-08-29 11:10:54 +00:00
child: Text(
lyricSlice.text,
textAlign:
MediaQuery.of(context).size.width >= 720
? TextAlign.center
: TextAlign.left,
),
);
}).paddingSymmetric(horizontal: 24),
),
),
),
);
2024-08-29 08:42:48 +00:00
}),
2024-08-29 11:10:54 +00:00
)
else if (_lyric != null && _lyric!.lyrics.isEmpty)
SliverFillRemaining(
child: CenteredContainer(
maxWidth: 280,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Lyrics Not Found',
2024-09-02 13:20:30 +00:00
textAlign: TextAlign.center,
2024-08-29 11:10:54 +00:00
style: Theme.of(context).textTheme.bodyLarge,
),
const Text(
"This song haven't lyrics that recorded in our database.",
textAlign: TextAlign.center,
),
],
),
),
),
2024-08-30 04:56:28 +00:00
const SliverGap(16),
],
);
}
}