如何在颤动中按字母顺序对来自外部存储的歌曲列表进行排序 [英] how to sort list of songs from external storage alphabetically in flutter

查看:49
本文介绍了如何在颤动中按字母顺序对来自外部存储的歌曲列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As my Problem Says..i want to sort my listView items that are basically songs read by external storage..I want to sort them alphabetically..this is my code for 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中轻松排序.

Lists can be sorted very easily in Dart.

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

To sort alphabetically by song title add this line to the beginning of your build method (or wherever else you want to sort the songs list):

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

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

Note that this sorts the list in place, meaning the sort method does not return a sorted copy of the list, but sorts the existing list directly.

也请查看其他 answer .

这篇关于如何在颤动中按字母顺序对来自外部存储的歌曲列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