Issue
I have a listview which has Slideable I want a longPress gesture to activate a Slideable Action. I can get a longpress message, but I do not know what to do to cause the sliding action to work. This is a custom Tile Widget I need to edit.
This is what it looks like when I swipe.
I want the same thing to happen when I release the longpress
Code is below:
class BookmarkListTile extends StatelessWidget {
// final BookmarkPageViewModel bookmarkViewmodel;
// final int index;
const BookmarkListTile(
{Key? key, required this.bookmark, this.onTap, this.onDelete})
: super(key: key);
final Bookmark bookmark;
final Function(Bookmark bookmark)? onDelete;
final Function(Bookmark bookmark)? onTap;
@override
Widget build(BuildContext context) {
return GestureDetector(
behavior: HitTestBehavior.translucent,
onLongPressUp: () {
print("longpress");
// add code here to cause the Slidable Action to happen
},
child: Slidable(
actionPane: const SlidableDrawerActionPane(),
secondaryActions: [
IconSlideAction(
icon: Icons.delete,
color: Colors.red,
onTap: () {
if (onDelete != null) onDelete!(bookmark);
},
)
],
child: ListTile(
onTap: () {
if (onTap != null) onTap!(bookmark);
},
title: Text(bookmark.note),
subtitle: Text(PaliScript.getScriptOf(
language:
context.read<ScriptLanguageProvider>().currentLanguage,
romanText: bookmark.bookName!)),
trailing: SizedBox(
width: 100,
child: Row(
children: [
Text('${AppLocalizations.of(context)!.page} -'),
Expanded(
child: Text(
'${bookmark.pageNumber}',
textAlign: TextAlign.end,
)),
],
),
),
),
));
}
}
Solution
Try maybe this:
Widget build(BuildContext context) {
return Slidable(
actionPane: const SlidableDrawerActionPane(),
secondaryActions: [
IconSlideAction(
icon: Icons.delete,
color: Colors.red,
onTap: () {
if (onDelete != null) onDelete!(bookmark);
},
)
],
child: Builder(builder: (context) =>
GestureDetector(
behavior: HitTestBehavior.translucent,
onLongPress: () {
openSlidable(context);
},
child: ListTile(
onTap: () {
if (onTap != null) onTap!(bookmark);
},
title: Text(bookmark.note),
subtitle: Text(PaliScript.getScriptOf(
language:
context.read<ScriptLanguageProvider>().currentLanguage,
romanText: bookmark.bookName!)),
trailing: SizedBox(
width: 100,
child: Row(
children: [
Text('${AppLocalizations.of(context)!.page} -'),
Expanded(
child: Text(
'${bookmark.pageNumber}',
textAlign: TextAlign.end,
)),
],
),
),
))));
}
void openSlidable(BuildContext context) {
final slidable = Slidable.of(context);
final isClosed = slidable.renderingMode == SlidableRenderingMode.none;
if (isClosed) {
Future.delayed(Duration.zero, () {
if (slidable.mounted) {
slidable.open(actionType: SlideActionType.secondary);
}
});
}
}
This will automatically open your slidable if it was closed. Child of Slidable
should be wrapped in Builder
in order to work.
Answered By - kmtz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.