对接了音乐播放部分和排行榜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 {
final Dio dio = Dio();
Future<RankBean> getRank() async {
Future<RankBean> getRank({required String Authorization,}) async {
Response response = await dio.get(
_getRank,
data: {
'Authorization': Authorization,
},
options: Options(headers: {
'Authorization': Authorization,
'Content-Type': 'application/json;charset=UTF-8'
})
);
print(response.data);
return RankBean.formMap(response.data);

@ -4,6 +4,9 @@ class Song {
final String title;
final String artist;
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{
final box = GetStorage();
bool get isFlag => box.read('isFlag');
bool get isLikes => box.read('isLikes') ?? false;
bool get isLikes => box.read('isLikes');
String get currentToken => box.read('currentToken');
String get currentUsername => box.read('currentUsername') ?? '游客';
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:get/get.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/view/commend_view.dart';
import '../../view_model/home_view_model.dart';
import '../api/api_music_list.dart';
import '../common_widget/Song_widegt.dart';
import '../common_widget/list_cell.dart';
import '../models/getMusicList_bean.dart';
import 'music_view.dart';
class HomeView extends StatefulWidget {
@ -21,32 +24,61 @@ class _HomeViewState extends State<HomeView> {
final TextEditingController _controller = TextEditingController();
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 = [
{"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 = [];
Future<void> _filterData(String query) async {
@ -55,7 +87,8 @@ class _HomeViewState extends State<HomeView> {
if (bean.code == 200) {
setState(() {
_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() ??
[];
_isSearching = true;
@ -68,7 +101,6 @@ class _HomeViewState extends State<HomeView> {
}
}
@override
Widget build(BuildContext context) {
///
@ -88,7 +120,7 @@ class _HomeViewState extends State<HomeView> {
activeColor: const Color(0xff429482), // Color of active dot
),
),
autoplay: true,
// autoplay: true,
autoplayDelay: 3000,
);
return Container(
@ -232,7 +264,7 @@ class _HomeViewState extends State<HomeView> {
shrinkWrap: true,
itemBuilder: (context, index) {
return ListTile(
leading: Image.asset(songs[index].pic),
leading: Image.network(songs[index].pic),
title: Text(
songs[index].title,
style: const TextStyle(fontSize: 18, color: Colors.black),
@ -252,7 +284,9 @@ class _HomeViewState extends State<HomeView> {
context,
MaterialPageRoute(
builder: (context) => MusicView(
song: songs[index], initialSongIndex: index),
song: songs[index],
initialSongIndex: index,
),
),
);
},
@ -350,7 +384,9 @@ class _HomeViewState extends State<HomeView> {
children: [
IconButton(
onPressed: () {
Get.to(() => CommentView(initialSongIndex: index,));
Get.to(() => CommentView(
initialSongIndex: index,
));
},
icon: Image.asset("assets/img/list_comment.png"),
iconSize: 60,

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

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

Loading…
Cancel
Save