From 98942b231b416c4d2b58e2819d70ff9962f1a328 Mon Sep 17 00:00:00 2001 From: andrei Date: Tue, 23 Nov 2021 19:05:09 -0500 Subject: [PATCH] refactored card lists into seperate widgets --- lib/box_checker.dart | 113 ++++++++++++++------------------------- lib/check_list_item.dart | 3 +- lib/list_card.dart | 53 ++++++++++++++++++ 3 files changed, 94 insertions(+), 75 deletions(-) create mode 100644 lib/list_card.dart diff --git a/lib/box_checker.dart b/lib/box_checker.dart index 7075058..ec457aa 100644 --- a/lib/box_checker.dart +++ b/lib/box_checker.dart @@ -1,9 +1,12 @@ +import 'package:boxchecker/list_card.dart'; import 'package:flutter/material.dart'; import 'add_form.dart'; import 'check_list.dart'; import 'db_helper.dart'; import 'data_util.dart' as data; +typedef FutureCallback = Future Function(DismissDirection); + void main() { runApp(const BoxChecker()); } @@ -46,7 +49,6 @@ class _MainListPageState extends State { List lists = []; final double _marginHorizontal = 10; - final double _cardSpacing = 4; Future _loadData(data.Page listType) async { lists.clear(); @@ -58,10 +60,6 @@ class _MainListPageState extends State { }); } - Future _removeList(data.List list) async { - DBHelper.dbHelper.deleteList(list); - } - @override void initState() { _loadData(data.Page.lists); @@ -69,11 +67,42 @@ class _MainListPageState extends State { super.initState(); } + FutureCallback genCofirmDismiss(index) { + return (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(() { + DBHelper.dbHelper.deleteList(lists[index]); + lists.removeAt(index); + }); + } + + return value; + }); + }; + } + @override Widget build(BuildContext context) { - final EdgeInsets cardMargin = - EdgeInsets.symmetric(vertical: _cardSpacing, horizontal: 0); - return Scaffold( appBar: AppBar( title: Text(widget.title), @@ -98,71 +127,9 @@ class _MainListPageState extends State { child: ListView.builder( itemCount: lists.length, itemBuilder: (context, index) { - return Dismissible( - direction: DismissDirection.startToEnd, - key: Key(lists[index].id.toString()), - background: Card( - margin: cardMargin, - 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( - margin: cardMargin, - child: ListTile( - onTap: () { - Navigator.push( - context, - MaterialPageRoute( - builder: (context) => - CheckList(id: lists[index].id!))); - }, - title: Text(lists[index].name), - subtitle: Text((lists[index].description != null) - ? lists[index].description! - : ""), - ), - ), + return ListCard( + checkList: lists[index], + confirmDismiss: genCofirmDismiss(index), ); }))), bottomNavigationBar: BottomNavigationBar( diff --git a/lib/check_list_item.dart b/lib/check_list_item.dart index 1a4da11..6198baf 100644 --- a/lib/check_list_item.dart +++ b/lib/check_list_item.dart @@ -2,8 +2,7 @@ import 'package:boxchecker/check_list.dart'; import 'package:flutter/material.dart'; import 'data_util.dart' as data; import 'db_helper.dart'; - -typedef FutureCallback = Future Function(DismissDirection); +import 'box_checker.dart'; class CheckListItem extends StatefulWidget { const CheckListItem( diff --git a/lib/list_card.dart b/lib/list_card.dart new file mode 100644 index 0000000..3e4520f --- /dev/null +++ b/lib/list_card.dart @@ -0,0 +1,53 @@ +import 'package:flutter/material.dart'; +import 'data_util.dart' as data; +import 'check_list.dart'; +import 'box_checker.dart'; + +class ListCard extends StatelessWidget { + const ListCard({ + Key? key, + required this.checkList, + required this.confirmDismiss, + }) : super(key: key); + final data.List checkList; + final FutureCallback confirmDismiss; + final double _cardSpacing = 4; + + @override + Widget build(BuildContext context) { + final EdgeInsets cardMargin = + EdgeInsets.symmetric(vertical: _cardSpacing, horizontal: 0); + + return Dismissible( + direction: DismissDirection.startToEnd, + key: Key(checkList.id.toString()), + background: Card( + margin: cardMargin, + color: Colors.red, + child: Row( + children: const [ + Padding( + padding: EdgeInsets.all(10), + child: Icon(Icons.delete), + ), + ], + ), + ), + confirmDismiss: confirmDismiss, + child: Card( + margin: cardMargin, + child: ListTile( + onTap: () { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => CheckList(id: checkList.id!))); + }, + title: Text(checkList.name), + subtitle: Text( + (checkList.description != null) ? checkList.description! : ""), + ), + ), + ); + } +}