Factor out plan_tile

This commit is contained in:
Brandon Presley 2023-11-30 13:02:20 +13:00
parent fbffc0cf4a
commit 202ec599d6
2 changed files with 57 additions and 49 deletions

View File

@ -1,10 +1,7 @@
import 'package:flutter/material.dart';
import 'package:fmassive/constants.dart';
import 'package:fmassive/database.dart';
import 'package:fmassive/edit_plan.dart';
import 'package:fmassive/main.dart';
import 'package:fmassive/start_plan.dart';
import 'package:moor_flutter/moor_flutter.dart';
import 'package:fmassive/plan_tile.dart';
class PlanList extends StatelessWidget {
const PlanList({
@ -21,51 +18,7 @@ class PlanList extends StatelessWidget {
return ListView.builder(
itemCount: plans.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(plans[index].days.replaceAll(',', ', '),
style: TextStyle(
fontWeight: plans[index].days.contains(weekday)
? FontWeight.bold
: null,
decoration: plans[index].days.contains(weekday)
? TextDecoration.underline
: null)),
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) =>
StartPlan(plan: plans[index].toCompanion(false)),
),
);
});
return PlanTile(plan: plans[index], weekday: weekday);
},
);
}

55
lib/plan_tile.dart Normal file
View File

@ -0,0 +1,55 @@
import 'package:flutter/material.dart';
import 'package:fmassive/database.dart';
import 'package:fmassive/main.dart';
import 'package:fmassive/start_plan.dart';
import 'package:moor_flutter/moor_flutter.dart';
class PlanTile extends StatelessWidget {
PlanTile({
super.key,
required this.plan,
required this.weekday,
});
final Plan plan;
final String weekday;
final tapPosition = GlobalKey();
@override
Widget build(BuildContext context) {
return MenuAnchor(
menuChildren: [
MenuItemButton(
onPressed: () async {
await db.plans.deleteOne(plan);
},
child: const Text("Delete"),
),
],
builder: (context, controller, child) {
return ListTile(
title: Text(
plan.days.replaceAll(',', ', '),
style: TextStyle(
fontWeight: plan.days.contains(weekday) ? FontWeight.bold : null,
decoration:
plan.days.contains(weekday) ? TextDecoration.underline : null,
),
),
subtitle: Text(plan.exercises),
onLongPress: () {
controller.open();
},
onTap: () async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => StartPlan(plan: plan.toCompanion(false)),
),
);
},
);
},
);
}
}