Skip to content

Commit

Permalink
添加了搜索功能
Browse files Browse the repository at this point in the history
  • Loading branch information
MCredbear committed Feb 19, 2024
1 parent 0b19a49 commit e1571bd
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 59 deletions.
242 changes: 202 additions & 40 deletions lib/file_manager_page/file_manager_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@ class FileManagerPage extends StatefulWidget {
}

class FileManagerPageState extends State<FileManagerPage> {
FileManagerStore fileManagerStore = FileManagerStore();

List<String> pathsQueue = [];
final List<String> _pathsQueue = [];

@override
void initState() {
if (Platform.isAndroid) {
getExternalStorageDirectories().then((value) {
pathsQueue
_pathsQueue
.add('${value!.first.parent.parent.parent.parent.path}/Music');
fileManagerStore.readDir(pathsQueue.last);
fileManagerStore.readDir(_pathsQueue.last);
});
} else {
getApplicationDocumentsDirectory().then((value) {
pathsQueue.add(value.parent.path);
fileManagerStore.readDir(pathsQueue.last);
_pathsQueue.add(value.parent.path);
fileManagerStore.readDir(_pathsQueue.last);
});
}
super.initState();
Expand All @@ -42,20 +40,55 @@ class FileManagerPageState extends State<FileManagerPage> {
return PopScope(
canPop: false,
onPopInvoked: (value) {
if (pathsQueue.length > 1) {
pathsQueue.removeLast();
fileManagerStore.readDir(pathsQueue.last);
if (_pathsQueue.length > 1) {
_pathsQueue.removeLast();
fileManagerStore.readDir(_pathsQueue.last);
} else {
exit(0);
}
},
child: Scaffold(
appBar: AppBar(
title: const Text("Music tools Flutter"),
actions: [
PopupMenuButton(
floatingActionButton: Column(mainAxisSize: MainAxisSize.min, children: [
FloatingActionButton(
onPressed: () {
if (_pathsQueue.length > 1) {
_pathsQueue.removeLast();
fileManagerStore.readDir(_pathsQueue.last);
}
},
child: const Icon(Icons.keyboard_arrow_left)),
const SizedBox(
height: 5,
),
FloatingActionButton(
onPressed: (() {
_pathsQueue.add(dirname(_pathsQueue.last));
fileManagerStore.readDir(_pathsQueue.last);
}),
child: const Icon(Icons.keyboard_arrow_up)),
const SizedBox(
height: 5,
),
FloatingActionButton(
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return SearchDialog(
pathsQueue: _pathsQueue,
);
});
},
child: const Icon(Icons.search)),
const SizedBox(
height: 5,
),
FloatingActionButton(
onPressed: () {},
child: PopupMenuButton(
tooltip: '',
icon: const Icon(Icons.sort),
splashRadius: 24,
splashRadius: 28,
itemBuilder: (BuildContext context) => [
PopupMenuItem(
padding: EdgeInsets.zero,
Expand Down Expand Up @@ -101,29 +134,18 @@ class FileManagerPageState extends State<FileManagerPage> {
);
})),
]),
IconButton(
splashRadius: 24,
onPressed: () {
if (pathsQueue.length > 1) {
pathsQueue.removeLast();
fileManagerStore.readDir(pathsQueue.last);
}
},
icon: const Icon(Icons.keyboard_arrow_left_sharp)),
IconButton(
splashRadius: 24,
onPressed: (() {
fileManagerStore.readDir(pathsQueue.last);
}),
icon: const Icon(Icons.refresh)),
IconButton(
splashRadius: 24,
onPressed: (() {
pathsQueue.add(dirname(pathsQueue.last));
fileManagerStore.readDir(pathsQueue.last);
}),
icon: const Icon(Icons.arrow_upward))
],
),
const SizedBox(
height: 5,
),
FloatingActionButton(
onPressed: () {
fileManagerStore.readDir(_pathsQueue.last);
},
child: const Icon(Icons.refresh))
]),
appBar: AppBar(
title: const Text("Music tools Flutter"),
),
body: Center(
child: Observer(
Expand All @@ -135,10 +157,10 @@ class FileManagerPageState extends State<FileManagerPage> {
onTap: (() {
if (fileManagerStore.elements.elementAt(index)
is Directory) {
pathsQueue.add(fileManagerStore.elements
_pathsQueue.add(fileManagerStore.elements
.elementAt(index)
.path);
fileManagerStore.readDir(pathsQueue.last);
fileManagerStore.readDir(_pathsQueue.last);
} else {
Navigator.push(
context,
Expand Down Expand Up @@ -177,3 +199,143 @@ class FileManagerPageState extends State<FileManagerPage> {
);
}
}

class SearchDialog extends StatefulWidget {
const SearchDialog({super.key, required List<String> pathsQueue})
: _pathsQueue = pathsQueue;

final List<String> _pathsQueue;

@override
State<SearchDialog> createState() => _SearchDialogState();
}

class _SearchDialogState extends State<SearchDialog> {
final _searchController = TextEditingController();

List<FileSystemEntity> _filtedElements = [];

@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('搜索', textScaler: TextScaler.linear(2)),
content: SizedBox(
width: 400,
height: 800,
child: Column(
children: [
TextField(
controller: _searchController,
onChanged: (value) {
List<FileSystemEntity> filtedElements = [];
for (final element in fileManagerStore.elements) {
if (basename(element.path).contains(
RegExp(value, caseSensitive: false))) {
filtedElements.add(element);
}
}
setState(() {
_filtedElements = filtedElements;
});
},
),
Expanded(
child: ListView.builder(
itemCount: _filtedElements.length,
itemBuilder: (context, index) => SizedBox(
height: 60,
child: ListTile(
onTap: (() {
if (_filtedElements.elementAt(index)
is Directory) {
Navigator.pop(context);
widget._pathsQueue
.add(_filtedElements.elementAt(index).path);
fileManagerStore
.readDir(widget._pathsQueue.last);
} else {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => EditorPage(
_filtedElements
.elementAt(index)
.path)));
}
}),
leading:
(_filtedElements.elementAt(index) is Directory)
? const Icon(
Icons.folder,
size: 30,
)
: const Icon(
Icons.audio_file,
size: 30,
),
title: Text(
basename(_filtedElements.elementAt(index).path),
overflow: TextOverflow.ellipsis,
),
subtitle: Text(_filtedElements
.elementAt(index)
.statSync()
.modified
.toString()),
),
)),
)
],
),
));
}
}

class FileTile extends StatelessWidget {
const FileTile(
{super.key, required List<String> pathsQueue, required int index})
: _pathsQueue = pathsQueue,
_index = index;

final List<String> _pathsQueue;
final int _index;

@override
Widget build(BuildContext context) {
return SizedBox(
height: 60,
child: ListTile(
onTap: (() {
if (fileManagerStore.elements.elementAt(_index) is Directory) {
_pathsQueue.add(fileManagerStore.elements.elementAt(_index).path);
fileManagerStore.readDir(_pathsQueue.last);
} else {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => EditorPage(
fileManagerStore.elements.elementAt(_index).path)));
}
}),
leading: (fileManagerStore.elements.elementAt(_index) is Directory)
? const Icon(
Icons.folder,
size: 30,
)
: const Icon(
Icons.audio_file,
size: 30,
),
title: Text(
basename(fileManagerStore.elements.elementAt(_index).path),
overflow: TextOverflow.ellipsis,
),
subtitle: Text(fileManagerStore.elements
.elementAt(_index)
.statSync()
.modified
.toString()),
),
);
}
}
11 changes: 11 additions & 0 deletions lib/file_manager_page/file_manager_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ abstract class FileManagerStoreBase with Store {
}
}

// @action
// void search(String keyword){
// ObservableList<FileSystemEntity> newElements = ObservableList();
// for (final element in elements){
// if (basename(element.path).contains(RegExp(keyword,caseSensitive: false))) newElements.add(element);
// }
// elements = newElements;
// }

void sortByName() {
ObservableList<FileSystemEntity> dirs = ObservableList();
ObservableList<FileSystemEntity> files = ObservableList();
Expand Down Expand Up @@ -123,3 +132,5 @@ abstract class FileManagerStoreBase with Store {
elements.addAll(files);
}
}

final FileManagerStore fileManagerStore = FileManagerStore();
15 changes: 9 additions & 6 deletions lib/file_manager_page/file_manager_store.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e1571bd

Please sign in to comment.