Creare sondaggi in Flutter con flutter_polls

Mattepuffo's logo
Creare sondaggi in Flutter con flutter_polls

Creare sondaggi in Flutter con flutter_polls

flutter_polls è un widget per Flutter che ci consente di creare un sistema di sondaggi.

E' molto completo, ed in questo articolo ne vediamo un esempio basico.

Per cominciare installiamo il package così:

flutter pub add flutter_polls

Qui sotto un esempio:

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Test',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Test'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: FlutterPolls(
          pollId: '1',
          hasVoted: false,
          userVotedOptionId: 'uno',
          onVoted: (PollOption pollOption, int newTotalVotes) async {
            print('Voted: ${pollOption.id}');
            await Future.delayed(const Duration(seconds: 1));
            return true;
          },
          votedCheckmark: const Icon(Icons.check_circle),
          pollTitle: const Align(
            alignment: Alignment.center,
            child: Text("Vota il tuo libro"),
          ),
          pollOptions: [
            PollOption(title: const Text("IT"), votes: 10),
            PollOption(title: const Text("Sahara"), votes: 5),
          ],
          metaWidget: const Text("Mancano due giorni alla fine del sondaggio!"),
          pollOptionsSplashColor: Colors.amber,
          votedProgressColor: Colors.grey,
          votedBackgroundColor: Colors.amber,
        ),
      ),
    );
  }
}

Enjoy!


Condividi

Commentami!