2020-11-09 21:05:47 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:audio_service/audio_service.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2021-09-01 12:38:32 +00:00
|
|
|
import 'package:flutter/services.dart';
|
2020-11-09 21:05:47 +00:00
|
|
|
import 'package:freezer/api/deezer.dart';
|
|
|
|
import 'package:freezer/api/definitions.dart';
|
2021-02-09 20:14:14 +00:00
|
|
|
import 'package:freezer/api/player.dart';
|
|
|
|
import 'package:freezer/settings.dart';
|
2020-11-09 21:05:47 +00:00
|
|
|
import 'package:freezer/ui/elements.dart';
|
|
|
|
import 'package:freezer/translations.i18n.dart';
|
|
|
|
import 'package:freezer/ui/error.dart';
|
2021-09-01 12:38:32 +00:00
|
|
|
import 'package:freezer/ui/player_bar.dart';
|
2020-11-09 21:05:47 +00:00
|
|
|
|
|
|
|
class LyricsScreen extends StatefulWidget {
|
2021-09-01 12:38:32 +00:00
|
|
|
LyricsScreen({Key? key}) : super(key: key);
|
2020-11-09 21:05:47 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
_LyricsScreenState createState() => _LyricsScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LyricsScreenState extends State<LyricsScreen> {
|
2021-09-01 12:38:32 +00:00
|
|
|
late StreamSubscription _mediaItemSub;
|
|
|
|
late StreamSubscription _playbackStateSub;
|
|
|
|
int? _currentIndex = -1;
|
|
|
|
int? _prevIndex = -1;
|
2020-11-09 21:05:47 +00:00
|
|
|
ScrollController _controller = ScrollController();
|
|
|
|
final double height = 90;
|
2021-09-01 12:38:32 +00:00
|
|
|
Lyrics? lyrics;
|
|
|
|
bool _loading = true;
|
|
|
|
Object? _error;
|
2020-11-09 21:05:47 +00:00
|
|
|
|
2021-04-05 15:35:51 +00:00
|
|
|
bool _freeScroll = false;
|
2021-04-05 20:27:54 +00:00
|
|
|
bool _animatedScroll = false;
|
2021-04-05 15:35:51 +00:00
|
|
|
|
2021-09-01 12:38:32 +00:00
|
|
|
Future _loadForId(String trackId) async {
|
|
|
|
//Fetch
|
|
|
|
if (_loading == false && lyrics != null)
|
2020-11-09 21:05:47 +00:00
|
|
|
setState(() {
|
2021-09-01 12:38:32 +00:00
|
|
|
_loading = true;
|
|
|
|
lyrics = null;
|
2020-11-09 21:05:47 +00:00
|
|
|
});
|
|
|
|
try {
|
2021-09-01 12:38:32 +00:00
|
|
|
Lyrics l = await deezerAPI.lyrics(trackId);
|
2020-11-09 21:05:47 +00:00
|
|
|
setState(() {
|
|
|
|
_loading = false;
|
|
|
|
lyrics = l;
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
setState(() {
|
2021-09-01 12:38:32 +00:00
|
|
|
_error = e;
|
2020-11-09 21:05:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 20:27:54 +00:00
|
|
|
Future<void> _scrollToLyric() async {
|
2021-04-05 15:35:51 +00:00
|
|
|
//Lyric height, screen height, appbar height
|
2021-09-01 12:38:32 +00:00
|
|
|
double _scrollTo = (height * _currentIndex!) -
|
2021-04-05 15:35:51 +00:00
|
|
|
(MediaQuery.of(context).size.height / 2) +
|
|
|
|
(height / 2) +
|
|
|
|
56;
|
|
|
|
if (0 > _scrollTo) return;
|
2021-04-05 20:27:54 +00:00
|
|
|
_animatedScroll = true;
|
|
|
|
await _controller.animateTo(_scrollTo,
|
2021-04-05 15:35:51 +00:00
|
|
|
duration: Duration(milliseconds: 250), curve: Curves.ease);
|
2021-04-05 20:27:54 +00:00
|
|
|
_animatedScroll = false;
|
2021-04-05 15:35:51 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 21:05:47 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
2021-02-09 20:14:14 +00:00
|
|
|
//Enable visualizer
|
2021-08-30 12:51:51 +00:00
|
|
|
// if (settings.lyricsVisualizer) playerHelper.startVisualizer();
|
2021-09-01 12:38:32 +00:00
|
|
|
_playbackStateSub = AudioService.position.listen((position) {
|
2020-11-09 21:05:47 +00:00
|
|
|
if (_loading) return;
|
2021-09-01 12:38:32 +00:00
|
|
|
_currentIndex =
|
|
|
|
lyrics?.lyrics?.lastIndexWhere((l) => l.offset! <= position);
|
2020-11-09 21:05:47 +00:00
|
|
|
//Scroll to current lyric
|
2021-09-01 12:38:32 +00:00
|
|
|
if (_currentIndex! < 0) return;
|
2020-11-09 21:05:47 +00:00
|
|
|
if (_prevIndex == _currentIndex) return;
|
2021-04-04 22:59:57 +00:00
|
|
|
//Update current lyric index
|
|
|
|
setState(() => null);
|
2020-11-09 21:05:47 +00:00
|
|
|
_prevIndex = _currentIndex;
|
2021-04-05 15:35:51 +00:00
|
|
|
if (_freeScroll) return;
|
|
|
|
_scrollToLyric();
|
2020-11-09 21:05:47 +00:00
|
|
|
});
|
2021-09-01 12:38:32 +00:00
|
|
|
if (audioHandler.mediaItem.value != null)
|
|
|
|
_loadForId(audioHandler.mediaItem.value!.id);
|
|
|
|
|
|
|
|
/// Track change = ~exit~ reload lyrics
|
|
|
|
_mediaItemSub = audioHandler.mediaItem.listen((mediaItem) {
|
|
|
|
if (mediaItem == null) return;
|
|
|
|
_controller.jumpTo(0.0);
|
|
|
|
_loadForId(mediaItem.id);
|
2020-11-09 21:05:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2021-09-01 12:38:32 +00:00
|
|
|
_mediaItemSub.cancel();
|
|
|
|
_playbackStateSub.cancel();
|
2021-02-09 20:14:14 +00:00
|
|
|
//Stop visualizer
|
2021-08-30 12:51:51 +00:00
|
|
|
// if (settings.lyricsVisualizer) playerHelper.stopVisualizer();
|
2020-11-09 21:05:47 +00:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-09-01 12:38:32 +00:00
|
|
|
return AnnotatedRegion<SystemUiOverlayStyle>(
|
|
|
|
value: Theme.of(context).brightness == Brightness.dark
|
|
|
|
? SystemUiOverlayStyle.dark
|
|
|
|
: SystemUiOverlayStyle.light,
|
|
|
|
child: Scaffold(
|
|
|
|
appBar: FreezerAppBar('Lyrics'.i18n),
|
|
|
|
body: SafeArea(
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Theme(
|
|
|
|
data: settings.themeData!.copyWith(
|
|
|
|
textButtonTheme: TextButtonThemeData(
|
|
|
|
style: ButtonStyle(
|
|
|
|
foregroundColor:
|
|
|
|
MaterialStateProperty.all(Colors.white)))),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
if (_freeScroll && !_loading)
|
|
|
|
TextButton(
|
|
|
|
onPressed: () {
|
|
|
|
setState(() => _freeScroll = false);
|
|
|
|
_scrollToLyric();
|
|
|
|
},
|
|
|
|
child: Text(
|
|
|
|
_currentIndex! >= 0
|
|
|
|
? (lyrics?.lyrics?[_currentIndex!].text ??
|
|
|
|
'...')
|
|
|
|
: '...',
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
),
|
|
|
|
style: ButtonStyle(
|
|
|
|
foregroundColor:
|
|
|
|
MaterialStateProperty.all(Colors.white)))
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: Stack(children: [
|
|
|
|
//Lyrics
|
|
|
|
_error != null
|
|
|
|
?
|
|
|
|
//Shouldn't really happen, empty lyrics have own text
|
|
|
|
ErrorScreen(message: _error.toString())
|
|
|
|
:
|
|
|
|
// Loading lyrics
|
|
|
|
_loading
|
|
|
|
? Center(child: CircularProgressIndicator())
|
|
|
|
: NotificationListener(
|
|
|
|
onNotification: (Notification notification) {
|
|
|
|
if (_freeScroll ||
|
|
|
|
notification is! ScrollStartNotification)
|
|
|
|
return false;
|
|
|
|
if (!_animatedScroll && !_loading)
|
|
|
|
setState(() => _freeScroll = true);
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
child: ListView.builder(
|
|
|
|
controller: _controller,
|
|
|
|
padding: EdgeInsets.fromLTRB(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
settings.lyricsVisualizer! && false
|
|
|
|
? 100
|
|
|
|
: 0),
|
|
|
|
itemCount: lyrics!.lyrics!.length,
|
|
|
|
itemBuilder: (BuildContext context, int i) {
|
|
|
|
return Padding(
|
|
|
|
padding: EdgeInsets.symmetric(
|
|
|
|
horizontal: 8.0),
|
|
|
|
child: Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
borderRadius:
|
|
|
|
BorderRadius.circular(8.0),
|
|
|
|
color: _currentIndex == i
|
|
|
|
? Colors.grey
|
|
|
|
.withOpacity(0.25)
|
|
|
|
: Colors.transparent,
|
|
|
|
),
|
|
|
|
height: height,
|
|
|
|
child: InkWell(
|
|
|
|
borderRadius:
|
|
|
|
BorderRadius.circular(8.0),
|
|
|
|
onTap: lyrics!.id != null
|
|
|
|
? () => audioHandler.seek(
|
|
|
|
lyrics!
|
|
|
|
.lyrics![i].offset!)
|
|
|
|
: null,
|
|
|
|
child: Center(
|
|
|
|
child: Text(
|
|
|
|
lyrics!.lyrics![i].text!,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 26.0,
|
|
|
|
fontWeight:
|
|
|
|
(_currentIndex == i)
|
|
|
|
? FontWeight
|
|
|
|
.bold
|
|
|
|
: FontWeight
|
|
|
|
.normal),
|
|
|
|
),
|
|
|
|
))));
|
|
|
|
},
|
|
|
|
)),
|
|
|
|
|
|
|
|
//Visualizer
|
|
|
|
//if (settings.lyricsVisualizer)
|
|
|
|
// Positioned(
|
|
|
|
// bottom: 0,
|
|
|
|
// left: 0,
|
|
|
|
// right: 0,
|
|
|
|
// child: StreamBuilder(
|
|
|
|
// stream: playerHelper.visualizerStream,
|
|
|
|
// builder: (BuildContext context, AsyncSnapshot snapshot) {
|
|
|
|
// List<double> data = snapshot.data ?? [];
|
|
|
|
// double width = MediaQuery.of(context).size.width /
|
|
|
|
// data.length; //- 0.25;
|
|
|
|
// return Row(
|
|
|
|
// crossAxisAlignment: CrossAxisAlignment.end,
|
|
|
|
// children: List.generate(
|
|
|
|
// data.length,
|
|
|
|
// (i) => AnimatedContainer(
|
|
|
|
// duration: Duration(milliseconds: 130),
|
|
|
|
// color: settings.primaryColor,
|
|
|
|
// height: data[i] * 100,
|
|
|
|
// width: width,
|
|
|
|
// )),
|
|
|
|
// );
|
|
|
|
// }),
|
|
|
|
// ),
|
|
|
|
]),
|
|
|
|
),
|
|
|
|
PlayerBar(shouldHandleClicks: false),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
);
|
2020-11-09 21:05:47 +00:00
|
|
|
}
|
|
|
|
}
|