Compare commits
3 Commits
6bbb809a28
...
e793bc80a9
| Author | SHA1 | Date |
|---|---|---|
|
|
e793bc80a9 | |
|
|
188a8f43b9 | |
|
|
3d01120ce6 |
|
|
@ -1,15 +1,50 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'data_util.dart' as data;
|
import 'data_util.dart' as data;
|
||||||
|
import 'db_helper.dart';
|
||||||
|
|
||||||
class AddForm extends StatelessWidget {
|
class AddForm extends StatefulWidget {
|
||||||
AddForm({Key? key, required this.type}) : super(key: key);
|
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 _formKey = GlobalKey<FormState>();
|
||||||
|
|
||||||
final data.Page type;
|
final data.Page type;
|
||||||
int _templatChoice = -1;
|
int _templatChoice = -1;
|
||||||
|
List<data.List> templates = [];
|
||||||
|
|
||||||
final FocusNode _TemplateFocusNode = FocusNode();
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
|
|
@ -34,21 +69,17 @@ class AddForm extends StatelessWidget {
|
||||||
FocusScope.of(context).requestFocus(_TemplateFocusNode);
|
FocusScope.of(context).requestFocus(_TemplateFocusNode);
|
||||||
},
|
},
|
||||||
decoration: const InputDecoration(labelText: 'Label')),
|
decoration: const InputDecoration(labelText: 'Label')),
|
||||||
DropdownButtonFormField(
|
DropdownButtonFormField<int>(
|
||||||
focusNode: _TemplateFocusNode,
|
focusNode: _TemplateFocusNode,
|
||||||
value: _templatChoice,
|
value: _templatChoice,
|
||||||
onChanged: (value) {
|
onChanged: (value) {
|
||||||
setState() {
|
setState() {
|
||||||
_templatChoice = value as int;
|
_templatChoice = (value != null) ? value : -1;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
decoration:
|
decoration:
|
||||||
const InputDecoration(labelText: "Starting Template"),
|
const InputDecoration(labelText: "Starting Template"),
|
||||||
items: const [
|
items: generateDropdown(),
|
||||||
// TODO get templates from database
|
|
||||||
DropdownMenuItem<int>(child: Text("None"), value: -1),
|
|
||||||
DropdownMenuItem<int>(child: Text("1"), value: 0),
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'add_form.dart';
|
import 'add_form.dart';
|
||||||
|
import 'db_helper.dart';
|
||||||
import 'data_util.dart' as data;
|
import 'data_util.dart' as data;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
|
@ -30,6 +31,23 @@ class MainListPage extends StatefulWidget {
|
||||||
|
|
||||||
class _MainListPageState extends State<MainListPage> {
|
class _MainListPageState extends State<MainListPage> {
|
||||||
int _selectedPage = data.Page.lists.index;
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
|
|
@ -48,14 +66,20 @@ class _MainListPageState extends State<MainListPage> {
|
||||||
tooltip: 'Add List',
|
tooltip: 'Add List',
|
||||||
child: const Icon(Icons.add),
|
child: const Icon(Icons.add),
|
||||||
),
|
),
|
||||||
body: ListView.builder(itemBuilder: (context, index) {
|
body: ListView.builder(
|
||||||
return ListTile(); // TODO Implement tile rendering
|
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(
|
bottomNavigationBar: BottomNavigationBar(
|
||||||
currentIndex: _selectedPage,
|
currentIndex: _selectedPage,
|
||||||
onTap: (index) {
|
onTap: (index) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_selectedPage = index;
|
_selectedPage = index;
|
||||||
|
_loadData(data.Page.values[_selectedPage]);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
items: const [
|
items: const [
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,8 @@ class List {
|
||||||
}
|
}
|
||||||
|
|
||||||
static List fromMap(Map<String, dynamic> map) {
|
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) {
|
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"]);
|
listID: map["list_id"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import "package:sqflite/sqflite.dart";
|
import "package:sqflite/sqflite.dart";
|
||||||
import "package:sqflite/sqlite_api.dart";
|
import "package:sqflite/sqlite_api.dart";
|
||||||
import "package:path/path.dart";
|
import "package:path/path.dart";
|
||||||
|
import 'data_util.dart' as data;
|
||||||
|
|
||||||
class DBHelper {
|
class DBHelper {
|
||||||
DBHelper._();
|
DBHelper._();
|
||||||
|
|
@ -21,16 +22,16 @@ class DBHelper {
|
||||||
onCreate: (Database db, int version) {
|
onCreate: (Database db, int version) {
|
||||||
db.execute("""
|
db.execute("""
|
||||||
CREATE TABLE List(
|
CREATE TABLE List(
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY,
|
||||||
list_name TEXT
|
list_name TEXT,
|
||||||
is_template int2
|
is_template int2
|
||||||
)
|
)
|
||||||
""");
|
""");
|
||||||
db.execute("""
|
db.execute("""
|
||||||
CREATE TABLE Check(
|
CREATE TABLE Item(
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY,
|
||||||
check_text TEXT,
|
check_text TEXT,
|
||||||
value int2,
|
status int2,
|
||||||
list_id INTEGER
|
list_id INTEGER
|
||||||
)
|
)
|
||||||
""");
|
""");
|
||||||
|
|
@ -41,28 +42,20 @@ class DBHelper {
|
||||||
(1, 'test template', 1)
|
(1, 'test template', 1)
|
||||||
""");
|
""");
|
||||||
db.execute("""
|
db.execute("""
|
||||||
INSERT INTO CHECK(check_text, value, list_id)
|
INSERT INTO Item(id, check_text, status, list_id)
|
||||||
VALUES
|
VALUES
|
||||||
('test check', 1, 0),
|
(0, 'test item', 0, 0),
|
||||||
('test uncheck', 0, 0),
|
(1, 'test item', 1, 0),
|
||||||
('test check', 1, 1),
|
(2, 'test item', 0, 1),
|
||||||
('test uncheck', 0, 1)
|
(3, 'test item', 1, 1)
|
||||||
""");
|
""");
|
||||||
}, version: 1);
|
}, version: 1);
|
||||||
return database;
|
return database;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Map<String, dynamic>> getAllLists() async {
|
Future<List<Map<String, dynamic>>> getAllLists(data.Page type) async {
|
||||||
Database db = await database;
|
Database db = await database;
|
||||||
|
|
||||||
return db.query("List", where: "is_template = ?", whereArgs: ['0'])
|
return db.query("List", where: "is_template = ?", whereArgs: [type.index]);
|
||||||
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>;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue