Thursday, December 8, 2022

Create polls in Flutter

Customizable Polls for Flutter.Simple, easy to use and highly customizable.

Getting Started

In the pubspec.yaml of your flutter project, add the following dependency:

dependencies:
  ...
  flutter_polls: ^0.0.5

Import it:

import 'package:flutter_polls/flutter_polls.dart';

Example

Check out the example project in the example directory here: example on github

FlutterPolls(
    pollId: '1',
    onVoted: (PollOption pollOption, int newTotalVotes) {
      print('Voted: ${pollOption.id}');
    },
    pollOptionsSplashColor: Colors.white(),
    votedProgressColor: Colors.greyDark().withOpacity(0.3),
    votedBackgroundColor: Colors.grey().withOpacity(0.2),
    votesTextStyle: themeData.textTheme.subtitle1,
    votedPercentageTextStyle:
        themeData.textTheme.headline4?.copyWith(
    color: Colors.black(),
    ),
    votedCheckmark: Icon(
        Icons.circle_check,
        color: AppColors.black(),
        height: 18,
        width: 18,
    ),
    pollTitle: Align(
    alignment: Alignment.centerLeft,
    child: AutoSizeText(
        poll['title'],
        style: TextStyle(
            fontSize: 20,
        ),
    ),
    pollOptions: poll['options'].map(
    (option) {
        return PollOption(
        id: option['id'],
        title: AutoSizeText(
            option['title'],
            style: tTextStyle(
            fontSize: 20,
        ),
        ),
        votes: option['votes'],
        );
    },
    ).toList(),
    metaWidget: Row(
    children: [
        const SizedBox(width: 6),
        AutoSizeText(
        '•',
        style: TextStyle(
            fontSize: 20,
        ),
        ),
        const SizedBox(
        width: 6,
        ),
        AutoSizeText(
        '2 weeks left',
        style: TextStyle(
            fontSize: 20,
        ),
        ),
    ],
    ),
),

Parameters

ParameterTypeDescription
pollIdStringThe poll id.
hasVotedboolWhether the user has voted.
userVotedOptionIdintThe user voted option id. If the user hasn’t voted, this will be null.
onVotedvoid Function(PollOption pollOption, int newTotalVotes)The callback when the user voted.
pollTitleWidgetThe poll title. Can be any widget.
pollOptionsListThe poll options. Each item is a map with the following keys: id, title, votes.
heightBetweenTitleAndOptionsdoubleThe height between the poll title and the poll options.
heightBetweenOptionsdoubleThe height between the poll options.
votesTextStringVotes text. Can be “Votes”, “Votos”, “Ibo” or whatever language. If not specified, “Votes” is used.
votesTextStyleTextStyleThe style of the votes text.
metaWidgetWidgetThe meta widget. Can be any widget.
createdByStringThe poll creator.
userToVoteStringThe user to vote.
pollStartDateDateTimeThe poll start date.
pollEndedboolIf the poll is closed.
pollOptionsHeightdoubleThe poll options height.
pollOptionsWidthdoubleThe poll options width.
pollOptionsBorderRadiusBorderRadiusThe poll options border radius.
pollOptionsBorderBoxBorderThe poll options border.
pollOptionsFillColorColorThe poll options fill color.
pollOptionsSplashColorColorThe poll options splash color.
votedPollOptionsRadiusRadiusThe voted poll options border radius.
votedBackgroundColorColorThe voted poll options background color.
votedProgressColorColorThe voted poll options progress color.
votedCheckmarkWidgetThe voted poll options checkmark.
votedPercentageTextStyleTextStyleThe voted poll options percentage text style.
votedAnimationDurationintThe voted poll options animation duration.