Skip to content

Commit

Permalink
tags long press actions
Browse files Browse the repository at this point in the history
  • Loading branch information
judemont committed Apr 28, 2024
1 parent 99d1afb commit d1b8566
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 18 deletions.
1 change: 0 additions & 1 deletion lib/widgets/newTagDialog.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:reciper/models/tag.dart';
import 'package:reciper/utilities/database.dart';
Expand Down
43 changes: 43 additions & 0 deletions lib/widgets/tagActionsDialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'package:flutter/material.dart';
import 'package:reciper/models/tag.dart';
import 'package:reciper/utilities/database.dart';

class TagActionDialog extends StatelessWidget {
final Tag tag;
const TagActionDialog({super.key, required this.tag});

@override
Widget build(BuildContext context) {
TextEditingController newTagInputController =
TextEditingController(text: tag.name);

return AlertDialog(
title: const Text("Tag options"),
content: TextField(
controller: newTagInputController,
decoration: const InputDecoration(hintText: "Rename Tag"),
),
actions: <Widget>[
TextButton(
child: const Text("cancel"),
onPressed: () {
Navigator.pop(context);
}),
TextButton(
child: const Text("delete tag"),
onPressed: () {
DatabaseService.removeTag(tag.id!);
Navigator.pop(context);
}),
TextButton(
child: const Text("save"),
onPressed: () {
tag.name = newTagInputController.text;
DatabaseService.updateTag(tag);

Navigator.pop(context);
},
),
]);
}
}
47 changes: 30 additions & 17 deletions lib/widgets/tagsSelector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:reciper/models/tag.dart';
import 'package:reciper/utilities/database.dart';
import 'package:reciper/widgets/newTagDialog.dart';
import 'package:reciper/widgets/tagActionsDialog.dart';

class TagsSelector extends StatefulWidget {
final List<Tag> tags;
Expand Down Expand Up @@ -74,20 +75,29 @@ class _TagsSelectorState extends State<TagsSelector> {
shrinkWrap: true,
itemCount: widget.tags.length,
itemBuilder: (BuildContext context, int index) {
return CheckboxListTile(
value: widget.selectedTagsId.contains(widget.tags[index].id),
title: Text(widget.tags[index].name ?? ""),
onChanged: (value) {
setState(() {
if (value ?? false) {
widget.selectedTagsId.add(widget.tags[index].id!);
} else {
widget.selectedTagsId.remove(widget.tags[index].id);
}
widget.onTagsSelectionUpdate(widget.selectedTagsId);
widget.onTagsUpdate();
});
},
return GestureDetector(
onLongPress: () => showDialog(
context: context,
builder: (context) =>
TagActionDialog(tag: widget.tags[index])).then((value) {
widget.onTagsUpdate();
widget.selectedTagsId.clear();
}),
child: CheckboxListTile(
value: widget.selectedTagsId.contains(widget.tags[index].id),
title: Text(widget.tags[index].name ?? ""),
onChanged: (value) {
setState(() {
if (value ?? false) {
widget.selectedTagsId.add(widget.tags[index].id!);
} else {
widget.selectedTagsId.remove(widget.tags[index].id);
}
widget.onTagsSelectionUpdate(widget.selectedTagsId);
widget.onTagsUpdate();
});
},
),
);
},
),
Expand All @@ -96,9 +106,12 @@ class _TagsSelectorState extends State<TagsSelector> {
title: const Text('New Tag'),
onTap: () {
showDialog(
context: context,
builder: (context) => const NewTagDialog())
.then((value) => widget.onTagsUpdate());
context: context,
builder: (context) => const NewTagDialog()).then((value) {
widget.onTagsUpdate().then((value) {
widget.onTagsSelectionUpdate([]);
});
});
},
),
],
Expand Down

0 comments on commit d1b8566

Please sign in to comment.