Files
authsec_flutter/lib/screens/dynamic_form/UpdatedynamicForm.dart
T

307 lines
13 KiB
Dart
Raw Normal View History

2026-09-30 09:13:45 +05:30
import 'package:flutter/material.dart';
2026-10-03 12:12:31 +05:30
import '../../l10n/app_localizations.dart';
2026-09-30 09:13:45 +05:30
import '../../providers/token_manager.dart';
import 'dynamic_form_service.dart';
class EditForm extends StatefulWidget {
final Map<String, dynamic> formData;
EditForm({required this.formData});
@override
_EditFormState createState() => _EditFormState();
}
class _EditFormState extends State<EditForm> {
TextEditingController formNameController = TextEditingController();
TextEditingController formDescController = TextEditingController();
TextEditingController relatedToController = TextEditingController();
TextEditingController pageEventController = TextEditingController();
TextEditingController buttonCaptionController = TextEditingController();
final DynamicForApiService apiService = DynamicForApiService();
List<dynamic> components = []; // List to store table data
List<dynamic> relatedToFixedDropdown = ['Menu', 'Related To',];
List<dynamic> pageEventFixedDropdown = ['OnClick', 'OnBlur',];
List<dynamic> typeFixedDropdown = ['text', 'dropdown','date','checkbox','textarea','togglebutton'];
List<dynamic> mappingFixedDropdown = ['TEXTFIELD1', 'TEXTFIELD2','TEXTFIELD3','TEXTFIELD4','TEXTFIELD5','TEXTFIELD6','TEXTFIELD7','TEXTFIELD8','TEXTFIELD9','TEXTFIELD10','TEXTFIELD11','TEXTFIELD12','TEXTFIELD13','TEXTFIELD14','TEXTFIELD15','TEXTFIELD16','TEXTFIELD17','TEXTFIELD18','TEXTFIELD19','TEXTFIELD20','TEXTFIELD21','TEXTFIELD22','TEXTFIELD23',
'TEXTFIELD24','TEXTFIELD25','TEXTFIELD26','LONGTEXT1','LONGTEXT2','LONGTEXT3','LONGTEXT4',];
List<dynamic> truefalseFixedDropdown = ['true', 'false',];
@override
void initState() {
super.initState();
// Initialize text controllers with the data from the selected row
formNameController.text = widget.formData['form_name'] ?? '';
formDescController.text = widget.formData['form_desc'] ?? '';
relatedToController.text = widget.formData['related_to'] ?? '';
pageEventController.text = widget.formData['page_event'] ?? '';
buttonCaptionController.text = widget.formData['button_caption'] ?? '';
// Initialize the components data, or you can load it from formData['components'].
// For this example, we'll use an empty list.
components = widget.formData['components'] ?? [];
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2026-10-03 12:12:31 +05:30
title: Text(AppLocalizations.of(context)!.editForm),
2026-09-30 09:13:45 +05:30
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
TextFormField(
controller: formNameController,
2026-10-03 12:12:31 +05:30
decoration: InputDecoration(
labelText:
AppLocalizations.of(context)!.formName),
2026-09-30 09:13:45 +05:30
),
TextFormField(
controller: formDescController,
2026-10-03 12:12:31 +05:30
decoration: InputDecoration(
labelText: AppLocalizations.of(context)!
.formDescription),
2026-09-30 09:13:45 +05:30
),
DropdownButtonFormField<String>(
value: relatedToController.text,
2026-10-03 12:12:31 +05:30
decoration: InputDecoration(
labelText: AppLocalizations.of(context)!
.selectRelatedTo),
2026-09-30 09:13:45 +05:30
items: [
...relatedToFixedDropdown.map<DropdownMenuItem<String>>(
(item) {
return DropdownMenuItem<String>(
value: item.toString(),
child: Text(item),
);
},
),
],
onChanged: (value) {
setState(() {
relatedToController.text = value!;
});
},
),
DropdownButtonFormField<String>(
value: pageEventController.text,
2026-10-03 12:12:31 +05:30
decoration: InputDecoration(
labelText: AppLocalizations.of(context)!
.selectPageEvent),
2026-09-30 09:13:45 +05:30
items: [
...pageEventFixedDropdown.map<DropdownMenuItem<String>>(
(item) {
return DropdownMenuItem<String>(
value: item.toString(),
child: Text(item),
);
},
),
],
onChanged: (value) {
setState(() {
pageEventController.text = value!;
});
},
),
TextFormField(
controller: buttonCaptionController,
2026-10-03 12:12:31 +05:30
decoration: InputDecoration(
labelText: AppLocalizations.of(context)!
.buttonCaption),
2026-09-30 09:13:45 +05:30
),
2026-10-03 12:12:31 +05:30
Text(AppLocalizations.of(context)!.componentsDetails),
2026-09-30 09:13:45 +05:30
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: DataTable(
2026-10-03 12:12:31 +05:30
columns: <DataColumn>[
DataColumn(
label: Text(
AppLocalizations.of(context)!.colLabel)),
DataColumn(
label: Text(
AppLocalizations.of(context)!.colType)),
DataColumn(
label: Text(
AppLocalizations.of(context)!.colMapping)),
DataColumn(
label: Text(
AppLocalizations.of(context)!.colMandatory)),
DataColumn(
label: Text(
AppLocalizations.of(context)!.colReadonly)),
DataColumn(
label: Text(
AppLocalizations.of(context)!.colDropValues)),
DataColumn(
label: Text(
AppLocalizations.of(context)!.colSP)),
DataColumn(
label: Text(AppLocalizations.of(context)!
.colActions),
numeric: false), // Add Actions column
2026-09-30 09:13:45 +05:30
],
rows: components.asMap().entries.map((entry) {
final index = entry.key;
final component = entry.value;
return DataRow(
cells: <DataCell>[
DataCell(TextField(
controller: TextEditingController(text: component['label'] ?? ''),
onChanged: (value){
component['label']=value;
},
)),
DataCell(
DropdownButtonFormField<String>(
value: component['type'],
decoration:
const InputDecoration(labelText: 'Select Type'),
items: [
...typeFixedDropdown.map<DropdownMenuItem<String>>(
(item) {
return DropdownMenuItem<String>(
value: item.toString(),
child: Text(item),
);
},
),
],
onChanged: (value) {
setState(() {
component['type'] = value;
});
},
),
),
DataCell(
DropdownButtonFormField<String>(
value: component['mapping'],
decoration:
const InputDecoration(labelText: 'Select Mapping'),
items: [
...mappingFixedDropdown.map<DropdownMenuItem<String>>(
(item) {
return DropdownMenuItem<String>(
value: item.toString(),
child: Text(item),
);
},
),
],
onChanged: (value) {
setState(() {
component['mapping'] = value;
});
},
),
),
DataCell(TextField(
controller: TextEditingController(text: component['mandatory'] ?? ''),
onChanged: (value){
component['mandatory']=value;
},
)),
DataCell(
DropdownButtonFormField<String>(
value: component['readonly'],
decoration:
const InputDecoration(labelText: 'Select Read Only'),
items: [
...truefalseFixedDropdown.map<DropdownMenuItem<String>>(
(item) {
return DropdownMenuItem<String>(
value: item.toString(),
child: Text(item),
);
},
),
],
onChanged: (value) {
setState(() {
component['readonly'] = value;
});
},
),
),
DataCell(TextField(
controller: TextEditingController(text: component['drop_values'] ?? ''),
onChanged: (value){
component['drop_values']=value;
},
)),
DataCell(TextField(
controller: TextEditingController(text: component['sp'] ?? ''),
onChanged: (value){
component['sp']=value;
},
)),
DataCell(
ElevatedButton(
onPressed: () {
setState(() {
components.removeAt(index);
});
},
2026-10-03 12:12:31 +05:30
child: Text(
AppLocalizations.of(context)!.delete),
2026-09-30 09:13:45 +05:30
),
),
],
);
}).toList(),
),
),
ElevatedButton(
onPressed: () {
// Add a new row to the components table
components.add({
'label': '',
'type': null,
'mapping': null,
'mandatory': '',
'readonly': null,
'drop_values': '',
'sp': '',
});
setState(() {}); // Refresh the UI to show the new row
},
2026-10-03 12:12:31 +05:30
child: Text(AppLocalizations.of(context)!.addRow),
2026-09-30 09:13:45 +05:30
),
ElevatedButton(
onPressed: () async {
// Save the updated data back to the original data list
widget.formData['form_name'] = formNameController.text;
widget.formData['form_desc'] = formDescController.text;
widget.formData['related_to'] = relatedToController.text;
widget.formData['page_event'] = pageEventController.text;
widget.formData['button_caption'] = buttonCaptionController.text;
widget.formData['components'] = components;
final token = await TokenManager.getToken();
apiService.updateDynamicForm(token!, widget.formData['form_id'], widget.formData);
Navigator.of(context).pop();
},
2026-10-03 12:12:31 +05:30
child: Text(AppLocalizations.of(context)!.update),
2026-09-30 09:13:45 +05:30
),
],
),
),
),
);
}
}