spinning_wheel_package 0.0.1
spinning_wheel_package: ^0.0.1 copied to clipboard
A highly customizable Flutter package for creating a spinning wheel widget for games, raffles, and decision-making.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:just_audio/just_audio.dart';
import 'package:spinning_wheel_package/spinning_wheel_package.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Spin Package Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
),
home: const MyHomePage(title: 'Wheel Spin Package'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final AudioPlayer _audioPlayer = AudioPlayer();
bool _isSoundLoading = false;
String _spinResult = "Spin the wheel!";
final List<WheelItem> _sections = [
WheelItem(text: "Music", color: Colors.green,),
WheelItem(text: "Art & Books", color: Colors.orange,),
WheelItem(text: "Other Sports", color: Colors.purple),
WheelItem(text: "Nutrition", color: Colors.blue),
WheelItem(text: "Entertainment", color: Colors.red),
WheelItem(text: "Current Affairs", color: Colors.yellow),
];
@override
void dispose() {
_audioPlayer.dispose();
super.dispose();
}
///You can add a sound to the wheel by adding an audio file in the assets folder with any audio package of your choice
Future<void> _playSound() async {
// If a sound is already being prepared, do nothing
if (_isSoundLoading) return;
// Set the flag to block other calls
_isSoundLoading = true;
try {
// setAsset implicitly stops any previous sound
await _audioPlayer.setAsset('assets/audios/spin.mp3');
// Rewind to the beginning to ensure it plays every time
await _audioPlayer.seek(Duration.zero);
_audioPlayer.play();
} catch (e) {
print("Error playing sound: $e");
} finally {
// Always release the flag when done
_isSoundLoading = false;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
_spinResult,
style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
SpinningWheel(
sections: _sections,
onSpinStart: (){
_playSound();
},
onSpinEnd: (index) {
setState(() {
_spinResult = "Result: ${_sections[index].text}";
});
},
rimType: WheelRimType.silver,
standOffset: 0,
centerHubWidth: 50,
centerHubHeight: 50,
centerHubText: 'SPIN',
useCustomPointer: true,
pointerImagePath: 'assets/images/pointer.png',
)
],
),
),
);
}
}