Add long press to delete from homepage

This commit is contained in:
Brandon Presley 2023-11-10 18:50:56 +13:00
parent de926bce0e
commit fb278e4487
2 changed files with 71 additions and 18 deletions

View File

@ -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<SetList> {
body: PagedListView<int, GymSet>(
pagingController: pagingController,
builderDelegate: PagedChildBuilderDelegate<GymSet>(
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()),

67
lib/set_tile.dart Normal file
View File

@ -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<int, GymSet> 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: <Widget>[
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();
},
),
],
);
},
),
);
}
}