From 73c9190727ed3a8c0432b75ebc49135f742dadd2 Mon Sep 17 00:00:00 2001 From: wuliwudu <2973566943@qq.com> Date: Wed, 7 Feb 2024 17:06:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E6=8E=A5=E4=BA=86=E9=9F=B3=E4=B9=90?= =?UTF-8?q?=E6=92=AD=E6=94=BE=E9=83=A8=E5=88=86=E5=92=8C=E6=8E=92=E8=A1=8C?= =?UTF-8?q?=E6=A6=9Ctoken?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/api/api_music_list.dart | 53 ++++++ lib/api/api_music_rank.dart | 9 +- lib/common_widget/Song_widegt.dart | 5 +- lib/common_widget/app_data.dart | 8 +- lib/common_widget/try_row.dart | 41 ----- lib/models/getMusicList_bean.dart | 27 +++ lib/view/home_view.dart | 270 ++++++++++++++++------------- lib/view/music_view.dart | 71 ++++---- lib/view/rank_view.dart | 15 +- 9 files changed, 290 insertions(+), 209 deletions(-) create mode 100644 lib/api/api_music_list.dart delete mode 100644 lib/common_widget/try_row.dart create mode 100644 lib/models/getMusicList_bean.dart diff --git a/lib/api/api_music_list.dart b/lib/api/api_music_list.dart new file mode 100644 index 0000000..37eb2a9 --- /dev/null +++ b/lib/api/api_music_list.dart @@ -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 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 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 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); + } +} diff --git a/lib/api/api_music_rank.dart b/lib/api/api_music_rank.dart index 02c7fc7..d328e2d 100644 --- a/lib/api/api_music_rank.dart +++ b/lib/api/api_music_rank.dart @@ -7,9 +7,16 @@ const String _getRank = 'http://flyingpig.fun:10010/musics/rank-list'; class GetRank { final Dio dio = Dio(); - Future getRank() async { + Future 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); diff --git a/lib/common_widget/Song_widegt.dart b/lib/common_widget/Song_widegt.dart index 50cfd43..96109cc 100644 --- a/lib/common_widget/Song_widegt.dart +++ b/lib/common_widget/Song_widegt.dart @@ -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}); } diff --git a/lib/common_widget/app_data.dart b/lib/common_widget/app_data.dart index b4551e6..cf17ae9 100644 --- a/lib/common_widget/app_data.dart +++ b/lib/common_widget/app_data.dart @@ -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); - } - } } \ No newline at end of file diff --git a/lib/common_widget/try_row.dart b/lib/common_widget/try_row.dart deleted file mode 100644 index 43f3a42..0000000 --- a/lib/common_widget/try_row.dart +++ /dev/null @@ -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 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 filteredSongs = [].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()); - } - } -} diff --git a/lib/models/getMusicList_bean.dart b/lib/models/getMusicList_bean.dart new file mode 100644 index 0000000..2c419ac --- /dev/null +++ b/lib/models/getMusicList_bean.dart @@ -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']; + } +} diff --git a/lib/view/home_view.dart b/lib/view/home_view.dart index 51ad755..931365c 100644 --- a/lib/view/home_view.dart +++ b/lib/view/home_view.dart @@ -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 { final TextEditingController _controller = TextEditingController(); bool _isSearching = false; + void initState() { + super.initState(); + _fetchSonglistData(); + } + List songs = []; + + Future _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 imgList = [ {"image": "assets/img/banner.png"}, {"image": "assets/img/banner.png"}, {"image": "assets/img/banner.png"}, ]; - final List 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 _filteredData = []; Future _filterData(String query) async { @@ -55,7 +87,8 @@ class _HomeViewState extends State { 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 { } } - @override Widget build(BuildContext context) { ///轮播图 @@ -88,7 +120,7 @@ class _HomeViewState extends State { activeColor: const Color(0xff429482), // Color of active dot ), ), - autoplay: true, + // autoplay: true, autoplayDelay: 3000, ); return Container( @@ -114,7 +146,7 @@ class _HomeViewState extends State { Text( '喵听', style: - TextStyle(fontSize: 35, fontWeight: FontWeight.bold), + TextStyle(fontSize: 35, fontWeight: FontWeight.bold), ), SizedBox( width: 10, @@ -122,7 +154,7 @@ class _HomeViewState extends State { Text( '你的云端音乐库', style: - TextStyle(fontSize: 20, fontWeight: FontWeight.w500), + TextStyle(fontSize: 20, fontWeight: FontWeight.w500), ), ], ), @@ -149,7 +181,7 @@ class _HomeViewState extends State { child: TextField( controller: _controller, onChanged: (query) { - setState(() async{ + setState(() async { _filterData(query); }); }, @@ -185,8 +217,8 @@ class _HomeViewState extends State { height: 150, width: 345, decoration: BoxDecoration( - color: const Color(0xffF9F2AF).withOpacity(0.7), - ), + color: const Color(0xffF9F2AF).withOpacity(0.7), + ), child: ListView.builder( padding: EdgeInsets.zero, itemCount: _filteredData.length, @@ -232,7 +264,7 @@ class _HomeViewState extends State { 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), @@ -243,7 +275,7 @@ class _HomeViewState extends State { ), trailing: InkWell( onTap: () { - _bottomSheet(context,index); + _bottomSheet(context, index); }, child: Image.asset('assets/img/More.png'), ), @@ -252,7 +284,9 @@ class _HomeViewState extends State { context, MaterialPageRoute( builder: (context) => MusicView( - song: songs[index], initialSongIndex: index), + song: songs[index], + initialSongIndex: index, + ), ), ); }, @@ -291,110 +325,112 @@ class _HomeViewState extends State { ); } - Future _bottomSheet(BuildContext context,int index) { + Future _bottomSheet(BuildContext context, int index) { return showModalBottomSheet( context: context, backgroundColor: Colors.white, shape: const RoundedRectangleBorder( borderRadius: BorderRadius.vertical(top: Radius.circular(30))), builder: (context) => Container( - height: 210, - padding: const EdgeInsets.only(top: 20), - child: Column( - crossAxisAlignment: CrossAxisAlignment.stretch, + height: 210, + padding: const EdgeInsets.only(top: 20), + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Row( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ - Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, + Column( children: [ - Column( - children: [ - IconButton( - onPressed: () {}, - icon: Image.asset("assets/img/list_add.png"), - iconSize: 60, - ), - const Text("加入歌单") - ], - ), - Column( - children: [ - IconButton( - onPressed: () {}, - icon: Image.asset("assets/img/list_download.png"), - iconSize: 60, - ), - const Text("下载") - ], - ), - Column( - children: [ - IconButton( - onPressed: () {}, - icon: Image.asset("assets/img/list_collection.png"), - iconSize: 60, - ), - const Text("收藏") - ], - ), - Column( - children: [ - IconButton( - onPressed: () {}, - icon: Image.asset("assets/img/list_good.png"), - iconSize: 60, - ), - const Text("点赞") - ], + IconButton( + onPressed: () {}, + icon: Image.asset("assets/img/list_add.png"), + iconSize: 60, ), - Column( - children: [ - IconButton( - onPressed: () { - Get.to(() => CommentView(initialSongIndex: index,)); - }, - icon: Image.asset("assets/img/list_comment.png"), - iconSize: 60, - ), - const Text("评论") - ], + const Text("加入歌单") + ], + ), + Column( + children: [ + IconButton( + onPressed: () {}, + icon: Image.asset("assets/img/list_download.png"), + iconSize: 60, ), + const Text("下载") ], ), - const SizedBox( - height: 10, + Column( + children: [ + IconButton( + onPressed: () {}, + icon: Image.asset("assets/img/list_collection.png"), + iconSize: 60, + ), + const Text("收藏") + ], ), - ElevatedButton( - onPressed: () {}, - child: const Text( - "查看详情页", - style: TextStyle(color: Colors.black, fontSize: 18), - ), - style: ElevatedButton.styleFrom( - backgroundColor: const Color(0xffE6F4F1), - padding: const EdgeInsets.symmetric(vertical: 8), - tapTargetSize: MaterialTapTargetSize.shrinkWrap, - shape: const RoundedRectangleBorder( - borderRadius: BorderRadius.zero, + Column( + children: [ + IconButton( + onPressed: () {}, + icon: Image.asset("assets/img/list_good.png"), + iconSize: 60, ), - ), + const Text("点赞") + ], ), - ElevatedButton( - onPressed: () => Navigator.pop(context), - style: ElevatedButton.styleFrom( - backgroundColor: const Color(0xff429482), - padding: const EdgeInsets.symmetric(vertical: 8), - tapTargetSize: MaterialTapTargetSize.shrinkWrap, - shape: const RoundedRectangleBorder( - borderRadius: BorderRadius.zero, + Column( + children: [ + IconButton( + onPressed: () { + Get.to(() => CommentView( + initialSongIndex: index, + )); + }, + icon: Image.asset("assets/img/list_comment.png"), + iconSize: 60, ), - ), - child: const Text( - "取消", - style: TextStyle(color: Colors.black, fontSize: 18), - ), + const Text("评论") + ], ), ], ), - )); + const SizedBox( + height: 10, + ), + ElevatedButton( + onPressed: () {}, + child: const Text( + "查看详情页", + style: TextStyle(color: Colors.black, fontSize: 18), + ), + style: ElevatedButton.styleFrom( + backgroundColor: const Color(0xffE6F4F1), + padding: const EdgeInsets.symmetric(vertical: 8), + tapTargetSize: MaterialTapTargetSize.shrinkWrap, + shape: const RoundedRectangleBorder( + borderRadius: BorderRadius.zero, + ), + ), + ), + ElevatedButton( + onPressed: () => Navigator.pop(context), + style: ElevatedButton.styleFrom( + backgroundColor: const Color(0xff429482), + padding: const EdgeInsets.symmetric(vertical: 8), + tapTargetSize: MaterialTapTargetSize.shrinkWrap, + shape: const RoundedRectangleBorder( + borderRadius: BorderRadius.zero, + ), + ), + child: const Text( + "取消", + style: TextStyle(color: Colors.black, fontSize: 18), + ), + ), + ], + ), + )); } } diff --git a/lib/view/music_view.dart b/lib/view/music_view.dart index 3a6e728..7715bdd 100644 --- a/lib/view/music_view.dart +++ b/lib/view/music_view.dart @@ -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 { AppData appData = AppData(); late int currentSongIndex; - List song2 = [ - "audio/MAMAMOO.mp3", - "audio/FLOWER.mp3", - "audio/All.mp3" - ]; - List artist = [ - "林俊杰 1", - "林俊杰 2", - "林俊杰 3" - ]; - List music = [ - "背对背拥抱 1", - "背对背拥抱 2", - "背对背拥抱 3" - ]; + List song2 = []; + List artist = []; + List music = []; + List likes = []; + + Future _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 { } 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 { 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 { void initState() { playerInit(); currentSongIndex = widget.initialSongIndex; + _fetchSonglistData(); super.initState(); } @@ -203,7 +213,7 @@ class _MusicViewState extends State { 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 { 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, @@ -299,7 +309,7 @@ class _MusicViewState extends State { thumbShape: RoundSliderThumbShape( enabledThumbRadius: 7.0), // 调整拇指的大小 overlayShape: - RoundSliderOverlayShape(overlayRadius: 12.0), + RoundSliderOverlayShape(overlayRadius: 12.0), ), child: Slider( min: 0, @@ -354,11 +364,11 @@ class _MusicViewState extends State { onPressed: playOrPause, icon: _audioPlayer.state == PlayerState.playing ? Image.asset( - "assets/img/music_play.png", - ) + "assets/img/music_play.png", + ) : Image.asset( - "assets/img/music_pause.png", - )), + "assets/img/music_pause.png", + )), const SizedBox( width: 10, ), @@ -473,9 +483,10 @@ class _MusicViewState extends State { 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(); }); } diff --git a/lib/view/rank_view.dart b/lib/view/rank_view.dart index 505351c..0666064 100644 --- a/lib/view/rank_view.dart +++ b/lib/view/rank_view.dart @@ -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 { } Future _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 { 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(