对接了音乐播放部分和排行榜token

dev
wuliwudu 10 months ago
parent 14a6a42283
commit 73c9190727

@ -0,0 +1,53 @@
import 'package:dio/dio.dart';
import '../models/getMusicList_bean.dart';
import '../models/getRank_bean.dart';
const String _getMusic1 = 'http://flyingpig.fun:10010/musics/1';
const String _getMusic2 = 'http://flyingpig.fun:10010/musics/2';
const String _getMusic3 = 'http://flyingpig.fun:10010/musics/3';
///
class GetMusic {
final Dio dio = Dio();
Future<MusicListBean> getMusic1({required String Authorization}) async {
Response response = await dio.get(
_getMusic1,
data: {
'Authorization': Authorization,
},
options: Options(headers: {
'Authorization': Authorization,
'Content-Type': 'application/json;charset=UTF-8'
}));
print(response.data);
return MusicListBean.formMap(response.data);
}
Future<MusicListBean> getMusic2({required String Authorization}) async {
Response response = await dio.get(
_getMusic2,
data: {
'Authorization': Authorization,
},
options: Options(headers: {
'Authorization': Authorization,
'Content-Type': 'application/json;charset=UTF-8'
}));
print(response.data);
return MusicListBean.formMap(response.data);
}
Future<MusicListBean> getMusic3({required String Authorization}) async {
Response response = await dio.get(
_getMusic3,
data: {
'Authorization': Authorization,
},
options: Options(headers: {
'Authorization': Authorization,
'Content-Type': 'application/json;charset=UTF-8'
}));
print(response.data);
return MusicListBean.formMap(response.data);
}
}

