From 090c4501b13fbc31d8866d59767473942ff53a95 Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Mon, 6 Apr 2020 11:55:19 +0200 Subject: [PATCH] Tracks editor: Make tracks reorderable --- lib/editors/tracks.dart | 95 +++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 42 deletions(-) diff --git a/lib/editors/tracks.dart b/lib/editors/tracks.dart index b8263d7..f2aea99 100644 --- a/lib/editors/tracks.dart +++ b/lib/editors/tracks.dart @@ -37,54 +37,65 @@ class _TracksEditorState extends State { ), ], ), - body: ListView( - children: [ - recordingId != null - ? RecordingTile( - recordingId: recordingId, - onTap: selectRecording, - ) - : ListTile( - title: Text('Select recording'), - onTap: selectRecording, - ), - ListTile( - title: Text('Files'), - trailing: IconButton( - icon: const Icon(Icons.add), - onPressed: () async { - final Set paths = await Navigator.push( - context, - MaterialPageRoute( - builder: (context) => FilesSelector( - baseDirectory: backend.musicLibraryPath, - ), + body: ReorderableListView( + header: Column( + children: [ + recordingId != null + ? RecordingTile( + recordingId: recordingId, + onTap: selectRecording, + ) + : ListTile( + title: Text('Select recording'), + onTap: selectRecording, ), - ); - - if (paths != null) { - setState(() { - for (final path in paths) { - tracks.add(TrackModel(path)); - } - }); - } - }, - ), - ), - for (final track in tracks) ListTile( - title: Text(track.path), + title: Text('Files'), trailing: IconButton( - icon: const Icon(Icons.delete), - onPressed: () { - setState(() { - tracks.remove(track); - }); + icon: const Icon(Icons.add), + onPressed: () async { + final Set paths = await Navigator.push( + context, + MaterialPageRoute( + builder: (context) => FilesSelector( + baseDirectory: backend.musicLibraryPath, + ), + ), + ); + + if (paths != null) { + setState(() { + for (final path in paths) { + tracks.add(TrackModel(path)); + } + }); + } }, ), ), - ], + ], + ), + children: tracks + .map((t) => ListTile( + key: Key(t.hashCode.toString()), + title: Text(t.path), + trailing: IconButton( + icon: const Icon(Icons.delete), + onPressed: () { + setState(() { + tracks.remove(t); + }); + }, + ), + )) + .toList(), + onReorder: (i1, i2) { + setState(() { + final track = tracks.removeAt(i1); + final newIndex = i2 > i1 ? i2 - 1 : i2; + tracks.insert(newIndex, track); + }); + }, ), ); }