refactored card lists into seperate widgets
This commit is contained in:
parent
647cdc4002
commit
98942b231b
|
|
@ -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<bool?> Function(DismissDirection);
|
||||
|
||||
void main() {
|
||||
runApp(const BoxChecker());
|
||||
}
|
||||
|
|
@ -46,7 +49,6 @@ class _MainListPageState extends State<MainListPage> {
|
|||
List<data.List> lists = [];
|
||||
|
||||
final double _marginHorizontal = 10;
|
||||
final double _cardSpacing = 4;
|
||||
|
||||
Future<void> _loadData(data.Page listType) async {
|
||||
lists.clear();
|
||||
|
|
@ -58,10 +60,6 @@ class _MainListPageState extends State<MainListPage> {
|
|||
});
|
||||
}
|
||||
|
||||
Future<void> _removeList(data.List list) async {
|
||||
DBHelper.dbHelper.deleteList(list);
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
_loadData(data.Page.lists);
|
||||
|
|
@ -69,11 +67,42 @@ class _MainListPageState extends State<MainListPage> {
|
|||
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<MainListPage> {
|
|||
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(
|
||||
|
|
|
|||
|
|
@ -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<bool?> Function(DismissDirection);
|
||||
import 'box_checker.dart';
|
||||
|
||||
class CheckListItem extends StatefulWidget {
|
||||
const CheckListItem(
|
||||
|
|
|
|||
|
|
@ -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! : ""),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue