Form template selector generated from db

This commit is contained in:
andrei 2021-11-03 00:55:29 -04:00
parent 188a8f43b9
commit e793bc80a9
1 changed files with 40 additions and 9 deletions

View File

@ -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),
],
) )
], ],
), ),