diff --git a/lib/widgets/measurements/entries.dart b/lib/widgets/measurements/entries.dart index c3cf8337..cb4134ed 100644 --- a/lib/widgets/measurements/entries.dart +++ b/lib/widgets/measurements/entries.dart @@ -52,72 +52,46 @@ class EntriesList extends StatelessWidget { final currentEntry = _category.entries[index]; final provider = Provider.of(context, listen: false); - return Dismissible( - key: Key(currentEntry.id.toString()), - onDismissed: (direction) { - if (direction == DismissDirection.endToStart) { - // Delete entry from DB - provider.deleteEntry(currentEntry.id!, currentEntry.category); + return Card( + child: ListTile( + title: Text('${currentEntry.value} ${_category.unit}'), + subtitle: Text( + DateFormat.yMd(Localizations.localeOf(context).languageCode) + .format(currentEntry.date), + ), + trailing: PopupMenuButton( + itemBuilder: (BuildContext context) { + return [ + PopupMenuItem( + child: Text(AppLocalizations.of(context).edit), + onTap: () => Navigator.pushNamed( + context, + FormScreen.routeName, + arguments: FormScreenArguments( + AppLocalizations.of(context).edit, + MeasurementEntryForm(currentEntry.category, currentEntry), + ), + )), + PopupMenuItem( + child: Text(AppLocalizations.of(context).delete), + onTap: () async { + // Delete entry from DB + await provider.deleteEntry(currentEntry.id!, currentEntry.category); - // and inform the user - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text( - AppLocalizations.of(context).successfullyDeleted, - textAlign: TextAlign.center, - ), - ), - ); - } - }, - confirmDismiss: (direction) async { - // Edit entry - if (direction == DismissDirection.startToEnd) { - Navigator.pushNamed( - context, - FormScreen.routeName, - arguments: FormScreenArguments( - AppLocalizations.of(context).edit, - MeasurementEntryForm(currentEntry.category, currentEntry), - ), - ); - return false; - } - return true; - }, - secondaryBackground: Container( - color: Theme.of(context).colorScheme.error, - alignment: Alignment.centerRight, - padding: const EdgeInsets.only(right: 20), - margin: const EdgeInsets.symmetric( - horizontal: 4, - vertical: 4, - ), - child: const Icon( - Icons.delete, - color: Colors.white, - ), - ), - background: Container( - color: wgerPrimaryButtonColor, - alignment: Alignment.centerLeft, - padding: const EdgeInsets.only(left: 20), - margin: const EdgeInsets.symmetric( - horizontal: 4, - vertical: 4, - ), - child: const Icon( - Icons.edit, - color: Colors.white, - ), - ), - child: Card( - child: ListTile( - title: Text('${currentEntry.value} ${_category.unit}'), - subtitle: Text( - DateFormat.yMd(Localizations.localeOf(context).languageCode) - .format(currentEntry.date), - ), + // and inform the user + if (context.mounted) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + AppLocalizations.of(context).successfullyDeleted, + textAlign: TextAlign.center, + ), + ), + ); + } + }) + ]; + }, ), ), ); diff --git a/lib/widgets/weight/entries_list.dart b/lib/widgets/weight/entries_list.dart index 8377ec5b..ff3f033e 100644 --- a/lib/widgets/weight/entries_list.dart +++ b/lib/widgets/weight/entries_list.dart @@ -60,72 +60,46 @@ class WeightEntriesList extends StatelessWidget { itemCount: weightProvider.items.length, itemBuilder: (context, index) { final currentEntry = weightProvider.items[index]; - return Dismissible( - key: Key(currentEntry.id.toString()), - onDismissed: (direction) { - if (direction == DismissDirection.endToStart) { - // Delete entry from DB - weightProvider.deleteEntry(currentEntry.id!); + return Card( + child: ListTile( + title: Text('${currentEntry.weight} kg'), + subtitle: Text( + DateFormat.yMd(Localizations.localeOf(context).languageCode) + .format(currentEntry.date), + ), + trailing: PopupMenuButton( + itemBuilder: (BuildContext context) { + return [ + PopupMenuItem( + child: Text(AppLocalizations.of(context).edit), + onTap: () => Navigator.pushNamed( + context, + FormScreen.routeName, + arguments: FormScreenArguments( + AppLocalizations.of(context).edit, + WeightForm(currentEntry), + ), + )), + PopupMenuItem( + child: Text(AppLocalizations.of(context).delete), + onTap: () async { + // Delete entry from DB + await weightProvider.deleteEntry(currentEntry.id!); - // and inform the user - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text( - AppLocalizations.of(context).successfullyDeleted, - textAlign: TextAlign.center, - ), - ), - ); - } - }, - confirmDismiss: (direction) async { - // Edit entry - if (direction == DismissDirection.startToEnd) { - Navigator.pushNamed( - context, - FormScreen.routeName, - arguments: FormScreenArguments( - AppLocalizations.of(context).edit, - WeightForm(currentEntry), - ), - ); - return false; - } - return true; - }, - secondaryBackground: Container( - color: Theme.of(context).colorScheme.error, - alignment: Alignment.centerRight, - padding: const EdgeInsets.only(right: 20), - margin: const EdgeInsets.symmetric( - horizontal: 4, - vertical: 4, - ), - child: const Icon( - Icons.delete, - color: Colors.white, - ), - ), - background: Container( - // color: wgerPrimaryButtonColor, - alignment: Alignment.centerLeft, - padding: const EdgeInsets.only(left: 20), - margin: const EdgeInsets.symmetric( - horizontal: 4, - vertical: 4, - ), - child: const Icon( - Icons.edit, - color: Colors.white, - ), - ), - child: Card( - child: ListTile( - title: Text('${currentEntry.weight} kg'), - subtitle: Text( - DateFormat.yMd(Localizations.localeOf(context).languageCode) - .format(currentEntry.date), - ), + // and inform the user + if (context.mounted) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + AppLocalizations.of(context).successfullyDeleted, + textAlign: TextAlign.center, + ), + ), + ); + } + }) + ]; + }, ), ), ); diff --git a/test/weight/weight_screen_test.dart b/test/weight/weight_screen_test.dart index 10bd75bd..f566e8e1 100644 --- a/test/weight/weight_screen_test.dart +++ b/test/weight/weight_screen_test.dart @@ -58,15 +58,21 @@ void main() { expect(find.text('Weight'), findsOneWidget); expect(find.byType(MeasurementChartWidgetFl), findsOneWidget); - expect(find.byType(Dismissible), findsNWidgets(2)); + expect(find.byType(Card), findsNWidgets(2)); expect(find.byType(ListTile), findsNWidgets(2)); }); - testWidgets('Test deleting an item by dragging the dismissible', (WidgetTester tester) async { + testWidgets('Test deleting an item using the Delete button', (WidgetTester tester) async { await tester.pumpWidget(createWeightScreen()); - await tester.drag(find.byKey(const Key('1')), const Offset(-500.0, 0.0)); + expect(find.byType(ListTile), findsNWidgets(2)); + + await tester.tap(find.byTooltip('Show menu').first); await tester.pumpAndSettle(); + + await tester.tap(find.text('Delete')); + await tester.pumpAndSettle(); + verify(mockWeightProvider.deleteEntry(1)).called(1); expect(find.byType(ListTile), findsOneWidget); });