boxchecker/lib/check_list.dart

173 lines
5.3 KiB
Dart
Raw Normal View History

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> {
2021-11-03 17:42:42 -04:00
bool _editable = false;
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;
}
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 22:00:02 -04:00
void _removeItem(data.Check item) async {
DBHelper.dbHelper.deleteItem(item);
}
2021-11-03 17:28:03 -04:00
void _updateItem(data.Check item) async {
DBHelper.dbHelper.updateItem(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) {
2021-11-03 22:00:02 -04:00
return Dismissible(
key: Key(list[index].id.toString()),
confirmDismiss: (direction) async {
2021-11-04 13:13:11 -04:00
var item = list[index];
if (direction == DismissDirection.endToStart) {
2021-11-03 22:00:02 -04:00
setState(() {
2021-11-04 13:13:11 -04:00
_removeItem(item);
2021-11-03 22:00:02 -04:00
list.removeAt(index);
});
return true;
}
2021-11-04 13:13:11 -04:00
_updateItem(item);
2021-11-03 22:00:02 -04:00
setState(() {
2021-11-04 13:13:11 -04:00
item.value = !item.value;
2021-11-03 22:00:02 -04:00
});
return false;
},
background: Container(
2021-11-04 13:13:11 -04:00
color: Colors.blue,
child: Row(children: const [
Padding(
padding: EdgeInsets.all(10),
child: Icon(Icons.check),
),
Spacer(),
]),
),
secondaryBackground: Container(
color: Colors.red,
child: Row(children: const [
Spacer(),
Padding(
padding: EdgeInsets.all(10),
child: Icon(Icons.delete_forever)),
]),
2021-11-03 22:00:02 -04:00
),
2021-11-03 18:55:51 -04:00
child: CheckboxListTile(
title: TextFormField(
enabled: (listData != null &&
(!listData!.isTemplate! || _editable)),
2021-11-20 17:51:29 -05:00
decoration: const InputDecoration(border: InputBorder.none),
2021-11-03 18:55:51 -04:00
initialValue: list[index].text,
onChanged: (value) {
list[index].text = value;
_updateItem(list[index]);
},
),
controlAffinity: ListTileControlAffinity.leading,
value: list[index].value,
onChanged: (listData != null &&
(!listData!.isTemplate! || _editable))
? ((value) {
_updateItem(list[index]);
setState(() {
list[index].value = value!;
});
})
: null,
2021-11-03 18:45:37 -04:00
),
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
),
);
}
}