fix: 修复Mini播放器初始化数组大小为0的bug

chen
Spark 8 months ago
parent ffc5c95a55
commit 10d326f3ff

File diff suppressed because one or more lines are too long

@ -287,7 +287,7 @@ class _HomeViewState extends State<HomeView>
), ),
const SizedBox(width: 10), const SizedBox(width: 10),
const Text( const Text(
"大家都在搜《背对背拥抱》", "搜索你想找的音乐",
style: TextStyle( style: TextStyle(
color: Color(0xffA5A5A5), color: Color(0xffA5A5A5),
fontSize: 13, fontSize: 13,

@ -111,12 +111,12 @@ class MiniPlayer extends StatelessWidget {
maxWidth: 42, maxWidth: 42,
maxHeight: 42, maxHeight: 42,
), ),
onPressed: null, // onPressed: null, //
icon: Image.asset( icon: Image.asset(
"assets/img/music_pause.png", "assets/img/music_pause.png",
width: 25, width: 25,
height: 25, height: 25,
color: Colors.grey, // 使 color: Colors.grey, // 使
), ),
); );
} }
@ -142,12 +142,12 @@ class MiniPlayer extends StatelessWidget {
), ),
onPressed: audioController.playOrPause, onPressed: audioController.playOrPause,
icon: Obx(() => Image.asset( icon: Obx(() => Image.asset(
audioController.isPlaying.value audioController.isPlaying.value
? "assets/img/music_play.png" ? "assets/img/music_play.png"
: "assets/img/music_pause.png", : "assets/img/music_pause.png",
width: 25, width: 25,
height: 25, height: 25,
)), )),
); );
}), }),
), ),
@ -158,21 +158,22 @@ class MiniPlayer extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Obx(() { return Obx(() {
final bool hasPlaylist = audioController.songList.isNotEmpty; final bool hasPlaylist = audioController.songList.isNotEmpty;
return GestureDetector( return GestureDetector(
onHorizontalDragEnd: hasPlaylist ? (DragEndDetails details) { onHorizontalDragEnd: hasPlaylist
if (audioController.songList.isEmpty) return; ? (DragEndDetails details) {
final velocity = details.velocity.pixelsPerSecond.dx; if (audioController.songList.isEmpty) return;
const threshold = 300.0; final velocity = details.velocity.pixelsPerSecond.dx;
const threshold = 300.0;
if (velocity > threshold) { if (velocity > threshold) {
audioController.playPrevious(); audioController.playPrevious();
HapticFeedback.mediumImpact(); HapticFeedback.mediumImpact();
} else if (velocity < -threshold) { } else if (velocity < -threshold) {
audioController.playNext(); audioController.playNext();
HapticFeedback.mediumImpact(); HapticFeedback.mediumImpact();
} }
} : null, }
: null,
child: Container( child: Container(
height: 64, // 使 height: 64, // 使
decoration: BoxDecoration( decoration: BoxDecoration(
@ -207,12 +208,15 @@ class MiniPlayer extends StatelessWidget {
child: Hero( child: Hero(
tag: 'album_cover', tag: 'album_cover',
flightShuttleBuilder: ( flightShuttleBuilder: (
BuildContext flightContext, BuildContext flightContext,
Animation<double> animation, Animation<double> animation,
HeroFlightDirection flightDirection, HeroFlightDirection flightDirection,
BuildContext fromHeroContext, BuildContext fromHeroContext,
BuildContext toHeroContext, BuildContext toHeroContext,
) { ) {
if (!hasPlaylist) {
return const Icon(Icons.music_note, size: 30);
}
return ClipRRect( return ClipRRect(
borderRadius: BorderRadius.circular(8), borderRadius: BorderRadius.circular(8),
clipBehavior: Clip.hardEdge, clipBehavior: Clip.hardEdge,
@ -220,10 +224,13 @@ class MiniPlayer extends StatelessWidget {
width: 48, width: 48,
height: 48, height: 48,
child: Image.network( child: Image.network(
audioController.songList[audioController.currentSongIndex.value].pic, audioController
.songList[
audioController.currentSongIndex.value]
.pic,
fit: BoxFit.cover, fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) => errorBuilder: (context, error, stackTrace) =>
const Icon(Icons.music_note, size: 30), const Icon(Icons.music_note, size: 30),
), ),
), ),
); );
@ -234,38 +241,20 @@ class MiniPlayer extends StatelessWidget {
child: Obx(() { child: Obx(() {
final currentSong = audioController.songList.isEmpty final currentSong = audioController.songList.isEmpty
? null ? null
: audioController.songList[audioController.currentSongIndex.value]; : audioController.songList[
audioController.currentSongIndex.value];
return AnimatedSwitcher( return AnimatedSwitcher(
duration: const Duration(milliseconds: 300), duration: const Duration(milliseconds: 300),
child: currentSong != null child: currentSong != null
? Image.network( ? Image.network(
currentSong.pic, currentSong.pic,
key: ValueKey(currentSong.pic), key: ValueKey(currentSong.pic),
width: 48, width: 48,
height: 48, height: 48,
fit: BoxFit.cover, fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) => errorBuilder: (context, error, stackTrace) =>
const Icon(Icons.music_note, size: 30), const Icon(Icons.music_note, size: 30),
// loadingBuilder: (context, child, loadingProgress) { )
// if (loadingProgress == null) return child;
// return Container(
// color: Colors.grey[100],
// width: 48,
// height: 48,
// child: Center(
// child: CircularProgressIndicator(
// strokeWidth: 2,
// valueColor: const AlwaysStoppedAnimation<Color>(
// Color(0xff429482)),
// value: loadingProgress.expectedTotalBytes != null
// ? loadingProgress.cumulativeBytesLoaded /
// loadingProgress.expectedTotalBytes!
// : null,
// ),
// ),
// );
// },
)
: const Icon(Icons.music_note, size: 30), : const Icon(Icons.music_note, size: 30),
); );
}), }),

@ -1,7 +1,10 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import '../api/api_music_list.dart';
import '../common/download_manager.dart'; import '../common/download_manager.dart';
import '../common_widget/Song_widegt.dart'; import '../common_widget/Song_widegt.dart';
import '../common_widget/app_data.dart';
import '../models/MusicsListBean.dart';
import 'main_tab_view/main_tab_view.dart'; import 'main_tab_view/main_tab_view.dart';
import 'music_view.dart'; import 'music_view.dart';
@ -20,39 +23,29 @@ class _SearchViewState extends State<SearchView> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_loadMockData(); _loadRecommendData();
} }
void _loadMockData() { void _loadRecommendData() async {
// MusicsListBean bean = await GetMusic()
final mockSongs = [ .getMusicList(Authorization: AppData().currentToken, num: 10);
Song( if (bean.code == 200) {
id: 1, setState(() {
title: "夜曲", songs.clear();
artist: "周杰伦", for (var data in bean.data!) {
artistPic: "https://example.com/jay.jpg", songs.add(Song(
pic: "https://example.com/ye.jpg", artistPic: data.coverPath!,
musicurl: "https://example.com/music1.mp3", title: data.name!,
likes: false, artist: data.singerName!,
collection: false, musicurl: data.musicPath!,
), pic: data.coverPath!,
Song( id: data.id!,
id: 2, likes: data.likeOrNot!,
title: "泡沫", collection: data.collectOrNot!,
artist: "邓紫棋", ));
artistPic: "https://example.com/gem.jpg", }
pic: "https://example.com/foam.jpg", });
musicurl: "https://example.com/music2.mp3", }
likes: false,
collection: false,
),
//
];
setState(() {
songs.clear();
songs.addAll(mockSongs);
});
} }
@override @override
@ -76,6 +69,7 @@ class _SearchViewState extends State<SearchView> {
child: _buildSongsList(), child: _buildSongsList(),
), ),
MiniPlayer(), MiniPlayer(),
const SizedBox(height: 10),
], ],
), ),
), ),
@ -102,10 +96,11 @@ class _SearchViewState extends State<SearchView> {
controller: _searchController, controller: _searchController,
decoration: InputDecoration( decoration: InputDecoration(
border: InputBorder.none, border: InputBorder.none,
hintText: '搜索音乐、歌手', hintText: '搜索你想找的音乐',
hintStyle: TextStyle(color: Colors.grey.shade400), hintStyle: TextStyle(color: Colors.grey.shade400),
prefixIcon: Icon(Icons.search, color: Colors.grey.shade400), prefixIcon: Icon(Icons.search, color: Colors.grey.shade400),
contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 15), contentPadding:
const EdgeInsets.symmetric(horizontal: 20, vertical: 15),
), ),
), ),
), ),
@ -141,7 +136,8 @@ class _SearchViewState extends State<SearchView> {
); );
} }
}, },
leading: const Icon(Icons.play_circle_fill, color: Colors.blueGrey, size: 30), leading: const Icon(Icons.play_circle_fill,
color: Colors.blueGrey, size: 30),
title: const Text( title: const Text(
'播放全部', '播放全部',
style: TextStyle( style: TextStyle(
@ -205,7 +201,7 @@ class _SearchViewState extends State<SearchView> {
song.pic, song.pic,
fit: BoxFit.cover, fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) => errorBuilder: (context, error, stackTrace) =>
const Icon(Icons.music_note, size: 30), const Icon(Icons.music_note, size: 30),
), ),
), ),
), ),

Loading…
Cancel
Save