Long press on plan list to delete a plan

This commit is contained in:
Brandon Presley 2023-11-11 15:24:57 +13:00
parent cbb2e2c4b0
commit 4aa6926f5c
2 changed files with 63 additions and 17 deletions

61
lib/plan_list.dart Normal file
View File

@ -0,0 +1,61 @@
import 'package:flutter/material.dart';
import 'package:fmassive/database.dart';
import 'package:fmassive/edit_plan.dart';
import 'package:fmassive/main.dart';
import 'package:moor_flutter/moor_flutter.dart';
class PlanList extends StatelessWidget {
const PlanList({
super.key,
required this.plans,
});
final List<Plan> plans;
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: plans.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(plans[index].days.replaceAll(',', ', ')),
subtitle: Text(plans[index].exercises),
onLongPress: () => showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Delete set'),
content: Text(
'Are you sure you want to delete ${plans[index].days}?'),
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.plans.deleteOne(plans[index]);
navigator.pop();
},
),
],
);
},
),
onTap: () async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
EditPlanPage(plan: plans[index].toCompanion(false)),
),
);
});
},
);
}
}

View File

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:fmassive/database.dart'; import 'package:fmassive/database.dart';
import 'package:fmassive/edit_plan.dart'; import 'package:fmassive/edit_plan.dart';
import 'package:fmassive/main.dart'; import 'package:fmassive/main.dart';
import 'package:fmassive/plan_list.dart';
import 'package:moor/moor.dart'; import 'package:moor/moor.dart';
class PlansPage extends StatelessWidget { class PlansPage extends StatelessWidget {
@ -78,23 +79,7 @@ class _PlansPageState extends State<_PlansPage> {
if (plans == null) if (plans == null)
return const Center(child: CircularProgressIndicator()); return const Center(child: CircularProgressIndicator());
return ListView.builder( return PlanList(plans: plans);
itemCount: plans.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(plans[index].days.replaceAll(',', ', ')),
subtitle: Text(plans[index].exercises),
onTap: () async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => EditPlanPage(
plan: plans[index].toCompanion(false)),
),
);
});
},
);
}), }),
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
onPressed: () async { onPressed: () async {