-
Notifications
You must be signed in to change notification settings - Fork 74
/
slide_countdown.dart
133 lines (123 loc) · 4.02 KB
/
slide_countdown.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:slide_countdown/slide_countdown.dart';
import 'package:confetti/confetti.dart';
class MySlideCountdown extends StatefulWidget {
const MySlideCountdown({Key? key}) : super(key: key);
@override
State<MySlideCountdown> createState() => _MySlideCountdownState();
}
class _MySlideCountdownState extends State<MySlideCountdown> {
late ConfettiController _controllerCenter;
bool isPlaying = false;
String countState = 'Counting Down...';
@override
void initState() {
super.initState();
_controllerCenter =
ConfettiController(duration: const Duration(seconds: 5));
}
@override
void dispose() {
_controllerCenter.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Slide Countdown'),
actions: [
IconButton(
onPressed: () {
_controllerCenter.stop();
setState(() {
isPlaying = false;
});
},
icon: isPlaying
? const Icon(Icons.stop)
: const Icon(Icons.play_arrow),
),
],
),
body: Center(
child: Stack(
children: [
Align(
alignment: Alignment.center,
child: ConfettiWidget(
confettiController: _controllerCenter,
blastDirectionality: BlastDirectionality
.explosive, // don't specify a direction, blast randomly
shouldLoop:
true, // start again as soon as the animation is finished
colors: const [
Colors.green,
Colors.blue,
Colors.pink,
Colors.orange,
Colors.purple
], // manually specify the colors to be used
createParticlePath: drawStar, // define a custom shape/path.
),
),
Align(
alignment: Alignment.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
countState,
style: const TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 30),
SlideCountdownSeparated(
duration: const Duration(seconds: 10),
textStyle: const TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
onDone: () {
_controllerCenter.play();
setState(() {
isPlaying = true;
countState = 'Done!!';
});
},
),
],
),
),
],
),
),
);
}
/// A custom Path to paint stars.
Path drawStar(Size size) {
// Method to convert degree to radians
double degToRad(double deg) => deg * (pi / 180.0);
const numberOfPoints = 5;
final halfWidth = size.width / 2;
final externalRadius = halfWidth;
final internalRadius = halfWidth / 2.5;
final degreesPerStep = degToRad(360 / numberOfPoints);
final halfDegreesPerStep = degreesPerStep / 2;
final path = Path();
final fullAngle = degToRad(360);
path.moveTo(size.width, halfWidth);
for (double step = 0; step < fullAngle; step += degreesPerStep) {
path.lineTo(halfWidth + externalRadius * cos(step),
halfWidth + externalRadius * sin(step));
path.lineTo(halfWidth + internalRadius * cos(step + halfDegreesPerStep),
halfWidth + internalRadius * sin(step + halfDegreesPerStep));
}
path.close();
return path;
}
}