124 lines
3.9 KiB
Dart
124 lines
3.9 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'data_util.dart' as data;
|
|
import 'db_helper.dart';
|
|
|
|
class AddForm extends StatefulWidget {
|
|
const AddForm({Key? key, required this.type}) : super(key: key);
|
|
final data.Page type;
|
|
|
|
@override
|
|
State<StatefulWidget> createState() => _AddFormState();
|
|
}
|
|
|
|
class _AddFormState extends State<AddForm> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
int _templatChoice = -1;
|
|
String _listLabel = "";
|
|
String _listDesc = "";
|
|
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 = [
|
|
const 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(
|
|
appBar: AppBar(
|
|
title: Text(
|
|
'Add ' +
|
|
((widget.type == data.Page.lists)
|
|
? 'Check List'
|
|
: (widget.type == data.Page.templates)
|
|
? 'Template'
|
|
: ''),
|
|
),
|
|
),
|
|
body: Container(
|
|
margin: const EdgeInsets.all(10),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
children: [
|
|
TextFormField(
|
|
onChanged: (value) {
|
|
_listLabel = value;
|
|
},
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return 'Label is required';
|
|
}
|
|
return null;
|
|
},
|
|
textInputAction: TextInputAction.next,
|
|
onFieldSubmitted: (value) =>
|
|
_formKey.currentState!.validate(),
|
|
decoration: const InputDecoration(labelText: 'Label')),
|
|
TextFormField(
|
|
decoration:
|
|
const InputDecoration(labelText: 'Description(optional)'),
|
|
textInputAction: TextInputAction.next,
|
|
onChanged: (value) {
|
|
_listDesc = value;
|
|
},
|
|
),
|
|
DropdownButtonFormField<int>(
|
|
focusNode: _templateFocusNode,
|
|
value: _templatChoice,
|
|
onChanged: (value) => setState(() {
|
|
_templatChoice = value!;
|
|
}),
|
|
decoration:
|
|
const InputDecoration(labelText: "Starting Template"),
|
|
items: generateDropdown(),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
child: const Icon(Icons.save_alt),
|
|
onPressed: () async {
|
|
if (_formKey.currentState!.validate()) {
|
|
int id = await DBHelper.dbHelper.insertList(
|
|
data.List(
|
|
_listLabel,
|
|
isTemplate: widget.type.index == 1,
|
|
description: _listDesc,
|
|
),
|
|
);
|
|
if (_templatChoice >= 0 && _templatChoice < templates.length) {
|
|
DBHelper.dbHelper.copyList(templates[_templatChoice].id!, id);
|
|
}
|
|
Navigator.pop(context); // TODO replace route with checklist page
|
|
}
|
|
},
|
|
),
|
|
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
|
|
);
|
|
}
|
|
}
|