homepage: selection of notes: select all, deselect all, toggle select. Select on longpress

master
EmaMaker 2021-09-19 11:59:03 +02:00
parent d7652f2378
commit d56dcddadc
3 changed files with 102 additions and 53 deletions

View File

@ -24,7 +24,7 @@ class CounterStorage {
final directory = await localDir();
await directory.list().forEach((element) {
files.add(File(element.path));
debugPrint("debugPrinting $element");
// debugPrint("debugPrinting $element");
});
return files;

View File

@ -10,13 +10,21 @@ class MyListTile extends StatefulWidget {
{Key? key,
required this.file,
required this.storage,
required this.function,
required this.selectMode})
required this.updateFunction,
required this.deleteFunction,
required this.toggleFunction,
required this.selectFunction,
required this.selectMode,
required this.isChecked})
: super(key: key);
final File file;
final CounterStorage storage;
final Function function;
final Function updateFunction;
final Function toggleFunction;
final Function deleteFunction;
final Function selectFunction;
final bool selectMode;
bool isChecked;
@override
MyListTileState createState() => MyListTileState();
@ -26,8 +34,6 @@ class MyListTileState extends State<MyListTile> {
final _biggerFont = const TextStyle(fontSize: 18);
late TextEditingController _controller;
bool isChecked = false;
@override
void initState() {
super.initState();
@ -36,18 +42,18 @@ class MyListTileState extends State<MyListTile> {
@override
Widget build(BuildContext context) {
debugPrint("Recreating cards");
print("${widget.isChecked} / ${widget.file}");
return widget.selectMode
? CheckboxListTile(
title: Text(basename(widget.file.path)),
checkColor: Colors.white,
value: isChecked,
onChanged: (bool? value) {
setState(() {
isChecked = widget.selectMode && value!;
});
})
title: Text(basename(widget.file.path)),
checkColor: Colors.white,
value: widget.isChecked,
onChanged: (bool? value) {
setState(() {
widget.toggleFunction(widget.file, widget.isChecked);
});
})
: ListTile(
title: Text(
basename(widget.file.path),
@ -55,7 +61,10 @@ class MyListTileState extends State<MyListTile> {
),
onTap: () =>
widget.storage.openNote(context, basename(widget.file.path)),
onLongPress: () => widget.function,
onLongPress: () {
widget.toggleFunction(widget.file, true);
widget.selectFunction();
},
// onTap: _handleNote,
subtitle: _subtitle(widget.file),
trailing: btnListTileMore(context, widget.file),
@ -108,8 +117,7 @@ class MyListTileState extends State<MyListTile> {
// Return to previous screen
Navigator.pop(context);
_controller.text = "";
setState(() {});
widget.function();
widget.updateFunction();
// Remove the box
},
@ -119,8 +127,7 @@ class MyListTileState extends State<MyListTile> {
Navigator.pop(context);
widget.storage.renameNote(
basename(widget.file.path), _controller.text);
setState(() {});
widget.function();
widget.deleteFunction(widget.file);
},
child: Text('Yes')),
],
@ -145,16 +152,15 @@ class MyListTileState extends State<MyListTile> {
Navigator.pop(context);
_controller.text = "";
setState(() {});
widget.function();
widget.updateFunction();
},
child: Text('Cancel')),
TextButton(
onPressed: () {
deleteFile();
Navigator.pop(context);
//Return to previous screen
setState(() {});
widget.function();
widget.deleteFunction(widget.file);
},
child: Text('Ok')),
],
@ -185,12 +191,4 @@ class MyListTileState extends State<MyListTile> {
}
return Colors.red;
}
void deleteFileFromSelect() {
if (isChecked) deleteFile();
}
void deleteFile() {
widget.file.deleteSync();
}
}

View File

@ -15,10 +15,8 @@ class MyHomePage extends StatefulWidget {
}
class _MyHomePageState extends State<MyHomePage> {
final List<GlobalKey<MyListTileState>> keys = [];
List<File> _allFilesNames = [];
late Iterable<MyListTile> tiles;
List<String> _selected = [];
@override
void initState() {
@ -82,20 +80,21 @@ class _MyHomePageState extends State<MyHomePage> {
bool selectMode = false;
Widget _buildList(BuildContext context) {
keys.clear();
tiles = _allFilesNames.map(
(File f) {
GlobalKey<MyListTileState> key = GlobalKey();
keys.add(key);
print(_selected);
final tiles = _allFilesNames.map(
(
File f,
) {
return new MyListTile(
file: f,
selectMode: selectMode,
storage: widget.storage,
function: updateChildren,
key: key,
);
file: f,
selectMode: selectMode,
storage: widget.storage,
selectFunction: enterSelectMode,
updateFunction: updateChildren,
toggleFunction: toggleFile,
deleteFunction: _deleteFile,
isChecked: _selected.contains(f.path));
},
);
final divided = tiles.isNotEmpty
@ -115,12 +114,32 @@ class _MyHomePageState extends State<MyHomePage> {
void exitSelectMode() {
setState(() {
_selected.clear();
selectMode = false;
});
}
void updateChildren() {
enterSelectMode();
setState(() {});
}
void toggleFile(File f, bool b) {
if (_selected.contains(f.path))
_selected.remove(f.path);
else
_selected.add(f.path);
setState(() {
});
}
void _deleteFile(File f) {
setState(() {
_selected.clear();
try {
f.deleteSync();
} catch (e) {}
});
exitSelectMode();
}
/* DROP DOWN MENUS*/
@ -130,19 +149,47 @@ class _MyHomePageState extends State<MyHomePage> {
onChanged: (String? newValue) {
switch (newValue) {
case "Delete":
keys.forEach((element) {
element.currentState?.deleteFileFromSelect();
_selected.forEach((element) {
File(element).deleteSync();
});
_selected.clear();
exitSelectMode();
break;
case "Cancel":
exitSelectMode();
break;
case "Select All":
_selected.clear();
_allFilesNames.forEach((element) {
_selected.add(element.path);
});
break;
case "Deselect All":
_selected.clear();
break;
case "Toggle Selected":
List<String> newSelected = [];
_allFilesNames.forEach((element) {
if (!(_selected.contains(element.path))) newSelected.add(element.path);
});
print("new $newSelected");
_selected = newSelected;
// keys.forEach((element) {element.currentState?.isChecked = !(element.currentState?.isChecked) ;});
break;
}
setState(() {});
},
underline: Container(color: Colors.transparent),
items: <String>['Delete', 'Cancel']
.map<DropdownMenuItem<String>>((String value) {
items: <String>[
"Delete",
"Select All",
"Deselect All",
"Toggle Selected",
"Cancel",
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
@ -157,11 +204,15 @@ class _MyHomePageState extends State<MyHomePage> {
onChanged: (String? newValue) {
switch (newValue) {
case "Select":
if(tiles.isNotEmpty) enterSelectMode();
if (_allFilesNames.isNotEmpty) enterSelectMode();
break;
case "Settings":
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => Settings(storage: widget.storage,)));
context,
MaterialPageRoute(
builder: (context) => Settings(
storage: widget.storage,
)));
}
setState(() {});
},