// SPDX-License-Identifier: Apache-2.0 import 'dart:async'; import 'package:badges/badges.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter/material.dart'; import 'package:musicplayer/size.dart'; import 'music_methods/controller.dart'; import 'music_methods/modeClass.dart'; import 'music_methods/musicProvider.dart'; class MusicPageTest extends ConsumerStatefulWidget { List> list; MusicPageTest({Key? key, required this.list}) : super(key: key); @override _MusicPageTestState createState() => _MusicPageTestState(); } class _MusicPageTestState extends ConsumerState { MPDTalker mpdTalker = MPDTalker(); bool isPlaying = false; late int currindex; String currSongTime = '0'; late Timer timer; late Mode mode; @override void initState() { // TODO: implement initState super.initState(); WidgetsBinding.instance.addPostFrameCallback((timeStamp) { ref.read(CurrentSongProvider.notifier).update(artist:widget.list[0]['Artist'].toString(), title:widget.list[0]['file'].toString(), duration: convertToMin(widget.list[0]['Time'].toString()), time: '0'); //timer for progress bar timer = Timer.periodic(Duration(seconds: 1), (timer) async{ Map info = await mpdTalker.cmdMap('status'); ref.read(CurrentSongProvider.notifier).update(time: info['time']); if(int.parse(currSongTime) == int.parse(convertTosimpleStr(widget.list[currindex]['Time'].toString()))-1 && mode.isSingle == false){ ref.read(CurrentSongProvider.notifier).update(isPlaying: true); if(currindex == widget.list.length-1){ currindex = 0; ref.read(currIndexProvider.notifier).update(currindex); ref.read(CurrentSongProvider.notifier).update(artist:widget.list[currindex]['Artist'].toString(), title: widget.list[currindex]['file'].toString(), duration: convertToMin(widget.list[currindex]['Time'].toString(),)); } else{ currindex++; ref.read(currIndexProvider.notifier).update(currindex); ref.read(CurrentSongProvider.notifier).update(artist:widget.list[currindex]['Artist'].toString(), title: widget.list[currindex]['file'].toString(), duration: convertToMin(widget.list[currindex]['Time'].toString(),)); } } }); }); } @override void dispose() { timer.cancel(); super.dispose(); } String convertToMin(String str){ String strforint = ''; for(int i = 0; i Card( color: Colors.blueGrey, shadowColor: Colors.blueAccent, elevation: 5, child: ListTile( minLeadingWidth: 4, textColor: Color.fromARGB(199, 255, 255, 255), title: Text(widget.list[index]['file'].toString()), subtitle: Text(widget.list[index]['Artist'] == null ? '' : widget.list[index]['Artist'].toString(), style: TextStyle(color: Colors.black),), trailing: Text( convertToMin(widget.list[index]['Time'].toString(),), style: TextStyle(color: Colors.black), ), onTap: (){ ref.read(CurrentSongProvider.notifier).update(artist:widget.list[index]['Artist'] == null ? "Unknown": widget.list[index]['Artist'].toString(), title: widget.list[index]['file'].toString(), duration: convertToMin(widget.list[index]['Time'].toString(),) ,isPlaying: true,); mpdTalker.cmdStr('playid ' +widget.list[index]['Id'].toString()); currindex = index; ref.read(currIndexProvider.notifier).update(currindex); }, ), ))), ),), ], )), Flexible( flex: 2, child: Flex( direction: Axis.vertical, children: [ Flexible( flex: 1, child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Flexible( flex: 1, child: Text(convertToMin(currSongTime))), Flexible( flex: 6, child: RotatedBox( quarterTurns: 4, child: Slider( activeColor: Colors.blueGrey, thumbColor: Colors.transparent, value: int.parse(currSongTime).toDouble()/((int.parse(convertTosimpleStr(widget.list[currindex]['Time'].toString())).toDouble())), onChanged:(value){ double seekTime = value*int.parse(convertTosimpleStr(widget.list[currindex]['Time'].toString())).toDouble(); int seektime = seekTime.toInt(); mpdTalker.cmd('seekcur $seektime'); }, max: 1, min: 0, ), ), ), Flexible( flex: 1, child: Text(convertToMin(widget.list[currindex]['Time'].toString()))), ], ),), Flex( direction: Axis.horizontal, children: [ Flexible( flex: 1, child: SizedBox( width: SizeConfig.screenWidth/3, )), Flexible( flex : 1, child: Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Badge( badgeContent: mode.isSingle ? const Text('1') : null, elevation: 0, badgeColor: Colors.greenAccent, child: IconButton( icon: const Icon(Icons.loop), color: Colors.greenAccent, onPressed: (() { if(mode.isSingle){ ref.read(Modeprovider.notifier).update(isSingle: false); mpdTalker.cmd('single 0'); } if(mode.isSingle == false ){ ref.read(Modeprovider.notifier).update(isSingle: true); mpdTalker.cmd('single 1'); } }),), ), IconButton(onPressed: (){ mpdTalker.cmdStr('previous'); ref.read(CurrentSongProvider.notifier).update(isPlaying: true); if(currindex == 0){ ref.read(currIndexProvider.notifier).update(widget.list.length-1); currindex = widget.list.length-1; ref.read(CurrentSongProvider.notifier).update(artist:widget.list[currindex]['Artist'].toString(), title: widget.list[currindex]['file'].toString(), duration: convertToMin(widget.list[currindex]['Time'].toString(),)); } else{ currindex--; ref.read(currIndexProvider.notifier).update(currindex); ref.read(CurrentSongProvider.notifier).update(artist:widget.list[currindex]['Artist'].toString(), title: widget.list[currindex]['file'].toString(), duration: convertToMin(widget.list[currindex]['Time'].toString(),)); } }, icon: Icon(Icons.skip_previous)), IconButton( onPressed: (){ if(isPlaying){ mpdTalker.cmdStr('pause 1'); } else{ mpdTalker.cmdStr('pause 0'); } ref.read(CurrentSongProvider.notifier).update( isPlaying: !isPlaying); }, icon: isPlaying ? Icon(Icons.pause): Icon(Icons.play_arrow) ), IconButton(onPressed: (){ mpdTalker.cmdStr('next'); ref.read(CurrentSongProvider.notifier).update(isPlaying: true); if(currindex == widget.list.length-1){ ref.read(currIndexProvider.notifier).update(0); ref.read(CurrentSongProvider.notifier).update(artist:widget.list[0]['Artist'].toString(), title: widget.list[0]['file'].toString(), duration: convertToMin(widget.list[0]['Time'].toString(),)); } else{ currindex++; ref.read(currIndexProvider.notifier).update(currindex); ref.read(CurrentSongProvider.notifier).update(artist:widget.list[currindex]['Artist'].toString(), title: widget.list[currindex]['file'].toString(), duration: convertToMin(widget.list[currindex]['Time'].toString(),)); } }, icon: Icon(Icons.skip_next)), ], ),), Flexible( flex: 1, child: Row( children: [ Icon(Icons.volume_up), SizedBox( width: SizeConfig.screenWidth/4, child: RotatedBox( quarterTurns: 4, child: Slider(value: ref.watch(VolumeProvider).toDouble()/100, onChanged: (Value){ double vol = Value*100; int songVol = vol.toInt(); mpdTalker.cmd('setvol $songVol'); ref.read(VolumeProvider.notifier).update(songVol); }), ), ), ], ), ) ], ), ], ), ), ], ), ); } }