single static method

void single({
  1. required String title,
  2. String? questionText,
  3. String? helperText,
  4. String? initialValue,
  5. double height = 250,
  6. double width = 300,
  7. double titleTextSize = 18,
  8. TextStyle? questionTextStyle,
  9. TextStyle? helperTextStyle,
  10. bool isDismissible = true,
  11. String inputHint = 'example',
  12. String btnOkText = 'Simpan',
  13. String btnCancelText = 'Batal',
  14. Color? barrierColor = Colors.black54,
  15. double cornerRadius = 4,
  16. dynamic onYes(
    1. String
    )?,
  17. dynamic onNo()?,
})

Implementation

static void single({
  required String title,
  String? questionText,
  String? helperText,
  String? initialValue,
  double height = 250,
  double width = 300,
  double titleTextSize = 18,
  TextStyle? questionTextStyle,
  TextStyle? helperTextStyle,
  bool isDismissible = true,
  String inputHint = 'example',
  String btnOkText = 'Simpan',
  String btnCancelText = 'Batal',
  Color? barrierColor = Colors.black54,
  double cornerRadius = 4,
  Function(String)? onYes,
  Function()? onNo,
}) {
  final isInputValid = false.obs;
  final tfController = TextEditingController(text: initialValue);

  showDialog(
    context: Get.context!,
    barrierDismissible: isDismissible,
    barrierColor: barrierColor,
    builder: (context) {
      return AlertDialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(cornerRadius))),
        contentPadding: EdgeInsets.all(0),
        content: SizedBox(
          height: height,
          width: width,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                color: Color(0xFFF6F7F9),
                height: 56,
                width: width,
                child: HStack(
                  [title.text.size(20).bold.make().pSymmetric(h: 24)],
                ),
              ).cornerRadius(4),
              24.heightBox,
              if (questionText != null)
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 16),
                  child: Text(
                    questionText,
                    textAlign: TextAlign.start,
                    style: questionTextStyle ?? TextStyle(fontSize: 12, color: Color(0xFF676E76)),
                  ),
                ),
              VxTextField(
                controller: tfController,
                borderType: VxTextFieldBorderType.roundLine,
                borderRadius: 4,
                borderColor: Color(0xFF676E76),
                fillColor: Colors.white,
                contentPaddingLeft: 8,
                contentPaddingTop: 8,
                onChanged: (s) => isInputValid.value = s.isNotBlank,
              ).pSymmetric(h: 12, v: 12),
              if (helperText != null)
                Text(
                  helperText,
                  textAlign: TextAlign.start,
                  style: helperTextStyle ?? TextStyle(fontSize: 12, color: Color(0xFF676E76)),
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                ).pSymmetric(h: 12),
              8.heightBox,
              Divider(),
            ],
          ),
        ),
        actions: [
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              OutlinedButton(
                onPressed: onNo ?? () => Get.back(),
                child: Text(
                  btnCancelText,
                  style: TextStyle(color: Colors.black),
                ),
              ).pOnly(right: 12, bottom: 12).h(55),
              Obx(
                () => ElevatedButton(
                  style: ElevatedButton.styleFrom(backgroundColor: Theme.of(context).primaryColor),
                  onPressed: isInputValid.value == true
                      ? () {
                          if (tfController.text.isNotBlank) {
                            Get.back();
                            onYes?.call(tfController.text);
                          } else {
                            Get.snackbar(
                              'Gagal',
                              'Field tidak boleh kosong',
                              backgroundColor: Theme.of(context).colorScheme.error,
                              colorText: Colors.white,
                              duration: 900.milliseconds,
                              animationDuration: 100.milliseconds,
                            );
                          }
                        }
                      : null,
                  child: Text(btnOkText),
                ).pOnly(right: 12, bottom: 12).h(55),
              ),
            ],
          ),
        ],
      );
    },
  );
}