getPollBar method

Widget getPollBar(
  1. int index,
  2. String id,
  3. List<PollOptions> options,
  4. int totalVote,
  5. String chosenId,
  6. String pollId,
  7. CometChatTheme theme,
)

Implementation

Widget getPollBar(int index, String id, List<PollOptions> options,
    int totalVote, String chosenId, String pollId, CometChatTheme theme) {
  int count = options[index].voteCount;
  String text = options[index].optionText;
  double percentage = totalVote == 0 ? 0 : (count / totalVote * 100);
  Color progressColor =
      style?.unSelectedOptionColor ?? Colors.transparent;
  Color background =
      style?.pollOptionsBackgroundColor ?? Colors.transparent;

  if (id == chosenId) {
    progressColor =
        style?.selectedOptionColor ?? AppTheme.tertiary;
  }

  return Padding(
    padding: const EdgeInsets.only(bottom: 6.0, right: 10, left: 10),
    child: GestureDetector(
      onTap: () {
        if (loggedInUser != null &&
            senderUid != null &&
            senderUid == loggedInUser) {
          return;
        }

        int previousChosen = options
            .indexWhere((PollOptions element) => element.id == chosenId);
        if (previousChosen != -1) {
          options[previousChosen].voteCount--;
        } else {
          totalVote++;
        }

        chosenId = id;
        options[index].voteCount++;
        choosePoll(id, pollId);
      },
      child: Stack(
        alignment: Alignment.center,
        children: [
          ClipRRect(
              borderRadius: BorderRadius.circular(24.0),
              child: LinearProgressIndicator(
                backgroundColor: background,
                minHeight: 36,
                value: percentage / 100,
                valueColor: AlwaysStoppedAnimation<Color>(progressColor),
              )),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Flexible(
                  flex: 7,
                  child: Padding(
                    padding: const EdgeInsets.only(left: 11),
                    child: Text(
                      text,
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                              fontSize: theme.typography.subtitle1.fontSize,
                              fontWeight: FontWeight.w600,
                              fontFamily:
                                  theme.typography.subtitle1.fontFamily,
                              color: AppTheme.white.withOpacity(0.8))
                          .merge(style?.pollOptionsTextStyle),
                    ),
                  )),
              Flexible(
                  flex: 3,
                  child: Padding(
                    padding: const EdgeInsets.only(right: 11),
                    child: Text(
                      "${percentage.toStringAsFixed(1)}%",
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                              fontSize: theme.typography.subtitle2.fontSize,
                              fontWeight:FontWeight.w600,
                              fontFamily:
                                  theme.typography.subtitle2.fontFamily,
                              color: AppTheme.white)
                          .merge(style?.pollResultTextStyle),
                    ),
                  )),
            ],
          )
        ],
      ),
    ),
  );
}