showPollingAnswerDialog function

void showPollingAnswerDialog(
  1. BuildContext context,
  2. EnxController obj,
  3. Map map
)

Implementation

void showPollingAnswerDialog(
    BuildContext context, EnxController obj, Map<dynamic, dynamic> map) {
  obj.isAnswerDialogOpen.value = true;
  // Variable to hold the selected value
  print('poll Ans${jsonEncode(map)}');
  var pollData = map['message']['data'];

  // Iterate over the options dynamically
  obj.pollTimer();
  Get.dialog(
    Center(
      child: Material(
        type: MaterialType.transparency,
        child: Container(
          width: MediaQuery.of(context).size.width * 0.9,
          height: MediaQuery.of(context).size.height * 0.7,
          padding: const EdgeInsets.all(16.0),
          decoration: BoxDecoration(
            color: Colors.black87,
            borderRadius: BorderRadius.circular(15.0),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Obx(() => Text(
                        "${obj.timeRemaining.value}",
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 15,
                        ),
                      )),
                  IconButton(
                    icon: Icon(Icons.close),
                    color: Colors.white,
                    onPressed: () {
                      obj.isAnswerDialogOpen.value = false;
                      obj.cancelTimer(); // Cancel the timer
                      Get.back(); // Close the dialog
                    },
                  ),
                ],
              ),
              SizedBox(height: 8),
              Text(
                "Participant in Poll!",
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                ),
              ),
              SizedBox(height: 8),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 10),
                child: Text(
                  "${pollData['question']}",
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Colors.yellow,
                    fontSize: 15,
                  ),
                ),
              ),
              SizedBox(height: 15),
              Expanded(
                child: ListView.builder(
                  itemCount: obj.optionList.length,
                  itemBuilder: (context, index) {
                    final option = obj.optionList[index];
                    print(
                        "Option Key: ${obj.selectedOptionKey.value}, Selected: ${obj.selectedOptionValue.value}");

                    return Obx(() => RadioListTile<String>(
                          key: UniqueKey(),
                          title: Text(
                            option.optionValue,
                            style: TextStyle(color: Colors.white),
                          ),
                          value: option.optionKey,
                          groupValue: obj.selectedOptionKey.value,
                          activeColor: Colors.pink,
                          // Ensure matching types
                          onChanged: (value) {
                            obj.selectedOptionKey.value =
                                value!; // Store the selected option key
                            obj.selectedOptionValue.value = option
                                .optionValue; // Store the selected option value

                            obj.update();
                          },
                        ));
                  },
                ),
              ),
              SizedBox(height: 15),
              ElevatedButton(
                onPressed: () {
                  obj.isAnswerDialogOpen.value = false;
                  obj.cancelTimer();
                  obj.sendPollResponse(map['senderId']);// Cancel the timer on submit
                  Get.back();
                  // Handle the submit action
                },
                style: ElevatedButton.styleFrom(
                  padding: const EdgeInsets.symmetric(
                    vertical: 10,
                    horizontal: 25,
                  ),
                  backgroundColor: Colors.pink,
                ),
                child: Text(
                  "Submit",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 15,
                  ),
                ),
              ),
              SizedBox(height: 30),
            ],
          ),
        ),
      ),
    ),
    barrierDismissible: false,
    barrierColor: Colors.black54,
  ).then((_) {
    obj.isAnswerDialogOpen.value =
        false; // Also ensure this is updated if the dialog is dismissed
    obj.cancelTimer(); // Cancel the timer when the dialog is dismissed
  });
}