From a29e9dcfffc2e9ccba8d812bb45ef6714b42013e Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Thu, 24 Mar 2022 14:50:33 +0100 Subject: [PATCH] Can filter playlist --- lib/ui/music_player.dart | 51 +++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/lib/ui/music_player.dart b/lib/ui/music_player.dart index f82f2d7..9ae5868 100644 --- a/lib/ui/music_player.dart +++ b/lib/ui/music_player.dart @@ -45,6 +45,9 @@ class _MusicPlayerState extends State { var _showPlaylist = true; + final _filterController = fluent.TextEditingController(); + MusicsList? _filteredList; + MusicEntry get currMusic { if (currMusicPos < 0) currMusicPos = 0; @@ -113,6 +116,20 @@ class _MusicPlayerState extends State { void _updatePosition(Duration d) => _videoPlayerController?.seekTo(d); + void _refreshFilteredList() { + final value = _filterController.value.text.toLowerCase(); + if (value.isEmpty) { + setState(() => _filteredList = null); + return; + } + + setState(() { + _filteredList = widget.musicsList + .where((m) => m.fullName.toLowerCase().contains(value)) + .toList(); + }); + } + @override void dispose() { super.dispose(); @@ -171,6 +188,7 @@ class _MusicPlayerState extends State { _showPlaylist ? SizedBox( width: playlistWidth, + height: constraints.maxHeight, child: _buildPlaylistPane(), ) : Container(), @@ -261,17 +279,28 @@ class _MusicPlayerState extends State { ); Widget _buildPlaylistPane() { - return ListView.builder( - itemBuilder: (c, i) { - final music = widget.musicsList[i]; - return ListTile( - title: Text(music.title), - subtitle: Text(music.artist), - selected: currMusic == music, - onTap: () => _playMusic(music), - ); - }, - itemCount: widget.musicsList.length, + return Column( + children: [ + fluent.TextBox( + controller: _filterController, + placeholder: "Filter list...", + onChanged: (s) => _refreshFilteredList(), + ), + Flexible( + child: ListView.builder( + itemBuilder: (c, i) { + final music = (_filteredList ?? widget.musicsList)[i]; + return ListTile( + title: Text(music.title), + subtitle: Text(music.artist), + selected: currMusic == music, + onTap: () => _playMusic(music), + ); + }, + itemCount: (_filteredList ?? widget.musicsList).length, + ), + ), + ], ); } }