showPollingViewDialog function

void showPollingViewDialog(
  1. BuildContext context,
  2. EnxController obj
)

Implementation

void showPollingViewDialog(BuildContext context, EnxController obj) {
  print("++++++${obj.data.length}");
  int safeAreaHeight = (Get.window.viewPadding.bottom.toInt() == 0)
      ? 20
      : Get.window.viewPadding.bottom.toInt();
  double appBarHeight = 0.0;

  showGeneralDialog(
    context: context,
    barrierColor: Colors.white, // Background color
    barrierDismissible: false,
    pageBuilder: (context, __, ___) {
      return Center(
        child: Material(
          type: MaterialType.transparency,
          // Make the dialog background transparent
          child: SizedBox(
              width: MediaQuery.of(context).size.width -
                  MediaQuery.of(context).padding.left +
                  MediaQuery.of(context).padding.right,
              height: Platform.isIOS
                  ? MediaQuery.of(context).size.height - safeAreaHeight
                  : Get.height -
                      appBarHeight -
                      Get.window.viewPadding.top / 2.5 -
                      Get.window.viewPadding.bottom,
              child: Material(
                type: MaterialType.transparency,
                child: Padding(
                  padding: const EdgeInsets.only(
                      left: 8.0, right: 8.0, top: 16.0, bottom: 8.0),
                  child: Column(
                    children: <Widget>[
                      Expanded(
                        child: Obx(() => obj.data.isNotEmpty
                            ? ListView.builder(
                               reverse: false,
                                itemCount:
                                    obj.data.length, // Example with 10 items
                                itemBuilder: (context, index) {
                                  final reversedIndex = obj.data.length - 1 - index;
                                  final question =
                                      obj.data.keys.elementAt(reversedIndex);
                                  final answers = obj.data[question]!;
                                  print("testStatus${question.status}");

                                  return Card(
                                    child: ExpansionTile(
                                      key: Key(reversedIndex.toString()),
                                      title: Text(question.questionTitle),
                                      children:
                                          answers.asMap().entries.map((entry) {
                                        int index = entry.key;
                                        var answer = entry.value;
                                        bool isLastItem =
                                            index == answers.length - 1;

                                        return Column(
                                          children: [
                                            ListTile(
                                              title: Text(answer.title),
                                              trailing: SizedBox(
                                                width: 200,
                                                child: Row(
                                                  mainAxisSize:
                                                      MainAxisSize.max,
                                                  mainAxisAlignment:
                                                      MainAxisAlignment
                                                          .spaceBetween,
                                                  children: [
                                                    Expanded(
                                                      child:
                                                          LinearProgressIndicator(
                                                        borderRadius:
                                                            BorderRadius.all(
                                                                Radius.circular(
                                                                    8)),
                                                        minHeight: 20,
                                                        value:
                                                            answer.percentage /
                                                                100,
                                                        backgroundColor:
                                                            Colors.grey[300],
                                                        color: Colors.pink,
                                                      ),
                                                    ),
                                                    SizedBox(width: 8),
                                                    Text(
                                                        '${answer.percentage}%'),
                                                  ],
                                                ),
                                              ),
                                            ),
                                            // Ensure a widget is always returned
                                            if (isLastItem)
      Obx(() {
      // Check the status and whether it's the last item
      // Replace with your actual logic
       // Get the current status from the controller

      // Conditionally render the UI based on the reactive variables
      if (question.status == 'P') {
      return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
      // Left Text
      Text(
      'All',
      style: TextStyle(
      fontWeight: FontWeight.w600,
      fontSize: 15,
      color: !obj.isAll.value ? Colors.pink : Colors.grey,
      ),
      ),
      SizedBox(width: 20),

      // Switch
      Switch(
      value: obj.isAll.value,
      onChanged: (value) {
      obj.isAll.value = value;  // Update observable
      },
      activeColor: Colors.pink,
      inactiveThumbColor: Colors.grey,
      activeTrackColor: Colors.pink[200],
      inactiveTrackColor: Colors.grey[300],
      ),
      SizedBox(width: 20),

      // Right Text
      Text(
      'Moderator',
      style: TextStyle(
      color: !obj.isAll.value ? Colors.grey : Colors.pink,
      fontWeight: FontWeight.w400,
      fontSize: 15,
      ),
      ),
      ],
      );
      } else {
      return SizedBox.shrink();  // Return an empty widget if conditions are not met
      }
      }),
                                                 // Return an empty widget if conditions are not met

                                            if (isLastItem)

                                              Row(
                                                  mainAxisSize:
                                                      MainAxisSize.min,
                                                  mainAxisAlignment:
                                                      MainAxisAlignment
                                                          .spaceEvenly,
                                                  children: [
                                                    Visibility(
                                                      visible: true,
                                                      child: Padding(
                                                        padding:
                                                            const EdgeInsets
                                                                .only(
                                                                top: 16.0,
                                                                right: 16.0),
                                                        child: ElevatedButton(
                                                          onPressed: () {
                                                            if (question
                                                                    .status ==
                                                                "I") {
                                                              obj.extendDuration(
                                                                  question
                                                                      .pollId);
                                                            } else {
                                                              if (question
                                                                      .status ==
                                                                  "P") {
                                                                obj.publishResult(
                                                                    question
                                                                        .pollId);
                                                              } else {
                                                                obj.startPoll(
                                                                    question
                                                                        .pollId);
                                                              }
                                                            }

                                                            // Handle button press
                                                          },
                                                          child: Obx(() => Text(
                                                                question.status ==
                                                                        "I"
                                                                    ? "Extend 10Sec"
                                                                    : question.status ==
                                                                            "P"
                                                                        ? "Publish Poll"
                                                                        : "Start Poll",
                                                                style: TextStyle(
                                                                    color: Colors
                                                                        .white),
                                                              )),
                                                          style: ElevatedButton
                                                              .styleFrom(
                                                            backgroundColor:
                                                                Colors.pink,
                                                            shape:
                                                                RoundedRectangleBorder(
                                                              borderRadius:
                                                                  BorderRadius
                                                                      .circular(
                                                                          15),
                                                            ),
                                                          ),
                                                        ),
                                                      ),
                                                    ),
                                                    Obx(() => Visibility(
                                                          visible:
                                                              question.status ==
                                                                      ""
                                                                  ? false
                                                                  : true,
                                                          child: Padding(
                                                            padding:
                                                                const EdgeInsets
                                                                    .only(
                                                                    top: 16.0,
                                                                    left: 16.0),
                                                            child:
                                                                ElevatedButton(
                                                              onPressed: () {
                                                                if (question
                                                                        .status ==
                                                                    "P") {
                                                                  obj.repoll(
                                                                      question
                                                                          .pollId);
                                                                } else {
                                                                  obj.stopPoll(
                                                                      question
                                                                          .pollId);
                                                                }
                                                              },
                                                              child: Text(
                                                                  question.status ==
                                                                          "P"
                                                                      ? "Repoll"
                                                                      : "Stop Poll",
                                                                  style: TextStyle(
                                                                      color: Colors
                                                                          .white)),
                                                              style:
                                                                  ElevatedButton
                                                                      .styleFrom(
                                                                backgroundColor:
                                                                    Colors.pink,
                                                                shape:
                                                                    RoundedRectangleBorder(
                                                                  borderRadius:
                                                                      BorderRadius
                                                                          .circular(
                                                                              15),
                                                                ),
                                                              ),
                                                            ),
                                                          ),
                                                        )),
                                                  ]),
                                          ],
                                        );
                                      }).toList(),
                                      initiallyExpanded: index == 0,
                                      onExpansionChanged: (isExpanded) {
                                        obj.toggleExpansion(index);
                                      },
                                    ),
                                  );
                                })
                            : const Center(
                                child: Text(
                                  'No Poll Available,Please create poll',
                                  style: TextStyle(
                                      color: Colors.black87,
                                      fontWeight: FontWeight.bold),
                                ),
                              )),
                      ),
                      Align(
                        alignment: Alignment.bottomRight,
                        child: Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: FloatingActionButton(
                            backgroundColor: Colors.pink,
                            onPressed: () {
                              Future.delayed(const Duration(milliseconds: 500),
                                  () {
                                showCreatePollingViewDialog(context, obj);
                              });
                            },
                            child: Icon(Icons.add, color: Colors.white),
                            // Shows a tooltip when long-pressed
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              )),
        ),
      );
    },
  );
}