loading lists and templates from db

This commit is contained in:
andrei 2021-11-03 00:10:34 -04:00
parent 3d01120ce6
commit 188a8f43b9
3 changed files with 43 additions and 23 deletions

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,26 +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<List<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']);
}
Future<List<Map<String, dynamic>>> getAllTemplates() async {
Database db = await database;
return db.query("List", where: "is_template = ?", whereArgs: ['1']);
return db.query("List", where: "is_template = ?", whereArgs: [type.index]);
}
}