From fb278e448796cad1f32b192b76b61df8fe92443d Mon Sep 17 00:00:00 2001 From: Brandon Presley Date: Fri, 10 Nov 2023 18:50:56 +1300 Subject: [PATCH] Add long press to delete from homepage --- lib/set_list.dart | 22 +++------------- lib/set_tile.dart | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 lib/set_tile.dart diff --git a/lib/set_list.dart b/lib/set_list.dart index ba32818..12e38d7 100644 --- a/lib/set_list.dart +++ b/lib/set_list.dart @@ -2,8 +2,8 @@ import 'package:flutter/material.dart'; import 'package:fmassive/database.dart'; import 'package:fmassive/edit_set.dart'; import 'package:fmassive/main.dart'; +import 'package:fmassive/set_tile.dart'; import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart'; -import 'package:intl/intl.dart'; import 'package:moor_flutter/moor_flutter.dart'; class SetList extends StatefulWidget { @@ -65,23 +65,9 @@ class _SetList extends State { body: PagedListView( pagingController: pagingController, builderDelegate: PagedChildBuilderDelegate( - itemBuilder: (context, gymSet, index) => ListTile( - title: Text(gymSet.name), - subtitle: Text("${gymSet.reps} x ${gymSet.weight}kg"), - trailing: Text( - DateFormat("yyyy-MM-dd").format(DateTime.parse(gymSet.created)), - ), - onTap: () async { - await Navigator.push( - context, - MaterialPageRoute( - builder: (context) => EditGymSetPage( - gymSet: gymSet.toCompanion(false), - ), - ), - ); - pagingController.refresh(); - }, + itemBuilder: (context, gymSet, index) => SetTile( + pagingController: pagingController, + gymSet: gymSet, ), firstPageProgressIndicatorBuilder: (_) => const Center(child: CircularProgressIndicator()), diff --git a/lib/set_tile.dart b/lib/set_tile.dart new file mode 100644 index 0000000..f1b885f --- /dev/null +++ b/lib/set_tile.dart @@ -0,0 +1,67 @@ +import 'package:flutter/material.dart'; +import 'package:fmassive/database.dart'; +import 'package:fmassive/edit_set.dart'; +import 'package:fmassive/main.dart'; +import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart'; +import 'package:intl/intl.dart'; +import 'package:moor/moor.dart'; + +class SetTile extends StatelessWidget { + const SetTile({ + super.key, + required this.pagingController, + required this.gymSet, + }); + + final PagingController pagingController; + final GymSet gymSet; + + @override + Widget build(BuildContext context) { + return ListTile( + title: Text(gymSet.name), + subtitle: Text("${gymSet.reps} x ${gymSet.weight}kg"), + trailing: Text( + DateFormat("yyyy-MM-dd").format(DateTime.parse(gymSet.created)), + ), + onTap: () async { + await Navigator.push( + context, + MaterialPageRoute( + builder: (context) => EditGymSetPage( + gymSet: gymSet.toCompanion(false), + ), + ), + ); + pagingController.refresh(); + }, + onLongPress: () => showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + title: const Text('Delete set'), + content: Text( + 'Are you sure you want to delete ${gymSet.name} ${gymSet.reps}x${gymSet.weight}${gymSet.unit}?'), + actions: [ + ElevatedButton( + child: const Text('Cancel'), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + ElevatedButton( + child: const Text('Delete'), + onPressed: () async { + final navigator = Navigator.of(context); + await db.gymSets.deleteOne(gymSet); + pagingController.refresh(); + navigator.pop(); + }, + ), + ], + ); + }, + ), + ); + } +}