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
134
135
136
137
138
| | import 'dart:async';
| |
| | import 'package:flutter/material.dart';
| | import 'package:vibration/vibration.dart';
| |
| | import '../theme/app_theme.dart';
| |
| | /// Large circular talk button. Tap to toggle recording.
| | /// Long-press while recording (600ms) cancels.
| | class VoiceButton extends StatefulWidget {
| | final bool isRecording;
| | final VoidCallback onTapStart;
| | final VoidCallback onTapStop;
| | final VoidCallback onCancel;
| |
| | const VoiceButton({
| | super.key,
| | required this.isRecording,
| | required this.onTapStart,
| | required this.onTapStop,
| | required this.onCancel,
| | });
| |
| | @override
| | State<VoiceButton> createState() => _VoiceButtonState();
| | }
| |
| | class _VoiceButtonState extends State<VoiceButton>
| | with SingleTickerProviderStateMixin {
| | late final AnimationController _pulseController;
| | Timer? _longPressTimer;
| |
| | @override
| | void initState() {
| | super.initState();
| | _pulseController = AnimationController(
| | vsync: this,
| | duration: const Duration(milliseconds: 1500),
| | );
| | }
| |
| | @override
| | void didUpdateWidget(VoiceButton oldWidget) {
| | super.didUpdateWidget(oldWidget);
| | if (widget.isRecording && !oldWidget.isRecording) {
| | _pulseController.repeat(reverse: true);
| | } else if (!widget.isRecording && oldWidget.isRecording) {
| | _pulseController.stop();
| | _pulseController.value = 0;
| | }
| | }
| |
| | @override
| | void dispose() {
| | _pulseController.dispose();
| | _longPressTimer?.cancel();
| | super.dispose();
| | }
| |
| | void _onTap() {
| | if (widget.isRecording) {
| | widget.onTapStop();
| | } else {
| | widget.onTapStart();
| | }
| | _haptic();
| | }
| |
| | void _onLongPressStart(LongPressStartDetails _) {
| | if (widget.isRecording) {
| | _longPressTimer = Timer(const Duration(milliseconds: 600), () {
| | widget.onCancel();
| | _haptic();
| | });
| | }
| | }
| |
| | void _onLongPressEnd(LongPressEndDetails _) {
| | _longPressTimer?.cancel();
| | }
| |
| | Future<void> _haptic() async {
| | try {
| | final hasVibrator = await Vibration.hasVibrator();
| | if (hasVibrator) {
| | Vibration.vibrate(duration: 20);
| | }
| | } catch (_) {}
| | }
| |
| | @override
| | Widget build(BuildContext context) {
| | return GestureDetector(
| | onTap: _onTap,
| | onLongPressStart: _onLongPressStart,
| | onLongPressEnd: _onLongPressEnd,
| | child: AnimatedBuilder(
| | animation: _pulseController,
| | builder: (context, child) {
| | final glowRadius = widget.isRecording
| | ? 8.0 + (_pulseController.value * 12.0)
| | : 0.0;
| |
| | return Container(
| | width: 72,
| | height: 72,
| | decoration: BoxDecoration(
| | color: widget.isRecording
| | ? AppColors.recording
| | : AppColors.accent,
| | shape: BoxShape.circle,
| | boxShadow: widget.isRecording
| | ? [
| | BoxShadow(
| | color: AppColors.recordingGlow,
| | blurRadius: glowRadius,
| | spreadRadius: glowRadius / 2,
| | ),
| | ]
| | : [
| | BoxShadow(
| | color: AppColors.accent.withAlpha(60),
| | blurRadius: 8,
| | spreadRadius: 2,
| | ),
| | ],
| | ),
| | child: Icon(
| | widget.isRecording ? Icons.stop : Icons.mic,
| | color: Colors.white,
| | size: 32,
| | ),
| | );
| | },
| | ),
| | );
| | }
| | }
|
|