From ef40b4cd069ce9a9c17a9c3f8c64c2283884b5db Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Mon, 4 May 2020 23:05:23 +0200 Subject: [PATCH] Files chooser: Add search --- common/lib/src/selectors/files.dart | 70 +++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 19 deletions(-) diff --git a/common/lib/src/selectors/files.dart b/common/lib/src/selectors/files.dart index a50b0d5..24bd8b9 100644 --- a/common/lib/src/selectors/files.dart +++ b/common/lib/src/selectors/files.dart @@ -37,11 +37,26 @@ class FilesSelector extends StatefulWidget { } class _FilesSelectorState extends State { + final _searchController = TextEditingController(); + MusicusBackendState backend; List history = []; List children = []; Set selection = {}; + String _search = ''; + + @override + void initState() { + super.initState(); + + _searchController.addListener(() { + setState(() { + _search = _searchController.text; + }); + }); + } + @override void didChangeDependencies() { super.didChangeDependencies(); @@ -52,6 +67,11 @@ class _FilesSelectorState extends State { @override Widget build(BuildContext context) { + final title = history.isNotEmpty ? history.last.name : 'base path'; + final filteredChildren = children + .where((d) => d.name.toLowerCase().contains(_search.toLowerCase())) + .toList(); + return WillPopScope( child: Scaffold( appBar: AppBar( @@ -88,21 +108,26 @@ class _FilesSelectorState extends State { icon: const Icon(Icons.arrow_upward), onPressed: history.isNotEmpty ? up : null, ), - title: Text( - history.isNotEmpty ? history.last.name : 'Music library'), + title: TextField( + autofocus: true, + controller: _searchController, + decoration: InputDecoration.collapsed( + hintText: 'Search in $title...'), + ), ), ), Expanded( child: ListView.builder( - itemCount: children.length, + itemCount: filteredChildren.length, itemBuilder: (context, index) { - final document = children[index]; + final document = filteredChildren[index]; if (document.isDirectory) { return ListTile( leading: const Icon(Icons.folder), title: Text(document.name), onTap: () { + _searchController.text = ''; setState(() { history.add(document); }); @@ -110,21 +135,28 @@ class _FilesSelectorState extends State { }, ); } else { - return CheckboxListTile( - controlAffinity: ListTileControlAffinity.trailing, - secondary: const Icon(Icons.insert_drive_file), - title: Text(document.name), - value: selection.contains(document), - onChanged: (selected) { - setState(() { - if (selected) { - selection.add(document); - } else { - selection.remove(document); - } - }); - }, - ); + if (widget.chooseDirectory) { + return ListTile( + leading: const Icon(Icons.insert_drive_file), + title: Text(document.name), + ); + } else { + return CheckboxListTile( + controlAffinity: ListTileControlAffinity.trailing, + secondary: const Icon(Icons.insert_drive_file), + title: Text(document.name), + value: selection.contains(document), + onChanged: (selected) { + setState(() { + if (selected) { + selection.add(document); + } else { + selection.remove(document); + } + }); + }, + ); + } } }, ),