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 'package:flutter/material.dart';
|
||||||
import 'add_form.dart';
|
import 'add_form.dart';
|
||||||
import 'check_list.dart';
|
import 'check_list.dart';
|
||||||
import 'db_helper.dart';
|
import 'db_helper.dart';
|
||||||
import 'data_util.dart' as data;
|
import 'data_util.dart' as data;
|
||||||
|
|
||||||
|
typedef FutureCallback = Future<bool?> Function(DismissDirection);
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const BoxChecker());
|
runApp(const BoxChecker());
|
||||||
}
|
}
|
||||||
|
|
@ -46,7 +49,6 @@ class _MainListPageState extends State<MainListPage> {
|
||||||
List<data.List> lists = [];
|
List<data.List> lists = [];
|
||||||
|
|
||||||
final double _marginHorizontal = 10;
|
final double _marginHorizontal = 10;
|
||||||
final double _cardSpacing = 4;
|
|
||||||
|
|
||||||
Future<void> _loadData(data.Page listType) async {
|
Future<void> _loadData(data.Page listType) async {
|
||||||
lists.clear();
|
lists.clear();
|
||||||
|
|
@ -58,10 +60,6 @@ class _MainListPageState extends State<MainListPage> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _removeList(data.List list) async {
|
|
||||||
DBHelper.dbHelper.deleteList(list);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
_loadData(data.Page.lists);
|
_loadData(data.Page.lists);
|
||||||
|
|
@ -69,11 +67,42 @@ class _MainListPageState extends State<MainListPage> {
|
||||||
super.initState();
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final EdgeInsets cardMargin =
|
|
||||||
EdgeInsets.symmetric(vertical: _cardSpacing, horizontal: 0);
|
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: Text(widget.title),
|
title: Text(widget.title),
|
||||||
|
|
@ -98,71 +127,9 @@ class _MainListPageState extends State<MainListPage> {
|
||||||
child: ListView.builder(
|
child: ListView.builder(
|
||||||
itemCount: lists.length,
|
itemCount: lists.length,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
return Dismissible(
|
return ListCard(
|
||||||
direction: DismissDirection.startToEnd,
|
checkList: lists[index],
|
||||||
key: Key(lists[index].id.toString()),
|
confirmDismiss: genCofirmDismiss(index),
|
||||||
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!
|
|
||||||
: ""),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}))),
|
}))),
|
||||||
bottomNavigationBar: BottomNavigationBar(
|
bottomNavigationBar: BottomNavigationBar(
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,7 @@ import 'package:boxchecker/check_list.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'data_util.dart' as data;
|
import 'data_util.dart' as data;
|
||||||
import 'db_helper.dart';
|
import 'db_helper.dart';
|
||||||
|
import 'box_checker.dart';
|
||||||
typedef FutureCallback = Future<bool?> Function(DismissDirection);
|
|
||||||
|
|
||||||
class CheckListItem extends StatefulWidget {
|
class CheckListItem extends StatefulWidget {
|
||||||
const CheckListItem(
|
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