Compare commits

..

3 Commits

Author SHA1 Message Date
andrei e793bc80a9 Form template selector generated from db 2021-11-03 00:55:29 -04:00
andrei 188a8f43b9 loading lists and templates from db 2021-11-03 00:10:34 -04:00
andrei 3d01120ce6 fixed get all functions 2021-11-02 22:58:45 -04:00
4 changed files with 83 additions and 34 deletions

View File

@ -1,15 +1,50 @@
import 'package:flutter/material.dart';
import 'data_util.dart' as data;
import 'db_helper.dart';
class AddForm extends StatelessWidget {
AddForm({Key? key, required this.type}) : super(key: key);
class AddForm extends StatefulWidget {
const AddForm({Key? key, required this.type}) : super(key: key);
final data.Page type;
@override
State<StatefulWidget> createState() => _AddFormState(type);
}
class _AddFormState extends State<AddForm> {
_AddFormState(this.type);
final _formKey = GlobalKey<FormState>();
final data.Page type;
int _templatChoice = -1;
List<data.List> templates = [];
final FocusNode _TemplateFocusNode = FocusNode();
void loadTemplates() async {
var rows = await DBHelper.dbHelper.getAllLists(data.Page.templates);
setState(() {
templates.clear();
for (var row in rows) {
var template = data.List.fromMap(row);
templates.add(template);
}
});
}
List<DropdownMenuItem<int>> generateDropdown() {
loadTemplates();
List<DropdownMenuItem<int>> items = [
DropdownMenuItem<int>(child: Text("None"), value: -1)
];
templates.asMap().forEach((index, value) {
items.add(DropdownMenuItem<int>(
child: Text(value.name),
value: index,
));
});
return items;
}
@override
Widget build(BuildContext context) {
return Scaffold(
@ -34,21 +69,17 @@ class AddForm extends StatelessWidget {
FocusScope.of(context).requestFocus(_TemplateFocusNode);
},
decoration: const InputDecoration(labelText: 'Label')),
DropdownButtonFormField(
DropdownButtonFormField<int>(
focusNode: _TemplateFocusNode,
value: _templatChoice,
onChanged: (value) {
setState() {
_templatChoice = value as int;
_templatChoice = (value != null) ? value : -1;
}
},
decoration:
const InputDecoration(labelText: "Starting Template"),
items: const [
// TODO get templates from database
DropdownMenuItem<int>(child: Text("None"), value: -1),
DropdownMenuItem<int>(child: Text("1"), value: 0),
],
items: generateDropdown(),
)
],
),

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'add_form.dart';
import 'db_helper.dart';
import 'data_util.dart' as data;
void main() {
@ -30,6 +31,23 @@ class MainListPage extends StatefulWidget {
class _MainListPageState extends State<MainListPage> {
int _selectedPage = data.Page.lists.index;
List<data.List> lists = [data.List(100, "test")];
void _loadData(data.Page listType) async {
lists.clear();
var res = await DBHelper.dbHelper.getAllLists(listType);
setState(() {
for (var row in res) lists.add(data.List.fromMap(row));
});
}
@override
void initState() {
_loadData(data.Page.lists);
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
@ -48,14 +66,20 @@ class _MainListPageState extends State<MainListPage> {
tooltip: 'Add List',
child: const Icon(Icons.add),
),
body: ListView.builder(itemBuilder: (context, index) {
return ListTile(); // TODO Implement tile rendering
}),
body: ListView.builder(
itemCount: lists.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(lists[index].name),
subtitle: Text(lists[index].id.toString()),
); // TODO Implement tile rendering
}),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _selectedPage,
onTap: (index) {
setState(() {
_selectedPage = index;
_loadData(data.Page.values[_selectedPage]);
});
},
items: const [

View File

@ -16,7 +16,8 @@ class List {
}
static List fromMap(Map<String, dynamic> map) {
return List(map["id"], map["list_name"], isTemplate: map["is_template"]);
return List(map["id"], map["list_name"],
isTemplate: map["is_template"] == 1);
}
}
@ -38,7 +39,7 @@ class Check {
}
static Check fromMap(Map<String, dynamic> map) {
return Check(map["id"], map["check_text"], map["value"],
return Check(map["id"], map["check_text"], map["value"] == 1,
listID: map["list_id"]);
}
}

View File

@ -1,6 +1,7 @@
import "package:sqflite/sqflite.dart";
import "package:sqflite/sqlite_api.dart";
import "package:path/path.dart";
import 'data_util.dart' as data;
class DBHelper {
DBHelper._();
@ -21,16 +22,16 @@ class DBHelper {
onCreate: (Database db, int version) {
db.execute("""
CREATE TABLE List(
id INTEGER PRIMARY KEY AUTOINCREMENT,
list_name TEXT
id INTEGER PRIMARY KEY,
list_name TEXT,
is_template int2
)
""");
db.execute("""
CREATE TABLE Check(
id INTEGER PRIMARY KEY AUTOINCREMENT,
CREATE TABLE Item(
id INTEGER PRIMARY KEY,
check_text TEXT,
value int2,
status int2,
list_id INTEGER
)
""");
@ -41,28 +42,20 @@ class DBHelper {
(1, 'test template', 1)
""");
db.execute("""
INSERT INTO CHECK(check_text, value, list_id)
INSERT INTO Item(id, check_text, status, list_id)
VALUES
('test check', 1, 0),
('test uncheck', 0, 0),
('test check', 1, 1),
('test uncheck', 0, 1)
(0, 'test item', 0, 0),
(1, 'test item', 1, 0),
(2, 'test item', 0, 1),
(3, 'test item', 1, 1)
""");
}, version: 1);
return database;
}
Future<Map<String, dynamic>> getAllLists() async {
Future<List<Map<String, dynamic>>> getAllLists(data.Page type) async {
Database db = await database;
return db.query("List", where: "is_template = ?", whereArgs: ['0'])
as Map<String, dynamic>;
}
Future<Map<String, dynamic>> getAllTemplates() async {
Database db = await database;
return db.query("List", where: "is_template = ?", whereArgs: ['1'])
as Map<String, dynamic>;
return db.query("List", where: "is_template = ?", whereArgs: [type.index]);
}
}