diff --git a/lib/add_form.dart b/lib/add_form.dart index b8fd62b..386faa9 100644 --- a/lib/add_form.dart +++ b/lib/add_form.dart @@ -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 createState() => _AddFormState(type); +} + +class _AddFormState extends State { + _AddFormState(this.type); final _formKey = GlobalKey(); final data.Page type; int _templatChoice = -1; + 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> generateDropdown() { + loadTemplates(); + List> items = [ + DropdownMenuItem(child: Text("None"), value: -1) + ]; + templates.asMap().forEach((index, value) { + items.add(DropdownMenuItem( + 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( 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(child: Text("None"), value: -1), - DropdownMenuItem(child: Text("1"), value: 0), - ], + items: generateDropdown(), ) ], ),