From 6b6c96ea43e1163c38876c1e2ffe90967647b1e7 Mon Sep 17 00:00:00 2001 From: andrei Date: Wed, 3 Nov 2021 17:28:03 -0400 Subject: [PATCH] checklist page with editing --- lib/add_form.dart | 1 + lib/box_checker.dart | 23 ++++++++---- lib/check_list.dart | 83 ++++++++++++++++++++++++++++++++++++++++++++ lib/data_util.dart | 4 +-- lib/db_helper.dart | 23 ++++++++++++ 5 files changed, 126 insertions(+), 8 deletions(-) create mode 100644 lib/check_list.dart diff --git a/lib/add_form.dart b/lib/add_form.dart index 6a8a8f2..fe98f84 100644 --- a/lib/add_form.dart +++ b/lib/add_form.dart @@ -105,6 +105,7 @@ class _AddFormState extends State { _listLabel, isTemplate: type.index == 1, )); + // TODO implement template starting point Navigator.pop(context); // TODO replace route with checklist page } }, diff --git a/lib/box_checker.dart b/lib/box_checker.dart index fa924d8..789eb1b 100644 --- a/lib/box_checker.dart +++ b/lib/box_checker.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'add_form.dart'; +import 'check_list.dart'; import 'db_helper.dart'; import 'data_util.dart' as data; @@ -15,8 +16,10 @@ class BoxChecker extends StatelessWidget { return MaterialApp( title: 'Flutter Demo', theme: ThemeData.from(colorScheme: ColorScheme.dark()).copyWith( - canvasColor: Colors.black, - shadowColor: Colors.blueGrey, + canvasColor: Colors.blueGrey[900], + cardColor: Colors.grey[800], + scaffoldBackgroundColor: Colors.black12, + shadowColor: Colors.black38, colorScheme: ColorScheme.fromSwatch( primarySwatch: Colors.blue, ), @@ -80,10 +83,18 @@ class _MainListPageState extends State { child: ListView.builder( itemCount: lists.length, itemBuilder: (context, index) { - return ListTile( - onTap: () {}, - title: Text(lists[index].name), - subtitle: Text(lists[index].id.toString()), + return Card( + child: ListTile( + onTap: () { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => + CheckList(id: lists[index].id!))); + }, + title: Text(lists[index].name), + subtitle: Text(lists[index].id.toString()), + ), ); })), bottomNavigationBar: BottomNavigationBar( diff --git a/lib/check_list.dart b/lib/check_list.dart new file mode 100644 index 0000000..4b62808 --- /dev/null +++ b/lib/check_list.dart @@ -0,0 +1,83 @@ +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; + 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); + } + + @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'), + ), + body: ListView.builder( + itemCount: list.length, + itemBuilder: (context, index) { + return Card( + child: CheckboxListTile( + title: Text(list[index].text), + controlAffinity: ListTileControlAffinity.leading, + value: list[index].value, + onChanged: (listData != null && !listData!.isTemplate!) + ? ((value) { + _updateItem(list[index]); + setState(() { + list[index].value = value!; + }); + }) + : null, + ), + ); + }, + ), + floatingActionButton: FloatingActionButton( + onPressed: () { + // TODO implment adding items to lists + }, + child: const Icon(Icons.check_box), + tooltip: "Add Item", + ), + floatingActionButtonLocation: FloatingActionButtonLocation.endFloat, + ); + } +} diff --git a/lib/data_util.dart b/lib/data_util.dart index ffd2bc8..98a6a1e 100644 --- a/lib/data_util.dart +++ b/lib/data_util.dart @@ -33,13 +33,13 @@ class Check { var map = Map(); if (id != null) map["id"] = this.id; map["check_text"] = this.text; - map["value"] = this.value as int; + map["status"] = this.value ? 1 : 0; if (listID != null) map["list_id"] = this.listID! as int; return map; } static Check fromMap(Map map) { - return Check(map["check_text"], map["value"] == 1, + return Check(map["check_text"], map["status"] == 1, id: map["id"], listID: map["list_id"]); } } diff --git a/lib/db_helper.dart b/lib/db_helper.dart index 4e0352c..a1b12f8 100644 --- a/lib/db_helper.dart +++ b/lib/db_helper.dart @@ -59,9 +59,32 @@ class DBHelper { return db.query("List", where: "is_template = ?", whereArgs: [type.index]); } + Future>> getList(int id) async { + Database db = await database; + + return db.query("Item", where: "list_id = ?", whereArgs: [id]); + } + + Future>> getListData(int id) async { + Database db = await database; + + return db.query("List", where: "id = ?", whereArgs: [id]); + } + Future insertList(data.List l) async { Database db = await database; return db.insert("List", l.toMap()); } + + Future updateItem(data.Check item) async { + Database db = await database; + + return db.update( + "Item", + item.toMap(), + where: 'id = ?', + whereArgs: [item.id], + ); + } }