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 Text(
"大家都在搜《背对背拥抱》",
"搜索你想找的音乐",
style: TextStyle(
color: Color(0xffA5A5A5),
fontSize: 13,

@ -111,12 +111,12 @@ class MiniPlayer extends StatelessWidget {
maxWidth: 42,
maxHeight: 42,
),
onPressed: null, //
onPressed: null, //
icon: Image.asset(
"assets/img/music_pause.png",
width: 25,
height: 25,
color: Colors.grey, // 使
color: Colors.grey, // 使
),
);
}
@ -142,12 +142,12 @@ class MiniPlayer extends StatelessWidget {
),
onPressed: audioController.playOrPause,
icon: Obx(() => Image.asset(
audioController.isPlaying.value
? "assets/img/music_play.png"
: "assets/img/music_pause.png",
width: 25,
height: 25,
)),
audioController.isPlaying.value
? "assets/img/music_play.png"
: "assets/img/music_pause.png",
width: 25,
height: 25,
)),
);
}),
),
@ -158,21 +158,22 @@ class MiniPlayer extends StatelessWidget {
Widget build(BuildContext context) {
return Obx(() {
final bool hasPlaylist = audioController.songList.isNotEmpty;
return GestureDetector(
onHorizontalDragEnd: hasPlaylist ? (DragEndDetails details) {
if (audioController.songList.isEmpty) return;
final velocity = details.velocity.pixelsPerSecond.dx;
const threshold = 300.0;
onHorizontalDragEnd: hasPlaylist
? (DragEndDetails details) {
if (audioController.songList.isEmpty) return;
final velocity = details.velocity.pixelsPerSecond.dx;
const threshold = 300.0;
if (velocity > threshold) {
audioController.playPrevious();
HapticFeedback.mediumImpact();
} else if (velocity < -threshold) {
audioController.playNext();
HapticFeedback.mediumImpact();
}
} : null,
if (velocity > threshold) {
audioController.playPrevious();
HapticFeedback.mediumImpact();
} else if (velocity < -threshold) {
audioController.playNext();
HapticFeedback.mediumImpact();
}
}
: null,
child: Container(
height: 64, // 使
decoration: BoxDecoration(
@ -207,12 +208,15 @@ class MiniPlayer extends StatelessWidget {
child: Hero(
tag: 'album_cover',
flightShuttleBuilder: (
BuildContext flightContext,
Animation<double> animation,
HeroFlightDirection flightDirection,
BuildContext fromHeroContext,
BuildContext toHeroContext,
) {
BuildContext flightContext,
Animation<double> animation,
HeroFlightDirection flightDirection,
BuildContext fromHeroContext,
BuildContext toHeroContext,
) {
if (!hasPlaylist) {
return const Icon(Icons.music_note, size: 30);
}
return ClipRRect(
borderRadius: BorderRadius.circular(8),
clipBehavior: Clip.hardEdge,
@ -220,10 +224,13 @@ class MiniPlayer extends StatelessWidget {
width: 48,
height: 48,
child: Image.network(
audioController.songList[audioController.currentSongIndex.value].pic,
audioController
.songList[
audioController.currentSongIndex.value]
.pic,
fit: BoxFit.cover,
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(() {
final currentSong = audioController.songList.isEmpty
? null
: audioController.songList[audioController.currentSongIndex.value];
: audioController.songList[
audioController.currentSongIndex.value];
return AnimatedSwitcher(
duration: const Duration(milliseconds: 300),
child: currentSong != null
? Image.network(
currentSong.pic,
key: ValueKey(currentSong.pic),
width: 48,
height: 48,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) =>
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,
// ),
// ),
// );
// },
)
currentSong.pic,
key: ValueKey(currentSong.pic),
width: 48,
height: 48,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) =>
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:get/get.dart';
import '../api/api_music_list.dart';
import '../common/download_manager.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 'music_view.dart';
@ -20,39 +23,29 @@ class _SearchViewState extends State<SearchView> {
@override
void initState() {
super.initState();
_loadMockData();
_loadRecommendData();
}
void _loadMockData() {
//
final mockSongs = [
Song(
id: 1,
title: "夜曲",
artist: "周杰伦",
artistPic: "https://example.com/jay.jpg",
pic: "https://example.com/ye.jpg",
musicurl: "https://example.com/music1.mp3",
likes: false,
collection: false,
),
Song(
id: 2,
title: "泡沫",
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);
});
void _loadRecommendData() async {
MusicsListBean bean = await GetMusic()
.getMusicList(Authorization: AppData().currentToken, num: 10);
if (bean.code == 200) {
setState(() {
songs.clear();
for (var data in bean.data!) {
songs.add(Song(
artistPic: data.coverPath!,
title: data.name!,
artist: data.singerName!,
musicurl: data.musicPath!,
pic: data.coverPath!,
id: data.id!,
likes: data.likeOrNot!,
collection: data.collectOrNot!,
));
}
});
}
}
@override
@ -76,6 +69,7 @@ class _SearchViewState extends State<SearchView> {
child: _buildSongsList(),
),
MiniPlayer(),
const SizedBox(height: 10),
],
),
),
@ -102,10 +96,11 @@ class _SearchViewState extends State<SearchView> {
controller: _searchController,
decoration: InputDecoration(
border: InputBorder.none,
hintText: '搜索音乐、歌手',
hintText: '搜索你想找的音乐',
hintStyle: TextStyle(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(
'播放全部',
style: TextStyle(
@ -205,7 +201,7 @@ class _SearchViewState extends State<SearchView> {
song.pic,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) =>
const Icon(Icons.music_note, size: 30),
const Icon(Icons.music_note, size: 30),
),
),
),
@ -272,4 +268,4 @@ class _SearchViewState extends State<SearchView> {
downloadManager.updateSongInfo(songs[index].id, isCollected, isLiked);
});
}
}
}

Loading…
Cancel
Save