Added ascending and descending sort option in files
This commit is contained in:
parent
9a7813a9fc
commit
1f9d6ca6bf
@ -19,8 +19,9 @@ class Files extends StatefulWidget {
|
|||||||
class BetterSortOption {
|
class BetterSortOption {
|
||||||
String displayName;
|
String displayName;
|
||||||
int Function(CacheableFile, CacheableFile) compare;
|
int Function(CacheableFile, CacheableFile) compare;
|
||||||
|
IconData icon;
|
||||||
|
|
||||||
BetterSortOption({required this.displayName, required this.compare});
|
BetterSortOption({required this.displayName, required this.icon, required this.compare});
|
||||||
}
|
}
|
||||||
|
|
||||||
enum SortOption {
|
enum SortOption {
|
||||||
@ -33,14 +34,17 @@ class SortOptions {
|
|||||||
static Map<SortOption, BetterSortOption> options = {
|
static Map<SortOption, BetterSortOption> options = {
|
||||||
SortOption.name: BetterSortOption(
|
SortOption.name: BetterSortOption(
|
||||||
displayName: "Name",
|
displayName: "Name",
|
||||||
|
icon: Icons.sort_by_alpha,
|
||||||
compare: (CacheableFile a, CacheableFile b) => a.name.compareTo(b.name)
|
compare: (CacheableFile a, CacheableFile b) => a.name.compareTo(b.name)
|
||||||
),
|
),
|
||||||
SortOption.date: BetterSortOption(
|
SortOption.date: BetterSortOption(
|
||||||
displayName: "Datum",
|
displayName: "Datum",
|
||||||
|
icon: Icons.more_time_outlined,
|
||||||
compare: (CacheableFile a, CacheableFile b) => a.modifiedAt!.compareTo(b.modifiedAt!)
|
compare: (CacheableFile a, CacheableFile b) => a.modifiedAt!.compareTo(b.modifiedAt!)
|
||||||
),
|
),
|
||||||
SortOption.size: BetterSortOption(
|
SortOption.size: BetterSortOption(
|
||||||
displayName: "Größe",
|
displayName: "Größe",
|
||||||
|
icon: Icons.monitor_weight,
|
||||||
compare: (CacheableFile a, CacheableFile b) {
|
compare: (CacheableFile a, CacheableFile b) {
|
||||||
if(a.isDirectory || b.isDirectory) return a.isDirectory ? 1 : 0;
|
if(a.isDirectory || b.isDirectory) return a.isDirectory ? 1 : 0;
|
||||||
if(a.size == null) return 0;
|
if(a.size == null) return 0;
|
||||||
@ -50,10 +54,6 @@ class SortOptions {
|
|||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
static String getDisplayName(SortOption option) {
|
|
||||||
return options[option]?.displayName ?? "";
|
|
||||||
}
|
|
||||||
|
|
||||||
static BetterSortOption getOption(SortOption option) {
|
static BetterSortOption getOption(SortOption option) {
|
||||||
return options[option]!;
|
return options[option]!;
|
||||||
}
|
}
|
||||||
@ -64,6 +64,7 @@ class _FilesState extends State<Files> {
|
|||||||
ListFilesResponse? data;
|
ListFilesResponse? data;
|
||||||
|
|
||||||
SortOption currentSort = SortOption.name;
|
SortOption currentSort = SortOption.name;
|
||||||
|
bool currentSortDirection = true;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@ -81,8 +82,8 @@ class _FilesState extends State<Files> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
if(data == null) return SizedBox.shrink();
|
List<CacheableFile> files = (data?.files.toList() ?? List.empty())..sort(SortOptions.getOption(currentSort).compare);
|
||||||
List<CacheableFile> files = data!.files.toList()..sort(SortOptions.getOption(currentSort).compare);
|
if(currentSortDirection) files = files.reversed.toList();
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
@ -92,10 +93,41 @@ class _FilesState extends State<Files> {
|
|||||||
icon: const Icon(Icons.search),
|
icon: const Icon(Icons.search),
|
||||||
onPressed: () => {},
|
onPressed: () => {},
|
||||||
),
|
),
|
||||||
|
PopupMenuButton<bool>(
|
||||||
|
icon: Icon(currentSortDirection ? Icons.text_rotate_up : Icons.text_rotation_down),
|
||||||
|
itemBuilder: (context) {
|
||||||
|
return [true, false].map((e) => PopupMenuItem<bool>(
|
||||||
|
value: e,
|
||||||
|
enabled: e != currentSortDirection,
|
||||||
|
child: Row(
|
||||||
|
children: [
|
||||||
|
Icon(e ? Icons.text_rotate_up : Icons.text_rotation_down, color: Colors.black),
|
||||||
|
const SizedBox(width: 15),
|
||||||
|
Text(e ? "Aufsteigend" : "Absteigend")
|
||||||
|
],
|
||||||
|
)
|
||||||
|
)).toList();
|
||||||
|
},
|
||||||
|
onSelected: (e) {
|
||||||
|
setState(() {
|
||||||
|
currentSortDirection = e;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
),
|
||||||
PopupMenuButton<SortOption>(
|
PopupMenuButton<SortOption>(
|
||||||
icon: const Icon(Icons.sort),
|
icon: const Icon(Icons.sort),
|
||||||
itemBuilder: (context) {
|
itemBuilder: (context) {
|
||||||
return SortOptions.options.keys.map((key) => PopupMenuItem<SortOption>(value: key, child: Text(SortOptions.getDisplayName(key)))).toList();
|
return SortOptions.options.keys.map((key) => PopupMenuItem<SortOption>(
|
||||||
|
value: key,
|
||||||
|
enabled: key != currentSort,
|
||||||
|
child: Row(
|
||||||
|
children: [
|
||||||
|
Icon(SortOptions.getOption(key).icon, color: Colors.black),
|
||||||
|
const SizedBox(width: 15),
|
||||||
|
Text(SortOptions.getOption(key).displayName)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
)).toList();
|
||||||
},
|
},
|
||||||
onSelected: (e) {
|
onSelected: (e) {
|
||||||
setState(() {
|
setState(() {
|
||||||
@ -106,31 +138,12 @@ class _FilesState extends State<Files> {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
body: data == null ? const Center(child: CircularProgressIndicator()) : data!.files.isEmpty ? const ErrorView(icon: Icons.folder_off_rounded, text: "Der Ordner ist leer") : ListView.builder(
|
body: data == null ? const Center(child: CircularProgressIndicator()) : data!.files.isEmpty ? const ErrorView(icon: Icons.folder_off_rounded, text: "Der Ordner ist leer") : ListView.builder(
|
||||||
itemCount: files.length,
|
itemCount: files.length,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
CacheableFile file = files.toList().skip(index).first;
|
CacheableFile file = files.toList().skip(index).first;
|
||||||
return FileElement(file, widget.path);
|
return FileElement(file, widget.path);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
// Consumer<FilesProps>(
|
|
||||||
// builder: (context, value, child) {
|
|
||||||
//
|
|
||||||
// if(value.listFilesResponse.files.isEmpty) {
|
|
||||||
// return const ErrorView(text: "Der Ordner ist leer", icon: Icons.folder_off_outlined);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// List<CacheableFile> files = value.listFilesResponse.files.toList();
|
|
||||||
// files.sort((a, b) => a.isDirectory ? -1 : 1);
|
|
||||||
//
|
|
||||||
// return ListView.builder(
|
|
||||||
// itemCount: files.length,
|
|
||||||
// itemBuilder: (context, index) {
|
|
||||||
// CacheableFile file = files.skip(index).first;
|
|
||||||
// return FileElement(file, props);
|
|
||||||
// },
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
// ),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user