boxchecker/lib/check_list.dart

127 lines
3.6 KiB
Dart
Raw Normal View History

import 'package:boxchecker/check_list_item.dart';
2021-11-03 17:28:03 -04:00
import 'package:flutter/material.dart';
import 'data_util.dart' as data;
import 'db_helper.dart';
class CheckList extends StatefulWidget {
const CheckList({Key? key, required this.id}) : super(key: key);
final int id;
@override
2021-11-20 17:51:29 -05:00
State<StatefulWidget> createState() => _CheckList();
2021-11-03 17:28:03 -04:00
}
class _CheckList extends State<CheckList> {
late bool _editable;
2021-11-03 17:28:03 -04:00
data.List? listData;
List<data.Check> list = [];
void _loadList() async {
2021-11-20 17:51:29 -05:00
var rows = await DBHelper.dbHelper.getList(widget.id);
2021-11-03 17:28:03 -04:00
list.clear();
setState(() {
for (var row in rows) {
list.add(data.Check.fromMap(row));
}
});
}
void _loadListData() async {
2021-11-20 17:51:29 -05:00
var rows = await DBHelper.dbHelper.getListData(widget.id);
2021-11-03 17:28:03 -04:00
listData = (rows.isNotEmpty) ? data.List.fromMap(rows[0]) : null;
_editable = (listData != null && !listData!.isTemplate!);
2021-11-03 17:28:03 -04:00
}
2021-11-03 19:41:20 -04:00
void _addItem() async {
var item = data.Check("", false, listID: listData!.id!);
int id = await DBHelper.dbHelper.insertItem(item);
item.id = id;
setState(() {
list.add(item);
});
}
2021-11-03 17:42:42 -04:00
void _toggleEditable() {
setState(() {
_editable = !_editable;
});
}
2021-11-03 17:28:03 -04:00
@override
void initState() {
_loadListData();
2021-11-03 18:45:37 -04:00
_loadList();
2021-11-03 17:28:03 -04:00
super.initState();
}
2021-11-04 22:36:27 -04:00
Widget iconButton() {
return IconButton(
onPressed: () => _toggleEditable(),
icon: Icon(_editable ? Icons.lock_open : Icons.lock),
);
}
2021-11-03 17:28:03 -04:00
@override
Widget build(BuildContext context) {
2021-11-03 18:55:51 -04:00
return GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: Scaffold(
appBar: AppBar(
2021-11-20 17:51:29 -05:00
title: Text(
(listData != null) ? listData!.name : 'Check List: ${widget.id}'),
2021-11-04 22:36:27 -04:00
actions:
(listData != null && listData!.isTemplate!) ? [iconButton()] : [],
2021-11-03 18:55:51 -04:00
),
body: RefreshIndicator(
onRefresh: () async {
_loadListData();
_loadList();
},
child: ListView.builder(
itemCount: list.length,
itemBuilder: (context, index) {
return CheckListItem(
2021-11-03 22:00:02 -04:00
confirmDismiss: (direction) async {
if (!_editable) return false;
2021-11-04 13:13:11 -04:00
if (direction == DismissDirection.endToStart) {
2021-11-03 22:00:02 -04:00
setState(() {
DBHelper.dbHelper.deleteItem(list[index]);
2021-11-03 22:00:02 -04:00
list.removeAt(index);
});
return true;
}
DBHelper.dbHelper.updateItem(list[index]);
2021-11-03 22:00:02 -04:00
setState(() {
list[index].value = !list[index].value;
2021-11-03 22:00:02 -04:00
});
return false;
},
item: list[index],
locked: !_editable,
2021-11-03 18:55:51 -04:00
);
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
if (listData!.isTemplate! && !_editable) {
ScaffoldMessenger.of(context)
..clearSnackBars()
2021-11-20 17:51:29 -05:00
..showSnackBar(
const SnackBar(content: Text("Template is locked")));
2021-11-03 18:55:51 -04:00
return;
}
2021-11-03 19:41:20 -04:00
_addItem();
// TODO switch focus to new card
2021-11-03 18:45:37 -04:00
},
2021-11-03 18:55:51 -04:00
child: const Icon(Icons.check_box_outlined),
tooltip: "Add Item",
2021-11-03 18:45:37 -04:00
),
2021-11-03 18:55:51 -04:00
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
2021-11-03 17:28:03 -04:00
),
);
}
}