AddEditColumnDialog function

dynamic AddEditColumnDialog(
  1. dynamic context,
  2. dynamic onColumnEdit,
  3. dynamic onColumnDelete
)

Implementation

AddEditColumnDialog(context, onColumnEdit, onColumnDelete) {
  return showDialog(
      barrierDismissible: false,
      context: context,
      builder: (context) => AlertDialog(
              content: GetBuilder<TableController>(builder: (controller) {
            List<TextEditingController> columnControllers = List.generate(
                controller.columns.length,
                (colindex) =>
                    TextEditingController(text: controller.columns[colindex]));

            return SingleChildScrollView(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                    children: [
                      Center(
                          child: Text("Update Column",
                              style: GoogleFonts.roboto(
                                  fontWeight: FontWeight.bold, fontSize: 16))),
                      Column(
                          children: List.generate(
                              columnControllers.length,
                              (index) => Row(
                                    children: [
                                      Container(
                                        width: 300,
                                        child: CustomField(
                                          keyboardType: TextInputType.text,
                                          controller: columnControllers[index],
                                          FieldName: controller.columns[index],
                                          readOnly: controller
                                                  .deletedColumns.isNotEmpty
                                              ? controller.deletedColumns
                                                      .contains(index)
                                                  ? true
                                                  : false
                                              : false,
                                          hintText: "Column Name",
                                        ),
                                      ),
                                      IconButton(
                                          onPressed: () {
                                            // if current index is not in controller.deletedColumns, then add it to controller.deletedColumns
                                            if (controller
                                                .deletedColumns.isEmpty) {
                                              controller
                                                  .addToDeletedColumns(index);
                                            } else {
                                              if (!controller.deletedColumns
                                                  .contains(index)) {
                                                controller
                                                    .addToDeletedColumns(index);
                                              }
                                              // if current index is in controller.deletedColumns, then remove it from controller.deletedColumns
                                              else {
                                                controller
                                                    .removeFromDeletedColumns(
                                                        index);
                                              }
                                            }
                                          },
                                          icon: controller
                                                  .deletedColumns.isEmpty
                                              ? const Icon(Icons.delete)
                                              : controller.deletedColumns
                                                      .contains(index)
                                                  ? const Icon(
                                                      Icons.delete_forever)
                                                  : const Icon(Icons.delete))
                                    ],
                                  ))),
                    ],
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      ElevatedButton(
                          onPressed: () {
                            for (var i = 0; i < columnControllers.length; i++) {
                              if (columnControllers[i].text.isEmpty) {
                                ScaffoldMessenger.of(context).showSnackBar(
                                    const SnackBar(
                                        backgroundColor: Colors.red,
                                        content: Text("Enter Column Name")));
                                return;
                              }
                            }
                            controller.editColumn(List.generate(
                                columnControllers.length,
                                (index) => columnControllers[index].text));
                            if (onColumnEdit != null) {
                              onColumnEdit(List.generate(
                                  columnControllers.length,
                                  (index) => columnControllers[index].text));
                            }
                            // when deletedColumns is not empty
                            if (controller.deletedColumns.isNotEmpty) {
                              for (var i = 0;
                                  i < controller.deletedColumns.length;
                                  i++) {
                                controller
                                    .removeColumn(controller.deletedColumns[i]);
                                if (onColumnDelete != null) {
                                  onColumnDelete(i);
                                }
                              }
                            }

                            Get.back();
                            return;
                          },
                          style: ElevatedButton.styleFrom(
                              shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(10.0)),
                              backgroundColor: AppColors.primaryColor),
                          child: Text("Update",
                              style: GoogleFonts.roboto(color: Colors.white))),
                      ElevatedButton(
                          onPressed: () {
                            Get.back();
                          },
                          style: ElevatedButton.styleFrom(
                              shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(10.0)),
                              backgroundColor: AppColors.primaryColor),
                          child: Text("Cancel",
                              style: GoogleFonts.roboto(color: Colors.white)))
                    ],
                  )
                ],
              ),
            );
          })));
}