2024-08-28 10:41:32 +00:00
|
|
|
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';
|
2024-08-28 10:41:32 +00:00
|
|
|
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';
|
2024-08-28 10:41:32 +00:00
|
|
|
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;
|
2024-08-28 10:41:32 +00:00
|
|
|
|
|
|
|
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;
|
2024-08-28 10:41:32 +00:00
|
|
|
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-28 10:41:32 +00:00
|
|
|
|
2024-08-29 14:39:54 +00:00
|
|
|
void _syncLyricsProgress() {
|
|
|
|
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,
|
|
|
|
);
|
2024-08-29 15:03:41 +00:00
|
|
|
return;
|
2024-08-29 14:39:54 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-29 15:03:41 +00:00
|
|
|
|
|
|
|
if (_lyric!.lyrics.isNotEmpty) {
|
|
|
|
_autoScrollController.scrollToIndex(
|
|
|
|
0,
|
|
|
|
preferPosition: AutoScrollPosition.begin,
|
|
|
|
);
|
|
|
|
}
|
2024-08-29 14:39:54 +00:00
|
|
|
}
|
|
|
|
|
2024-08-28 10:41:32 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-08-29 14:39:54 +00:00
|
|
|
_pullLyrics().then((_) {
|
|
|
|
_syncLyricsProgress();
|
|
|
|
});
|
2024-08-28 10:41:32 +00:00
|
|
|
_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
|
|
|
}
|
|
|
|
}),
|
2024-08-28 10:41:32 +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,
|
2024-08-28 10:41:32 +00:00
|
|
|
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-08-28 10:41:32 +00:00
|
|
|
if (_lyric != null && _lyric!.lyrics.isNotEmpty)
|
|
|
|
SliverList.builder(
|
|
|
|
itemCount: _lyric!.lyrics.length,
|
2024-08-29 08:42:48 +00:00
|
|
|
itemBuilder: (context, idx) => Obx(() {
|
2024-08-28 10:41:32 +00:00
|
|
|
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 >
|
|
|
|
_playback.durationCurrent.value.inSeconds);
|
|
|
|
|
|
|
|
if (_playback.durationCurrent.value.inSeconds ==
|
|
|
|
lyricSlice.time.inSeconds &&
|
2024-08-28 10:41:32 +00:00
|
|
|
_isLyricSynced) {
|
|
|
|
_autoScrollController.scrollToIndex(
|
|
|
|
idx,
|
|
|
|
preferPosition: AutoScrollPosition.middle,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
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),
|
2024-08-28 10:41:32 +00:00
|
|
|
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),
|
|
|
|
),
|
2024-08-28 10:41:32 +00:00
|
|
|
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(
|
2024-08-28 10:41:32 +00:00
|
|
|
style: TextStyle(
|
2024-08-29 11:10:54 +00:00
|
|
|
fontSize: isActive ? 20 : 16,
|
2024-08-28 10:41:32 +00:00
|
|
|
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-28 10:41:32 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
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',
|
|
|
|
style: Theme.of(context).textTheme.bodyLarge,
|
|
|
|
),
|
|
|
|
const Text(
|
|
|
|
"This song haven't lyrics that recorded in our database.",
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2024-08-28 10:41:32 +00:00
|
|
|
),
|
2024-08-30 04:56:28 +00:00
|
|
|
const SliverGap(16),
|
2024-08-28 10:41:32 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|