@ -7,9 +7,16 @@ const String _getRank = 'http://flyingpig.fun:10010/musics/rank-list';
class GetRank { class GetRank {
final Dio dio = Dio(); final Dio dio = Dio();
Future<RankBean> getRank() async { Future<RankBean> getRank({required String Authorization,}) async {
Response response = await dio.get( Response response = await dio.get(
_getRank, _getRank,
data: {
'Authorization': Authorization,
},
options: Options(headers: {
'Authorization': Authorization,
'Content-Type': 'application/json;charset=UTF-8'
})
); );
print(response.data); print(response.data);
return RankBean.formMap(response.data); return RankBean.formMap(response.data);

@ -4,6 +4,9 @@ class Song {
final String title; final String title;
final String artist; final String artist;
final String musicurl; final String musicurl;
final int id;
final bool likes;
final bool collection;
Song( {required this.pic,required this.artistPic,required this.title, required this.artist, required this.musicurl}); Song({required this.pic,required this.artistPic,required this.title, required this.artist, required this.musicurl,required this.id,required this.likes,required this.collection});
} }

@ -3,15 +3,9 @@ import 'package:get_storage/get_storage.dart';
class AppData extends GetxController{ class AppData extends GetxController{
final box = GetStorage(); final box = GetStorage();
bool get isFlag => box.read('isFlag'); bool get isLikes => box.read('isLikes');
bool get isLikes => box.read('isLikes') ?? false;
String get currentToken => box.read('currentToken'); String get currentToken => box.read('currentToken');
String get currentUsername => box.read('currentUsername') ?? '游客'; String get currentUsername => box.read('currentUsername') ?? '游客';
String get currentAvatar=> box.read('currentAvatar') ?? 'http://b.hiphotos.baidu.com/image/pic/item/e824b899a9014c08878b2c4c0e7b02087af4f4a3.jpg'; String get currentAvatar=> box.read('currentAvatar') ?? 'http://b.hiphotos.baidu.com/image/pic/item/e824b899a9014c08878b2c4c0e7b02087af4f4a3.jpg';
initData(){
if(box.read('isFlag')==null){
box.write('isFlag', false);
}
}
} }

@ -1,41 +0,0 @@
// home_view_model.dart
import 'package:get/get.dart';
import 'Song_widegt.dart';
class TryRow extends GetxController {
final RxString txtSearch = ''.obs;
final List<Song> allSongs = [
Song(
artistPic: 'assets/img/music_artist.png',
title: '背对背拥抱1',
artist: '林俊杰 1',
musicurl: 'audio/MAMAMOO.mp3',
pic: 'assets/img/artist_pic.png',
),
// Add more songs here
];
RxList<Song> filteredSongs = <Song>[].obs;
@override
void onInit() {
filteredSongs.assignAll(allSongs);
super.onInit();
}
void updateSearchResults() {
final String query = txtSearch.value.toLowerCase();
if (query.isEmpty) {
filteredSongs.assignAll(allSongs);
} else {
filteredSongs.assignAll(allSongs
.where((song) =>
song.title.toLowerCase().contains(query) ||
song.artist.toLowerCase().contains(query))
.toList());
}
}
}

@ -0,0 +1,27 @@
class MusicListBean {
int? code;
String? msg;
int? id;
String? name;
String? coverPath;
String? musicPath;
String? singerName;
String? uploadUserName;
bool? likeOrNot;
bool? collectOrNot;
MusicListBean.formMap(Map map){
code = map['code'];
msg= map['msg'];
if (map['data'] == '') return;
Map? data = map['data'];
if (data == null) return;
id = data['id'];
name = data['name'];
coverPath = data['coverPath'];
musicPath = data['musicPath'];
singerName = data['singerName'];
uploadUserName = data['uploadUserName'];
likeOrNot = data['likeOrNot'];
collectOrNot = data['collectOrNot'];
}
}

@ -2,11 +2,14 @@ import 'package:flutter/material.dart';
import 'package:flutter_swiper_view/flutter_swiper_view.dart'; import 'package:flutter_swiper_view/flutter_swiper_view.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:music_player_miao/api/api_music_return.dart'; import 'package:music_player_miao/api/api_music_return.dart';
import 'package:music_player_miao/common_widget/app_data.dart';
import 'package:music_player_miao/models/search_bean.dart'; import 'package:music_player_miao/models/search_bean.dart';
import 'package:music_player_miao/view/commend_view.dart'; import 'package:music_player_miao/view/commend_view.dart';
import '../../view_model/home_view_model.dart'; import '../../view_model/home_view_model.dart';
import '../api/api_music_list.dart';
import '../common_widget/Song_widegt.dart'; import '../common_widget/Song_widegt.dart';
import '../common_widget/list_cell.dart'; import '../common_widget/list_cell.dart';
import '../models/getMusicList_bean.dart';
import 'music_view.dart'; import 'music_view.dart';
class HomeView extends StatefulWidget { class HomeView extends StatefulWidget {
@ -21,32 +24,61 @@ class _HomeViewState extends State<HomeView> {
final TextEditingController _controller = TextEditingController(); final TextEditingController _controller = TextEditingController();
bool _isSearching = false; bool _isSearching = false;
void initState() {
super.initState();
_fetchSonglistData();
}
List<Song> songs = [];
Future<void> _fetchSonglistData() async {
MusicListBean bean1 =
await GetMusic().getMusic1(Authorization: AppData().currentToken);
MusicListBean bean2 =
await GetMusic().getMusic2(Authorization: AppData().currentToken);
MusicListBean bean3 =
await GetMusic().getMusic3(Authorization: AppData().currentToken);
setState(() {
songs = [
Song(
artistPic: bean1.coverPath!,
title: bean1.name!,
artist: bean1.singerName!,
musicurl: bean1.musicPath!,
pic: bean1.coverPath!,
id: bean1.id!,
likes: bean1.likeOrNot!,
collection: bean1.collectOrNot!),
Song(
artistPic: bean2.coverPath!,
title: bean2.name!,
artist: bean2.singerName!,
musicurl: bean2.musicPath!,
pic: bean2.coverPath!,
id: bean2.id!,
likes: bean2.likeOrNot!,
collection: bean2.collectOrNot!),
Song(
artistPic: bean3.coverPath!,
title: bean3.name!,
artist: bean3.singerName!,
musicurl: bean3.musicPath!,
pic: bean3.coverPath!,
id: bean3.id!,
likes: bean3.likeOrNot!,
collection: bean3.collectOrNot!),
];
});
}
/// ///
List<Map> imgList = [ List<Map> imgList = [
{"image": "assets/img/banner.png"}, {"image": "assets/img/banner.png"},
{"image": "assets/img/banner.png"}, {"image": "assets/img/banner.png"},
{"image": "assets/img/banner.png"}, {"image": "assets/img/banner.png"},
]; ];
final List<Song> songs = [
Song(
artistPic: 'assets/img/music_artist.png',
title: 'Chuck',
artist: 'MAMAMOO',
musicurl: 'audio/MAMAMOO.mp3',
pic: 'assets/img/artist_pic.png'),
Song(
artistPic: 'assets/img/music_artist.png',
title: 'FLOWER',
artist: 'Jisoo',
musicurl: 'audio/FLOWER.mp3',
pic: 'assets/img/artist_pic.png'),
Song(
artistPic: 'assets/img/music_artist.png',
title: 'All eyes on me',
artist: 'Jisoo',
musicurl: 'audio/All.mp3',
pic: 'assets/img/artist_pic.png'),
];
List<String> _filteredData = []; List<String> _filteredData = [];
Future<void> _filterData(String query) async { Future<void> _filterData(String query) async {
@ -55,7 +87,8 @@ class _HomeViewState extends State<HomeView> {
if (bean.code == 200) { if (bean.code == 200) {
setState(() { setState(() {
_filteredData = bean.data _filteredData = bean.data
?.map((data) => "${data.name} ") // Adjust this based on your data structure ?.map((data) =>
"${data.name} ") // Adjust this based on your data structure
.toList() ?? .toList() ??
[]; [];
_isSearching = true; _isSearching = true;
@ -68,7 +101,6 @@ class _HomeViewState extends State<HomeView> {
} }
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
/// ///
@ -88,7 +120,7 @@ class _HomeViewState extends State<HomeView> {
activeColor: const Color(0xff429482), // Color of active dot activeColor: const Color(0xff429482), // Color of active dot
), ),
), ),
autoplay: true, // autoplay: true,
autoplayDelay: 3000, autoplayDelay: 3000,
); );
return Container( return Container(
@ -232,7 +264,7 @@ class _HomeViewState extends State<HomeView> {
shrinkWrap: true, shrinkWrap: true,
itemBuilder: (context, index) { itemBuilder: (context, index) {
return ListTile( return ListTile(
leading: Image.asset(songs[index].pic), leading: Image.network(songs[index].pic),
title: Text( title: Text(
songs[index].title, songs[index].title,
style: const TextStyle(fontSize: 18, color: Colors.black), style: const TextStyle(fontSize: 18, color: Colors.black),
@ -252,7 +284,9 @@ class _HomeViewState extends State<HomeView> {
context, context,
MaterialPageRoute( MaterialPageRoute(
builder: (context) => MusicView( builder: (context) => MusicView(
song: songs[index], initialSongIndex: index), song: songs[index],
initialSongIndex: index,
),
), ),
); );
}, },
@ -350,7 +384,9 @@ class _HomeViewState extends State<HomeView> {
children: [ children: [
IconButton( IconButton(
onPressed: () { onPressed: () {
Get.to(() => CommentView(initialSongIndex: index,)); Get.to(() => CommentView(
initialSongIndex: index,
));
}, },
icon: Image.asset("assets/img/list_comment.png"), icon: Image.asset("assets/img/list_comment.png"),
iconSize: 60, iconSize: 60,

@ -5,7 +5,9 @@ import 'package:music_player_miao/common_widget/app_data.dart';
import '../../view_model/home_view_model.dart'; import '../../view_model/home_view_model.dart';
import 'package:audioplayers/audioplayers.dart'; import 'package:audioplayers/audioplayers.dart';
import '../api/api_music_list.dart';
import '../common_widget/Song_widegt.dart'; import '../common_widget/Song_widegt.dart';
import '../models/getMusicList_bean.dart';
class MusicView extends StatefulWidget { class MusicView extends StatefulWidget {
final Song song; final Song song;
@ -25,35 +27,41 @@ class _MusicViewState extends State<MusicView> {
AppData appData = AppData(); AppData appData = AppData();
late int currentSongIndex; late int currentSongIndex;
List<String> song2 = [ List song2 = [];
"audio/MAMAMOO.mp3", List artist = [];
"audio/FLOWER.mp3", List music = [];
"audio/All.mp3" List likes = [];
];
List<String> artist = [ Future<void> _fetchSonglistData() async {
"林俊杰 1", MusicListBean bean1 =
"林俊杰 2", await GetMusic().getMusic1(Authorization: AppData().currentToken);
"林俊杰 3" MusicListBean bean2 =
]; await GetMusic().getMusic2(Authorization: AppData().currentToken);
List<String> music = [ MusicListBean bean3 =
"背对背拥抱 1", await GetMusic().getMusic3(Authorization: AppData().currentToken);
"背对背拥抱 2",
"背对背拥抱 3" setState(() {
]; song2 = [bean1.musicPath,bean2.musicPath,bean3.musicPath];
artist = [bean1.singerName,bean2.singerName,bean3.singerName];
music = [bean1.name,bean2.name,bean3.name];
likes = [bean1.likeOrNot,bean2.likeOrNot,bean3.likeOrNot];
});
}
late AudioPlayer _audioPlayer; late AudioPlayer _audioPlayer;
late Duration _duration; late Duration _duration;
late Duration _position; late Duration _position; late String artistName;
late String artistName;
late String musicName; late String musicName;
late bool likesnot;
void playerInit() { void playerInit() {
_audioPlayer = AudioPlayer()..setSourceAsset('${widget.song.musicurl}'); _audioPlayer = AudioPlayer()..setSourceUrl('${widget.song.musicurl}');
_duration = const Duration(); _duration = const Duration();
_position = const Duration(); _position = const Duration();
artistName = '${widget.song.artist}'; artistName = '${widget.song.artist}';
musicName = '${widget.song.title}'; musicName = '${widget.song.title}';
likesnot = widget.song.likes;
_audioPlayer.onDurationChanged.listen((Duration d) { _audioPlayer.onDurationChanged.listen((Duration d) {
_duration = d; _duration = d;
@ -88,10 +96,10 @@ class _MusicViewState extends State<MusicView> {
} else { } else {
currentSongIndex = 0; currentSongIndex = 0;
} }
_audioPlayer.setSourceUrl(song2[currentSongIndex]);
_audioPlayer.setSourceAsset(song2[currentSongIndex]);
artistName = artist[currentSongIndex]; artistName = artist[currentSongIndex];
musicName = music[currentSongIndex]; musicName = music[currentSongIndex];
likesnot = likes[currentSongIndex];
_audioPlayer.resume(); _audioPlayer.resume();
} }
@ -102,9 +110,10 @@ class _MusicViewState extends State<MusicView> {
currentSongIndex = 2; currentSongIndex = 2;
} }
_audioPlayer.setSourceAsset(song2[currentSongIndex]); _audioPlayer.setSourceUrl(song2[currentSongIndex]);
artistName = artist[currentSongIndex]; artistName = artist[currentSongIndex];
musicName = music[currentSongIndex]; musicName = music[currentSongIndex];
likesnot = likes[currentSongIndex];
_audioPlayer.resume(); _audioPlayer.resume();
} }
@ -120,6 +129,7 @@ class _MusicViewState extends State<MusicView> {
void initState() { void initState() {
playerInit(); playerInit();
currentSongIndex = widget.initialSongIndex; currentSongIndex = widget.initialSongIndex;
_fetchSonglistData();
super.initState(); super.initState();
} }
@ -203,7 +213,7 @@ class _MusicViewState extends State<MusicView> {
Positioned( Positioned(
child: ClipRRect( child: ClipRRect(
borderRadius: BorderRadius.circular(80), borderRadius: BorderRadius.circular(80),
child: Image.asset( child: Image.network(
'${widget.song.artistPic}', '${widget.song.artistPic}',
width: 230, width: 230,
height: 230, height: 230,
@ -240,12 +250,12 @@ class _MusicViewState extends State<MusicView> {
IconButton( IconButton(
onPressed: () { onPressed: () {
setState(() { setState(() {
isGoodCommended = !isGoodCommended; likesnot = !likesnot;
appData.box.write('isLikes', isGoodCommended); appData.box.write('isLikes', isGoodCommended);
}); });
}, },
icon: Image.asset( icon: Image.asset(
isGoodCommended likesnot
? "assets/img/music_good.png" ? "assets/img/music_good.png"
: "assets/img/music_good_un.png", : "assets/img/music_good_un.png",
width: 29, width: 29,
@ -473,9 +483,10 @@ class _MusicViewState extends State<MusicView> {
if (!_isDisposed) { // Check the flag before using the player if (!_isDisposed) { // Check the flag before using the player
setState(() { setState(() {
currentSongIndex = index; currentSongIndex = index;
_audioPlayer.setSourceAsset(song2[currentSongIndex]); _audioPlayer.setSourceUrl(song2[currentSongIndex]);
artistName = artist[currentSongIndex]; artistName = artist[currentSongIndex];
musicName = music[currentSongIndex]; musicName = music[currentSongIndex];
likesnot = likes[currentSongIndex];
_audioPlayer.resume(); _audioPlayer.resume();
}); });
} }

@ -1,5 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:music_player_miao/common_widget/app_data.dart';
import '../api/api_music_rank.dart'; import '../api/api_music_rank.dart';
import '../common_widget/rank_song_row.dart'; import '../common_widget/rank_song_row.dart';
import '../models/getRank_bean.dart'; import '../models/getRank_bean.dart';
@ -25,7 +26,7 @@ class _RankViewState extends State<RankView> {
} }
Future<void> _fetchSonglistData() async { Future<void> _fetchSonglistData() async {
RankBean bean2 = await GetRank().getRank(); RankBean bean2 = await GetRank().getRank(Authorization: AppData().currentToken);
setState(() { setState(() {
rankNames = bean2.data!.map((data) => data.name!).toList(); rankNames = bean2.data!.map((data) => data.name!).toList();
rankSingerName = bean2.data!.map((data) => data.singerName!).toList(); rankSingerName = bean2.data!.map((data) => data.singerName!).toList();
@ -163,17 +164,7 @@ class _RankViewState extends State<RankView> {
width: 60, width: 60,
height: 60, height: 60,
fit: BoxFit.cover, fit: BoxFit.cover,
errorBuilder:
(context, error, stackTrace) {
//
return Image.asset(
'assets/img/app_logo.png',
//
width: 60,
height: 60,
fit: BoxFit.cover,
);
},
), ),
), ),
const SizedBox( const SizedBox(

Loading…
Cancel
Save