Preview:
void confirmDialog(
      String req, TicketModel ticket, List<AuthModel> assignToPersonList) {
    try {
      if (req == "Approve") {
        timeDurationController.clear();
        showDialog(
          barrierDismissible: false,
          context: context,
          builder: (context) {
            return StatefulBuilder(
              builder: (context, setState) {
                return AlertDialog(
                  title: Center(
                    child: Text(
                      "Verify",
                      style: Theme.of(context).textTheme.bodyLarge,
                    ),
                  ),
                  content: SizedBox(
                    height: 200,
                    child: Column(
                      children: [
                        DropdownButtonFormField<String>(
                          validator: (value) {
                            if (value == null) {
                              return 'Please select a Person to Assign';
                            }
                            return null;
                          },
                          decoration: InputDecoration(
                            labelText: "Person to Assign",
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(10),
                            ),
                          ),
                          style: Theme.of(context).textTheme.bodyLarge,
                          iconSize: 30,
                          hint: const Text("Select Person to Assign"),
                          items: assignToPersonList.map((authModel) {
                            return DropdownMenuItem(
                              value: authModel.uid,
                              child: Text(authModel.fullName),
                            );
                          }).toList(),
                          onChanged: (String? value) {
                            setState(() {
                              selectedPerson = value;
                            });
                          },
                        ),
                        const SizedBox(height: 20),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            ElevatedButton(
                              onPressed: () {
                                setState(() {
                                  isHourly = true;
                                  timeDurationController.clear();
                                });
                              },
                              style: ElevatedButton.styleFrom(
                                backgroundColor: isHourly
                                    ? Color.fromARGB(255, 0, 0, 0)
                                    : Color.fromARGB(255, 17, 17, 17),
                              ),
                              child: const Text("Hourly"),
                            ),
                            ElevatedButton(
                              onPressed: () {
                                setState(() {
                                  isHourly = false;
                                });
                              },
                              style: ElevatedButton.styleFrom(
                                backgroundColor: !isHourly
                                    ? Color.fromARGB(255, 0, 0, 0)
                                    : Color.fromARGB(255, 17, 17, 17),
                              ),
                              child: const Text("Day"),
                            ),
                          ],
                        ),
                        const SizedBox(height: 20),
                        TextField(
                          controller: timeDurationController,
                          keyboardType: TextInputType.number,
                          inputFormatters: [
                            FilteringTextInputFormatter.digitsOnly,
                          ],
                          decoration: InputDecoration(
                            labelText: isHourly
                                ? "Time Duration (Hours)"
                                : "Time Duration (Days)",
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(10),
                            ),
                          ),
                          // Restrict input based on mode
                          onChanged: (value) {
                            if (value.isNotEmpty) {
                              int enteredValue = int.parse(value);

                              if (isHourly) {
                                // If hourly, restrict between 1 and 7
                                if (enteredValue < 1 || enteredValue > 7) {
                                  timeDurationController.text = "1";
                                }
                              } else {
                                // If day-wise, restrict between 1 and 60
                                if (enteredValue < 1 || enteredValue > 60) {
                                  timeDurationController.text = "1";
                                }
                              }

                              // Set the cursor position at the end of the text
                              timeDurationController.selection =
                                  TextSelection.fromPosition(
                                TextPosition(
                                    offset: timeDurationController.text.length),
                              );
                            }
                          },
                        ),
                      ],
                    ),
                  ),
                  actions: <Widget>[
                    TextButton(
                      child: const Text('Cancel'),
                      onPressed: () {
                        Navigator.pop(context);
                      },
                    ),
                    ElevatedButton(
                      child: const Text('Approve'),
                      onPressed: () async {
                        if (selectedPerson != null &&
                            timeDurationController.text.isNotEmpty) {
                          int timeDuration =
                              int.parse(timeDurationController.text);
                          if (!isHourly) {
                            timeDuration *= 7;
                          }
                          ticket.assignTo = selectedPerson!;
                          ticket.timeDuration = timeDuration;
                          ticket.status = TicketStatus.inProgress;
                          ref
                              .read(ticketControllerProvider)
                              .updateTicket(ref, ticket);
                          ref.invalidate(ticketListStreamProvider);
                          ref.invalidate(ticketsByCreatedByStreamProvider);
                          Navigator.pop(context);
                        } else {
                          log("Error: Please fill all fields.");
                        }
                      },
                    ),
                  ],
                );
              },
            );
          },
        );
      } else if (req == "Reject") {
        TextEditingController rejectReason = TextEditingController();
        final GlobalKey<FormState> _reject = GlobalKey<FormState>();
        showDialog(
          barrierDismissible: false,
          context: context,
          builder: (context) {
            return AlertDialog(
              title: Center(
                child: Text(
                  "Verify",
                  style: Theme.of(context).textTheme.bodyLarge,
                ),
              ),
              content: SizedBox(
                height: 120,
                child: Form(
                  key: _reject,
                  child: TextFormField(
                    validator: (text) {
                      if (text == null || text.isEmpty) {
                        return 'Please enter a reason for rejecting the request';
                      }
                      return null;
                    },
                    controller: rejectReason,
                    keyboardType: TextInputType.text,
                    textCapitalization: TextCapitalization.words,
                    decoration: InputDecoration(
                      labelText: "Reason for Rejection",
                      hintText: "Enter reason to reject the request",
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(10),
                      ),
                    ),
                  ),
                ),
              ),
              actions: <Widget>[
                TextButton(
                  child: const Text('Cancel'),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                ),
                ElevatedButton(
                  child: const Text('Reject'),
                  onPressed: () async {
                    if (_reject.currentState!.validate()) {
                      ticket.status = TicketStatus.rejected;
                      ticket.rejectionRemark = rejectReason.text;
                      ref
                          .read(ticketControllerProvider)
                          .updateTicket(ref, ticket);
                      ref.invalidate(ticketListStreamProvider);
                      ref.invalidate(ticketsByCreatedByStreamProvider);
                      Navigator.pop(context);
                    }
                  },
                ),
              ],
            );
          },
        );
      }
    } catch (e) {
      log("Error occurred: $e");
    }
  }





