From 236be3000f10aebb1afac8bc2441dd48c280f69f Mon Sep 17 00:00:00 2001 From: andrei Date: Wed, 3 Nov 2021 14:52:39 -0400 Subject: [PATCH] adding new lists --- lib/add_form.dart | 25 +++++++++++++++++++++---- lib/box_checker.dart | 30 +++++++++++++++++------------- lib/data_util.dart | 24 ++++++++++++------------ lib/db_helper.dart | 6 ++++++ 4 files changed, 56 insertions(+), 29 deletions(-) diff --git a/lib/add_form.dart b/lib/add_form.dart index 386faa9..6a8a8f2 100644 --- a/lib/add_form.dart +++ b/lib/add_form.dart @@ -16,6 +16,7 @@ class _AddFormState extends State { final data.Page type; int _templatChoice = -1; + String _listLabel = ""; List templates = []; final FocusNode _TemplateFocusNode = FocusNode(); @@ -65,8 +66,19 @@ class _AddFormState extends State { child: Column( children: [ TextFormField( + onChanged: (value) { + _listLabel = value; + }, + validator: (value) { + if (value == null || value.isEmpty) { + return 'Label is required'; + } + return null; + }, onFieldSubmitted: (value) { - FocusScope.of(context).requestFocus(_TemplateFocusNode); + if (_formKey.currentState!.validate()) { + FocusScope.of(context).requestFocus(_TemplateFocusNode); + } }, decoration: const InputDecoration(labelText: 'Label')), DropdownButtonFormField( @@ -87,9 +99,14 @@ class _AddFormState extends State { ), floatingActionButton: FloatingActionButton( child: Icon(Icons.save_alt), - onPressed: () { - // TODO implement adding list to database - Navigator.pop(context); // TODO replace route with checklist page + onPressed: () async { + if (_formKey.currentState!.validate()) { + DBHelper.dbHelper.insertList(data.List( + _listLabel, + isTemplate: type.index == 1, + )); + Navigator.pop(context); // TODO replace route with checklist page + } }, ), floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, diff --git a/lib/box_checker.dart b/lib/box_checker.dart index 980c883..fa924d8 100644 --- a/lib/box_checker.dart +++ b/lib/box_checker.dart @@ -39,7 +39,7 @@ class _MainListPageState extends State { int _selectedPage = data.Page.lists.index; List lists = []; - void _loadData(data.Page listType) async { + Future _loadData(data.Page listType) async { lists.clear(); var res = await DBHelper.dbHelper.getAllLists(listType); setState(() { @@ -66,22 +66,26 @@ class _MainListPageState extends State { floatingActionButton: FloatingActionButton( onPressed: () { Navigator.push( - context, - MaterialPageRoute( - builder: (context) => - AddForm(type: data.Page.values[_selectedPage]))); + context, + MaterialPageRoute( + builder: (context) => + AddForm(type: data.Page.values[_selectedPage]))) + .then((value) => _loadData(data.Page.values[_selectedPage])); }, tooltip: 'Add List', child: const Icon(Icons.add), ), - body: ListView.builder( - itemCount: lists.length, - itemBuilder: (context, index) { - return ListTile( - title: Text(lists[index].name), - subtitle: Text(lists[index].id.toString()), - ); - }), + body: RefreshIndicator( + onRefresh: () => _loadData(data.Page.values[_selectedPage]), + child: ListView.builder( + itemCount: lists.length, + itemBuilder: (context, index) { + return ListTile( + onTap: () {}, + title: Text(lists[index].name), + subtitle: Text(lists[index].id.toString()), + ); + })), bottomNavigationBar: BottomNavigationBar( elevation: 4, currentIndex: _selectedPage, diff --git a/lib/data_util.dart b/lib/data_util.dart index 140a38f..ffd2bc8 100644 --- a/lib/data_util.dart +++ b/lib/data_util.dart @@ -1,45 +1,45 @@ enum Page { lists, templates } class List { - List(this.id, this.name, {this.isTemplate}); + List(this.name, {this.id, this.isTemplate}); - int id; + int? id; String name; bool? isTemplate; Map toMap() { var map = Map(); - map["id"] = this.id; + if (id != null) map["id"] = this.id; map["list_name"] = this.name; - if (isTemplate != null) map["is_template"] = this.isTemplate as int; + if (isTemplate != null) map["is_template"] = isTemplate! ? 1 : 0; return map; } static List fromMap(Map map) { - return List(map["id"], map["list_name"], - isTemplate: map["is_template"] == 1); + return List(map["list_name"], + id: map["id"], isTemplate: map["is_template"] == 1); } } class Check { - Check(this.id, this.text, this.value, {this.listID}); + Check(this.text, this.value, {this.id, this.listID}); - int id; + int? id; String text; bool value; int? listID; Map toMap() { var map = Map(); - map["id"] = this.id; + if (id != null) map["id"] = this.id; map["check_text"] = this.text; map["value"] = this.value as int; - if (listID != null) map["list_id"] = this.listID as int; + if (listID != null) map["list_id"] = this.listID! as int; return map; } static Check fromMap(Map map) { - return Check(map["id"], map["check_text"], map["value"] == 1, - listID: map["list_id"]); + return Check(map["check_text"], map["value"] == 1, + id: map["id"], listID: map["list_id"]); } } diff --git a/lib/db_helper.dart b/lib/db_helper.dart index d7aa7fa..4e0352c 100644 --- a/lib/db_helper.dart +++ b/lib/db_helper.dart @@ -58,4 +58,10 @@ class DBHelper { return db.query("List", where: "is_template = ?", whereArgs: [type.index]); } + + Future insertList(data.List l) async { + Database db = await database; + + return db.insert("List", l.toMap()); + } }