![](/img/trans.png)
[英]How to add list of songs from storage and display it on one of my fragments
[英]how to sort list of songs from external storage alphabetically in flutter
正如我的问题所说..我想对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));
请注意,这会对列表进行适当排序,这意味着sort方法不会返回列表的排序副本,而是直接对现有列表进行排序。
还要检查其他答案 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.