void confirmDialog(
    String req, TicketModel ticket, List<AuthModel> assignToPersonList) {
  try {
    if (req == "Approve") {
      timeDurationController.clear();
      showDialog(
        barrierDismissible: false,
        context: context,
        builder: (context) {
          return StatefulBuilder(
            builder: (context, setState) {
              return AlertDialog(
                title: Center(
                  child: Text(
                    "Verify",
                    style: Theme.of(context).textTheme.bodyLarge,
                  ),
                ),
                content: SizedBox(
                  height: 200,
                  child: Column(
                    children: [
                      DropdownButtonFormField<String>(
                        validator: (value) {
                          if (value == null) {
                            return 'Please select a Person to Assign';
                          }
                          return null;
                        },
                        decoration: InputDecoration(
                          labelText: "Person to Assign",
                          border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(10),
                          ),
                        ),
                        style: Theme.of(context).textTheme.bodyLarge,
                        iconSize: 30,
                        hint: const Text("Select Person to Assign"),
                        items: assignToPersonList.map((authModel) {
                          return DropdownMenuItem(
                            value: authModel.uid,
                            child: Text(authModel.fullName),
                          );
                        }).toList(),
                        onChanged: (String? value) {
                          setState(() {
                            selectedPerson = value;
                          });
                        },
                      ),
                      const SizedBox(height: 20),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          ElevatedButton(
                            onPressed: () {
                              setState(() {
                                isHourly = true;
                                timeDurationController.clear();
                              });
                            },
                            style: ElevatedButton.styleFrom(
                              backgroundColor: isHourly
                                  ? Color.fromARGB(255, 0, 0, 0)
                                  : Color.fromARGB(255, 17, 17, 17),
                            ),
                            child: const Text("Hourly"),
                          ),
                          ElevatedButton(
                            onPressed: () {
                              setState(() {
                                isHourly = false;
                              });
                            },
                            style: ElevatedButton.styleFrom(
                              backgroundColor: !isHourly
                                  ? Color.fromARGB(255, 0, 0, 0)
                                  : Color.fromARGB(255, 17, 17, 17),
                            ),
                            child: const Text("Day"),
                          ),
                        ],
                      ),
                      const SizedBox(height: 20),
                      TextField(
                        controller: timeDurationController,
                        keyboardType: TextInputType.number,
                        inputFormatters: [
                          FilteringTextInputFormatter.digitsOnly,
                        ],
                        decoration: InputDecoration(
                          labelText: isHourly
                              ? "Time Duration (Hours)"
                              : "Time Duration (Days)",
                          border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(10),
                          ),
                        ),
                        onChanged: (value) {
                          if (value.isNotEmpty) {
                            int enteredValue = int.parse(value);

                            if (isHourly) {
                              // If hourly, restrict between 1 and 7
                              if (enteredValue < 1 || enteredValue > 7) {
                                timeDurationController.text = "1";
                              }
                            } else {
                              // If day-wise, restrict between 1 and 60
                              if (enteredValue < 1 || enteredValue > 60) {
                                timeDurationController.text = "1";
                              }
                            }

                            timeDurationController.selection =
                                TextSelection.fromPosition(
                              TextPosition(
                                  offset: timeDurationController.text.length),
                            );
                          }
                        },
                      ),
                    ],
                  ),
                ),
                actions: <Widget>[
                  TextButton(
                    child: const Text('Cancel'),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  ),
                  ElevatedButton(
                    child: const Text('Approve'),
                    onPressed: () async {
                      if (selectedPerson != null &&
                          timeDurationController.text.isNotEmpty) {
                        int timeDuration = int.parse(timeDurationController.text);
                        ticket.assignTo = selectedPerson!;
                        
                        // Store the current time in assigningTime
                        DateTime assigningTime = DateTime.now();
                        ticket.assigningTime = assigningTime;

                        if (!isHourly) {
                          // Convert days to hours for storing as timeDuration
                          timeDuration *= 24;
                        }

                        // Add timeDuration to assigningTime to calculate estimatedTime
                        ticket.estimatedTime =
                            assigningTime.add(Duration(hours: timeDuration));

                        ticket.timeDuration = timeDuration;
                        ticket.status = TicketStatus.inProgress;

                        ref
                            .read(ticketControllerProvider)
                            .updateTicket(ref, ticket);
                        ref.invalidate(ticketListStreamProvider);
                        ref.invalidate(ticketsByCreatedByStreamProvider);
                        Navigator.pop(context);
                      } else {
                        log("Error: Please fill all fields.");
                      }
                    },
                  ),
                ],
              );
            },
          );
        },
      );
    } else if (req == "Reject") {
      // Code for Reject case
    }
  } catch (e) {
    log("Error occurred: $e");
  }
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter