checked items on db
This commit is contained in:
@@ -24,6 +24,7 @@ class _DetailChecklistPageState extends State<DetailChecklistPage> {
|
||||
Checklist? _checklist;
|
||||
late Future<List<Object>> _checklistFutures;
|
||||
late final ChecklistProvider _checklistProvider;
|
||||
List<int> _checkedItemIds = [];
|
||||
List<Item> _items = [];
|
||||
int? _selectedItemId;
|
||||
bool _titleEditMode = false;
|
||||
@@ -45,14 +46,22 @@ class _DetailChecklistPageState extends State<DetailChecklistPage> {
|
||||
return Future.wait([
|
||||
DbHelper.getChecklistById(checklistId),
|
||||
DbHelper.getItemsByChecklistId(checklistId),
|
||||
DbHelper.fetchcheckedItemIds(checklistId),
|
||||
]);
|
||||
}
|
||||
|
||||
Widget _futureBuilder(
|
||||
BuildContext context, AsyncSnapshot<List<Object>> snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
_checklist = snapshot.data!.first as Checklist;
|
||||
_items = snapshot.data!.last as List<Item>;
|
||||
_checklist = snapshot.data!.elementAt(0) as Checklist;
|
||||
_items = snapshot.data!.elementAt(1) as List<Item>;
|
||||
_checkedItemIds = snapshot.data!.elementAt(2) as List<int>;
|
||||
if (pageTitle == null) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => setState(() =>
|
||||
pageTitle = _checklist!.title == ''
|
||||
? 'Unnamed ${_checklist!.id}'
|
||||
: _checklist!.title));
|
||||
}
|
||||
return StreamBuilder(
|
||||
stream: DbHelper.itemsChangeEventStream,
|
||||
builder: (BuildContext context,
|
||||
@@ -130,13 +139,17 @@ class _DetailChecklistPageState extends State<DetailChecklistPage> {
|
||||
}
|
||||
|
||||
Widget? _itemListBuilder(BuildContext context, int index) {
|
||||
Item item = _items.elementAt(index);
|
||||
return ItemListTile(
|
||||
title: _items.elementAt(index).title,
|
||||
description: _items.elementAt(index).description,
|
||||
title: item.title,
|
||||
description: item.description,
|
||||
onTap: () => _itemTapped(index),
|
||||
itemSelectionChanged: (isSelected) =>
|
||||
_itemSelectionChanged(isSelected, index),
|
||||
selectionMode: selectedItemIndexes.isNotEmpty,
|
||||
isChecked: _checkedItemIds.contains(item.id),
|
||||
onCheckedChanged: (isSelected) =>
|
||||
_onItemCheckedChanged(isSelected, item.id),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -170,10 +183,12 @@ class _DetailChecklistPageState extends State<DetailChecklistPage> {
|
||||
child: Text(pageTitle ?? ''));
|
||||
} else {
|
||||
titleController.text = pageTitle ?? '';
|
||||
return Expanded(
|
||||
child: TextField(
|
||||
autofocus: true,
|
||||
controller: titleController,
|
||||
return TextField(
|
||||
autofocus: true,
|
||||
controller: titleController,
|
||||
onSubmitted: (value) => _onTitleChanged(
|
||||
_checklist!.id,
|
||||
titleController.text,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -192,12 +207,6 @@ class _DetailChecklistPageState extends State<DetailChecklistPage> {
|
||||
AsyncSnapshot<List<Map<String, dynamic>>> snapshot,
|
||||
Checklist? checklist,
|
||||
List<Item> items) {
|
||||
if (pageTitle != _checklist!.title) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => setState(() =>
|
||||
pageTitle = _checklist!.title == ''
|
||||
? 'Unnamed ${_checklist!.id}'
|
||||
: _checklist!.title));
|
||||
}
|
||||
if (snapshot.hasData) {
|
||||
_items = DbHelper.resToItemList(snapshot.data!);
|
||||
}
|
||||
@@ -250,4 +259,12 @@ class _DetailChecklistPageState extends State<DetailChecklistPage> {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
_onItemCheckedChanged(bool isSelected, int? id) async {
|
||||
if (isSelected) {
|
||||
await DbHelper.insertCheckedEntry(_checklist!.id, id!);
|
||||
} else {
|
||||
await DbHelper.deleteCheckedEntry(_checklist!.id, id!);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user