diff --git a/lib/box_checker.dart b/lib/box_checker.dart index 6077ed6..4f7d443 100644 --- a/lib/box_checker.dart +++ b/lib/box_checker.dart @@ -53,6 +53,10 @@ class _MainListPageState extends State { }); } + Future _removeList(data.List list) async { + DBHelper.dbHelper.deleteList(list); + } + @override void initState() { _loadData(data.Page.lists); @@ -84,17 +88,66 @@ class _MainListPageState extends State { child: ListView.builder( itemCount: lists.length, itemBuilder: (context, index) { - return Card( - child: ListTile( - onTap: () { - Navigator.push( - context, - MaterialPageRoute( - builder: (context) => - CheckList(id: lists[index].id!))); - }, - title: Text(lists[index].name), - subtitle: Text(lists[index].id.toString()), + return Dismissible( + direction: DismissDirection.startToEnd, + key: Key(lists[index].id.toString()), + background: Card( + color: Colors.red, + child: Row( + children: const [ + Padding( + padding: EdgeInsets.all(10), + child: Icon(Icons.delete), + ), + ], + ), + ), + confirmDismiss: (direction) async { + if (direction != DismissDirection.startToEnd) { + return false; + } + + showDialog( + context: context, + builder: (context) { + return AlertDialog( + title: const Text('Are you sure?'), + content: const Text( + 'You are about to delete this permently'), + actions: [ + TextButton( + onPressed: () => + Navigator.pop(context, false), + child: const Text('Cancel')), + TextButton( + onPressed: () => + Navigator.pop(context, true), + child: const Text('OK')), + ], + ); + }).then((value) { + if (value) { + setState(() { + _removeList(lists[index]); + lists.removeAt(index); + }); + } + + return value; + }); + }, + child: Card( + child: ListTile( + onTap: () { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => + CheckList(id: lists[index].id!))); + }, + title: Text(lists[index].name), + subtitle: Text(lists[index].id.toString()), + ), ), ); })), diff --git a/lib/db_helper.dart b/lib/db_helper.dart index 8d5fb0f..16edee7 100644 --- a/lib/db_helper.dart +++ b/lib/db_helper.dart @@ -77,6 +77,12 @@ class DBHelper { return db.insert("List", l.toMap()); } + Future deleteList(data.List l) async { + Database db = await database; + + return db.delete("List", where: 'id = ?', whereArgs: [l.id]); + } + Future insertItem(data.Check item) async { Database db = await database;