gpt4 book ai didi

android - 如何在 flutter 中按字母顺序对外部存储中的歌曲列表进行排序

转载 作者:IT王子 更新时间:2023-10-29 06:54:09 26 4
gpt4 key购买 nike

正如我的问题所述..我想对我的 listView 项目进行排序,这些项目基本上是外部存储读取的歌曲..我想按字母顺序对它们进行排序..这是我的 listView 代码

final List<MaterialColor> _colors = Colors.primaries;
@override
Widget build(BuildContext context) {
final rootIW = MPInheritedWidget.of(context);
SongData songData = rootIW.songData;
return new ListView.builder(
itemCount: songData.songs.length,
itemBuilder: (context, int index) {
var s = songData.songs[index];
final MaterialColor color = _colors[index % _colors.length];
var artFile =
s.albumArt == null ? null : new File.fromUri(Uri.parse(s.albumArt));

return new ListTile(
dense: false,
leading: new Hero(
child: avatar(artFile, s.title, color),
tag: s.title,
),
title: new Text(s.title),
subtitle: new Text(
"By ${s.artist}",
style: Theme.of(context).textTheme.caption,
),

最佳答案

在 Dart 中可以很容易地对列表进行排序。

要按歌曲名称的字母顺序排序,请将此行添加到构建方法的开头(或您想要对歌曲列表进行排序的任何其他位置):

Widget build(BuildContext context) {
songData.songs.sort((a, b) => a.title.compareTo(b.title));

请注意,这是对列表进行就地排序,这意味着排序方法不会返回列表的排序副本,而是直接对现有列表进行排序。

另请查看其他 answer .

关于android - 如何在 flutter 中按字母顺序对外部存储中的歌曲列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54065146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com