mirror of
https://git.meshkee.com/applications/cssd.git
synced 2026-10-10 18:58:54 +03:30
223 lines
7.2 KiB
Dart
Executable File
223 lines
7.2 KiB
Dart
Executable File
import 'package:flutter/material.dart';
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:random_string/random_string.dart';
|
|
|
|
import '../../config/routes.dart';
|
|
import '../../providers/input.dart';
|
|
import '../../providers/test_results.dart';
|
|
import '../../widgets/TestResultScreens/Add/form_creator.dart';
|
|
|
|
class FormPage extends StatefulWidget {
|
|
final String? name;
|
|
final String? uniqueCode;
|
|
final String? testResultId;
|
|
final String? testTypeId;
|
|
|
|
FormPage({this.name, this.uniqueCode, this.testResultId, this.testTypeId});
|
|
|
|
@override
|
|
_FormPageState createState() => _FormPageState();
|
|
}
|
|
|
|
class _FormPageState extends State<FormPage> {
|
|
bool _isInit = true;
|
|
bool _isLoading = false;
|
|
bool _isLoadingButton = false;
|
|
bool isAdding = false;
|
|
final _form = GlobalKey<FormState>();
|
|
Map<String, dynamic> values = {};
|
|
List<Map<String, dynamic>> formInputs = [];
|
|
|
|
void _saveForm() {
|
|
final batchId = randomString(10);
|
|
formatData(formInputs);
|
|
var prvObject = Provider.of<TestResultsProvider>(context, listen: false);
|
|
if (this.widget.testResultId != null && this.widget.testResultId != '') {
|
|
values['_method'] = 'put';
|
|
TestResultsProvider.updateTestResult(this.widget.testResultId!, values)
|
|
.then((_) {
|
|
Navigator.of(context).pop();
|
|
});
|
|
} else {
|
|
setState(() {
|
|
_isLoadingButton = true;
|
|
});
|
|
prvObject.createTestResult(values, batchId).then(
|
|
(result) {
|
|
setState(() {
|
|
_isLoadingButton = false;
|
|
});
|
|
if (result == 'done') {
|
|
Navigator.of(context).popAndPushNamed(Routes.testResultScreen);
|
|
Fluttertoast.showToast(
|
|
msg: "Test added.",
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
gravity: ToastGravity.BOTTOM,
|
|
);
|
|
} else {
|
|
Fluttertoast.showToast(
|
|
msg: result,
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
gravity: ToastGravity.BOTTOM,
|
|
);
|
|
}
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void didChangeDependencies() {
|
|
if (_isInit) {
|
|
setState(() {
|
|
_isLoading = true;
|
|
});
|
|
var providerObject = Provider.of<TestResultsProvider>(context);
|
|
String testTypeName = Provider.of<TestResultsProvider>(context).testType;
|
|
String testCode = Provider.of<TestResultsProvider>(context).testCode;
|
|
providerObject.catchImages.clear();
|
|
Provider.of<Inputs>(context, listen: false)
|
|
.getInputs(
|
|
testResultId: '',
|
|
name: testTypeName,
|
|
uniqueCode: testCode,
|
|
testTypeId: '',
|
|
)
|
|
.then((_) {
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
});
|
|
}
|
|
_isInit = false;
|
|
super.didChangeDependencies();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return WillPopScope(
|
|
onWillPop: () async {
|
|
Navigator.pushReplacementNamed(
|
|
context,
|
|
Routes.testResultAddScreen,
|
|
);
|
|
return true;
|
|
},
|
|
child: SafeArea(
|
|
child: Scaffold(
|
|
appBar: AppBar(
|
|
centerTitle: true,
|
|
title: Text(
|
|
'Add Test Result',
|
|
style: TextStyle(
|
|
color: Colors.white,
|
|
),
|
|
),
|
|
backgroundColor: Colors.black,
|
|
actions: <Widget>[
|
|
_isLoadingButton
|
|
? Center(
|
|
child: CircularProgressIndicator(
|
|
color: Colors.white,
|
|
),
|
|
)
|
|
: IconButton(
|
|
icon: Icon(
|
|
Icons.save,
|
|
color: Colors.white,
|
|
),
|
|
onPressed: () {
|
|
_saveForm();
|
|
},
|
|
)
|
|
],
|
|
leading: IconButton(
|
|
icon: Icon(
|
|
Icons.keyboard_arrow_left,
|
|
color: Colors.white,
|
|
size: 35,
|
|
),
|
|
onPressed: () {
|
|
Navigator.of(context).popAndPushNamed(Routes.testResultScreen);
|
|
},
|
|
),
|
|
),
|
|
body: SingleChildScrollView(
|
|
child: Column(
|
|
children: <Widget>[
|
|
_isLoading
|
|
? Center(
|
|
child: CircularProgressIndicator(
|
|
color: Colors.black,
|
|
),
|
|
)
|
|
: Form(
|
|
key: _form,
|
|
child: Consumer<Inputs>(
|
|
builder: (context, inputs, _) {
|
|
formInputs = inputs.inputItems;
|
|
print(formInputs);
|
|
return formInputs.length != 0
|
|
? FormCreator(
|
|
form: formInputs,
|
|
onChanged: (value) {
|
|
formInputs = value;
|
|
},
|
|
)
|
|
: Center(
|
|
child: Text('Something went wrong!!!'),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
// https://www.cssd-doc.com/api/GetInfo
|
|
// InkWell(
|
|
// onTap: () {
|
|
// _saveForm();
|
|
// },
|
|
// child: Container(
|
|
// padding: EdgeInsets.symmetric(horizontal: 60, vertical: 15),
|
|
// margin: EdgeInsets.symmetric(vertical: 20, horizontal: 50),
|
|
// decoration: BoxDecoration(
|
|
// gradient: LinearGradient(colors: [Colors.blue, Colors.green]),
|
|
// borderRadius: BorderRadius.circular(10),
|
|
// ),
|
|
// child: Text(
|
|
// 'Save',
|
|
// style: TextStyle(color: Colors.white, fontSize: 17),
|
|
// textAlign: TextAlign.center,
|
|
// ),
|
|
// ),
|
|
// ),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
void formatData(inputs) {
|
|
for (var input in inputs) {
|
|
if (input['type'] != 'Form') {
|
|
if (input['key'] == 'result') {
|
|
values[input["key"]] = input["value"] ? "Accept" : "Reject";
|
|
// print('${input["key"]} : ${input["value"] ? "Accept" : "Reject"}');
|
|
} else if (input['key'] == 'date') {
|
|
var date = input["value"];
|
|
|
|
values[input["key"]] = "${date.year}-${date.month}-${date.day}";
|
|
} else {
|
|
values[input["key"]] = input["value"];
|
|
// print('${input["key"]} : ${input["value"]}');
|
|
}
|
|
}
|
|
if (input['children'] != null && input['children'].isNotEmpty) {
|
|
formatData(input['children']);
|
|
}
|
|
}
|
|
// print(values);
|
|
}
|
|
}
|