adding new lists

This commit is contained in:
andrei 2021-11-03 14:52:39 -04:00
parent 47402efd64
commit 236be3000f
4 changed files with 56 additions and 29 deletions

View File

@ -16,6 +16,7 @@ class _AddFormState extends State<AddForm> {
final data.Page type; final data.Page type;
int _templatChoice = -1; int _templatChoice = -1;
String _listLabel = "";
List<data.List> templates = []; List<data.List> templates = [];
final FocusNode _TemplateFocusNode = FocusNode(); final FocusNode _TemplateFocusNode = FocusNode();
@ -65,8 +66,19 @@ class _AddFormState extends State<AddForm> {
child: Column( child: Column(
children: [ children: [
TextFormField( TextFormField(
onChanged: (value) {
_listLabel = value;
},
validator: (value) {
if (value == null || value.isEmpty) {
return 'Label is required';
}
return null;
},
onFieldSubmitted: (value) { onFieldSubmitted: (value) {
FocusScope.of(context).requestFocus(_TemplateFocusNode); if (_formKey.currentState!.validate()) {
FocusScope.of(context).requestFocus(_TemplateFocusNode);
}
}, },
decoration: const InputDecoration(labelText: 'Label')), decoration: const InputDecoration(labelText: 'Label')),
DropdownButtonFormField<int>( DropdownButtonFormField<int>(
@ -87,9 +99,14 @@ class _AddFormState extends State<AddForm> {
), ),
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
child: Icon(Icons.save_alt), child: Icon(Icons.save_alt),
onPressed: () { onPressed: () async {
// TODO implement adding list to database if (_formKey.currentState!.validate()) {
Navigator.pop(context); // TODO replace route with checklist page DBHelper.dbHelper.insertList(data.List(
_listLabel,
isTemplate: type.index == 1,
));
Navigator.pop(context); // TODO replace route with checklist page
}
}, },
), ),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,

View File

@ -39,7 +39,7 @@ class _MainListPageState extends State<MainListPage> {
int _selectedPage = data.Page.lists.index; int _selectedPage = data.Page.lists.index;
List<data.List> lists = []; List<data.List> lists = [];
void _loadData(data.Page listType) async { Future<void> _loadData(data.Page listType) async {
lists.clear(); lists.clear();
var res = await DBHelper.dbHelper.getAllLists(listType); var res = await DBHelper.dbHelper.getAllLists(listType);
setState(() { setState(() {
@ -66,22 +66,26 @@ class _MainListPageState extends State<MainListPage> {
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
onPressed: () { onPressed: () {
Navigator.push( Navigator.push(
context, context,
MaterialPageRoute( MaterialPageRoute(
builder: (context) => builder: (context) =>
AddForm(type: data.Page.values[_selectedPage]))); AddForm(type: data.Page.values[_selectedPage])))
.then((value) => _loadData(data.Page.values[_selectedPage]));
}, },
tooltip: 'Add List', tooltip: 'Add List',
child: const Icon(Icons.add), child: const Icon(Icons.add),
), ),
body: ListView.builder( body: RefreshIndicator(
itemCount: lists.length, onRefresh: () => _loadData(data.Page.values[_selectedPage]),
itemBuilder: (context, index) { child: ListView.builder(
return ListTile( itemCount: lists.length,
title: Text(lists[index].name), itemBuilder: (context, index) {
subtitle: Text(lists[index].id.toString()), return ListTile(
); onTap: () {},
}), title: Text(lists[index].name),
subtitle: Text(lists[index].id.toString()),
);
})),
bottomNavigationBar: BottomNavigationBar( bottomNavigationBar: BottomNavigationBar(
elevation: 4, elevation: 4,
currentIndex: _selectedPage, currentIndex: _selectedPage,

View File

@ -1,45 +1,45 @@
enum Page { lists, templates } enum Page { lists, templates }
class List { class List {
List(this.id, this.name, {this.isTemplate}); List(this.name, {this.id, this.isTemplate});
int id; int? id;
String name; String name;
bool? isTemplate; bool? isTemplate;
Map<String, dynamic> toMap() { Map<String, dynamic> toMap() {
var map = Map<String, dynamic>(); var map = Map<String, dynamic>();
map["id"] = this.id; if (id != null) map["id"] = this.id;
map["list_name"] = this.name; 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; return map;
} }
static List fromMap(Map<String, dynamic> map) { static List fromMap(Map<String, dynamic> map) {
return List(map["id"], map["list_name"], return List(map["list_name"],
isTemplate: map["is_template"] == 1); id: map["id"], isTemplate: map["is_template"] == 1);
} }
} }
class Check { 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; String text;
bool value; bool value;
int? listID; int? listID;
Map<String, dynamic> toMap() { Map<String, dynamic> toMap() {
var map = Map<String, dynamic>(); var map = Map<String, dynamic>();
map["id"] = this.id; if (id != null) map["id"] = this.id;
map["check_text"] = this.text; map["check_text"] = this.text;
map["value"] = this.value as int; 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; return map;
} }
static Check fromMap(Map<String, dynamic> map) { static Check fromMap(Map<String, dynamic> map) {
return Check(map["id"], map["check_text"], map["value"] == 1, return Check(map["check_text"], map["value"] == 1,
listID: map["list_id"]); id: map["id"], listID: map["list_id"]);
} }
} }

View File

@ -58,4 +58,10 @@ class DBHelper {
return db.query("List", where: "is_template = ?", whereArgs: [type.index]); return db.query("List", where: "is_template = ?", whereArgs: [type.index]);
} }
Future<int> insertList(data.List l) async {
Database db = await database;
return db.insert("List", l.toMap());
}
} }