From 284c59d8f84157dc910788c48c0cf08d6a3574eb Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 16 Jul 2021 01:04:55 +1000 Subject: [PATCH] Construct a custom dialog --- lib/api_form.dart | 85 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 65 insertions(+), 20 deletions(-) diff --git a/lib/api_form.dart b/lib/api_form.dart index 186dff56..db081019 100644 --- a/lib/api_form.dart +++ b/lib/api_form.dart @@ -1,10 +1,11 @@ -import 'dart:convert'; - import 'package:InvenTree/api.dart'; import 'package:InvenTree/widget/dialogs.dart'; import 'package:InvenTree/widget/fields.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:one_context/one_context.dart'; +import 'package:InvenTree/l10.dart'; + /* * Class that represents a single "form field", @@ -70,7 +71,6 @@ class APIFormField { initialValue: value ?? '', onSaved: (val) { data["value"] = val; - print("${name} -> ${val}"); }, validator: (value) { @@ -88,7 +88,6 @@ class APIFormField { initial: value, onSaved: (val) { data['value'] = val; - print("${name} -> ${val}"); }, ); } @@ -125,20 +124,20 @@ Map extractFields(dynamic options) { * @param method is the HTTP method to use to send the form data to the server (e.g. POST / PATCH) */ -Future launchApiForm(String title, String url, Map fields, {Map modelData = const {}, String method = "PATCH"}) async { +Future launchApiForm(String title, String url, Map fields, {Map modelData = const {}, String method = "PATCH"}) async { dynamic options = await InvenTreeAPI().options(url); // null response from server if (options == null) { - return false; + return; } var availableFields = extractFields(options); if (availableFields.isEmpty) { print("Empty fields {} returned from ${url}"); - return false; + return; } // Construct a list of APIFormField objects @@ -158,15 +157,11 @@ Future launchApiForm(String title, String url, Map fields // Override defined field parameters, if provided for (String key in localField.keys) { - // Special consideration + // Special consideration must be taken here! if (key == "filters") { - + // TODO: Custom filter updating } else { - String? val = localField[key]; - - if (val != null) { - remoteField[key] = val; - } + remoteField[key] = localField[key]; } } @@ -186,14 +181,64 @@ Future launchApiForm(String title, String url, Map fields List widgets = []; for (var ff in formFields) { - widgets.add(ff.constructField()); + if (!ff.hidden) { + widgets.add(ff.constructField()); + } } - final formKey = new GlobalKey(); + final _formKey = new GlobalKey(); - showFormDialog(title, fields: widgets, key: formKey, callback: () { - print("submitted, I guess?"); - }); + OneContext().showDialog( + builder: (BuildContext context) { + return AlertDialog( + title: Text(title), + actions: [ + // Cancel button + TextButton( + child: Text(L10().cancel), + onPressed: () { + Navigator.pop(context); + }, + ), + // Save button + TextButton( + child: Text(L10().save), + onPressed: () { + // Validate the form + if (_formKey.currentState!.validate()) { + _formKey.currentState!.save(); - return true; + // Package up the form data + Map formData = {}; + + for (var field in formFields) { + formData[field.name] = field.value.toString(); + } + + print(formData.toString()); + + // Send the data to the server + + // Respond to error message + + // Dismiss the form + + } + }, + ) + ], + content: Form( + key: _formKey, + child: SingleChildScrollView( + child: Column( + mainAxisSize: MainAxisSize.min, + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.start, + children: widgets + ) + ) + ) + ); + } + ); } \ No newline at end of file