better_polls 1.0.8 copy "better_polls: ^1.0.8" to clipboard
better_polls: ^1.0.8 copied to clipboard

A better flutter widget for polls, this immitates the twitter polls system, all you need do is connect you data to the polls, it allows voting and visualization.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:better_polls/better_polls.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Polls',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const PollView(),
    );
  }
}

class PollView extends StatefulWidget {
  const PollView({Key? key}) : super(key: key);

  @override
  State<PollView> createState() => _PollViewState();
}

class _PollViewState extends State<PollView> {
  double option1 = 1.0;
  double option2 = 0.0;
  double option3 = 1.0;
  double option4 = 1.0;
  double option5 = 7.0;
  double option6 = 3.0;
  double option7 = 17.0;
  double option8 = 1.0;

  String user = "king@mail.com";
  Map<String, int> usersWhoVoted = {
    'sam@mail.com': 3,
    'mike@mail.com': 4,
    'john@mail.com': 1,
    'kenny@mail.com': 1
  };
  String creator = "eddy@mail.com";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(32.0),
          child: Polls(
            children: [
              // This cannot be less than 2, else will throw an exception
              Polls.options(title: 'Cairo', value: option1),
              Polls.options(title: 'Mecca', value: option2),
              Polls.options(title: 'Denmark', value: option3),
              Polls.options(title: 'Mogadishu', value: option4),
              Polls.options(title: 'Maldives', value: option5),
              Polls.options(title: 'Brazil', value: option6),
              Polls.options(title: 'Ethiopia', value: option7),
              Polls.options(title: 'Italy', value: option8),
            ],
            optionBarRadius: 24,
            borderWidth: 1,
            optionHeight: 50,
            optionSpacing: 12,
            question: const Padding(
              padding: EdgeInsets.only(bottom: 16.0),
              child: Text('What is your favorite place?'),
            ),
            currentUser: user,
            creatorID: creator,
            voteData: usersWhoVoted,
            userChoice: usersWhoVoted[user],
            onVoteBorderColor: Colors.deepPurple,
            voteCastedBorderColor: Colors.orange,
            onVoteBackgroundColor: Colors.blue,
            leadingBackgroundColor: Colors.lightGreen,
            backgroundColor: Colors.grey,
            voteCastedBackgroundColor: Colors.grey,
            onVote: (choice) {
              setState(() {
                usersWhoVoted[user] = choice;
              });
              if (choice == 1) {
                setState(() {
                  option1 += 1.0;
                });
              }
              if (choice == 2) {
                setState(() {
                  option2 += 1.0;
                });
              }
              if (choice == 3) {
                setState(() {
                  option3 += 1.0;
                });
              }
              if (choice == 4) {
                setState(() {
                  option4 += 1.0;
                });
              }
              if (choice == 5) {
                setState(() {
                  option5 += 1.0;
                });
              }
              if (choice == 6) {
                setState(() {
                  option6 += 1.0;
                });
              }
              if (choice == 7) {
                setState(() {
                  option7 += 1.0;
                });
              }
              if (choice == 8) {
                setState(() {
                  option8 += 1.0;
                });
              }
            },
          ),
        ),
      ),
    );
  }
}
8
likes
140
points
122
downloads

Publisher

unverified uploader

Weekly Downloads

A better flutter widget for polls, this immitates the twitter polls system, all you need do is connect you data to the polls, it allows voting and visualization.

Repository (GitHub)

Documentation

API reference

License

unknown (license)

Dependencies

collection, flutter, percent_indicator

More

Packages that depend on better_polls