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 State createState() => _CheckList(id); } class _CheckList extends State { _CheckList(this.id); final int id; bool _editable = false; data.List? listData; List list = []; void _loadList() async { var rows = await DBHelper.dbHelper.getList(id); list.clear(); setState(() { for (var row in rows) { list.add(data.Check.fromMap(row)); } }); } void _loadListData() async { var rows = await DBHelper.dbHelper.getListData(id); listData = (rows.isNotEmpty) ? data.List.fromMap(rows[0]) : null; } void _updateItem(data.Check item) async { DBHelper.dbHelper.updateItem(item); } void _toggleEditable() { setState(() { _editable = !_editable; }); } @override void initState() { _loadList(); _loadListData(); super.initState(); } @override Widget build(BuildContext context) { // TODO: implement build return Scaffold( appBar: AppBar( title: Text((listData != null) ? listData!.name : 'Check List: $id'), actions: (listData != null && listData!.isTemplate! && !_editable) ? [ IconButton( onPressed: () => _toggleEditable(), icon: const Icon(Icons.lock)) ] : (listData != null && listData!.isTemplate!) ? [ IconButton( onPressed: () => _toggleEditable(), icon: const Icon(Icons.lock_open)) ] : [], ), body: ListView.builder( itemCount: list.length, itemBuilder: (context, index) { return Card( child: CheckboxListTile( title: TextFormField( enabled: (listData != null && (!listData!.isTemplate! || _editable)), decoration: InputDecoration(border: InputBorder.none), 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, ), ); }, ), floatingActionButton: FloatingActionButton( onPressed: () { if (listData!.isTemplate! && !_editable) { ScaffoldMessenger.of(context) ..clearSnackBars() ..showSnackBar(SnackBar(content: Text("Template is locked"))); return; } // TODO implment adding items to lists }, child: const Icon(Icons.check_box_outlined), tooltip: "Add Item", ), floatingActionButtonLocation: FloatingActionButtonLocation.endFloat, ); } }