-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmain.dart
More file actions
191 lines (176 loc) · 5.53 KB
/
main.dart
File metadata and controls
191 lines (176 loc) · 5.53 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import 'package:flutter/material.dart';
import 'package:vapi/vapi.dart';
void main() async {
// Wait for the Vapi SDK to be ready (required for web, instant on mobile)
await VapiClient.platformInitialized.future;
runApp(const MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String buttonText = 'Start Call';
bool isLoading = false;
bool isCallStarted = false;
VapiClient? vapiClient;
VapiCall? currentCall;
// Controllers for text fields
final TextEditingController _publicKeyController = TextEditingController();
final TextEditingController _assistantIdController = TextEditingController();
@override
void initState() {
super.initState();
}
void _handleCallEvents(VapiEvent event) {
if (event.label == "call-start") {
setState(() {
buttonText = 'End Call';
isLoading = false;
isCallStarted = true;
});
debugPrint('call started');
}
if (event.label == "call-end") {
setState(() {
buttonText = 'Start Call';
isLoading = false;
isCallStarted = false;
currentCall = null;
});
debugPrint('call ended');
}
if (event.label == "message") {
debugPrint('Message: ${event.value}');
}
}
Future<void> _onButtonPressed() async {
setState(() {
buttonText = 'Loading...';
isLoading = true;
});
try {
// Initialize client if not already done
// The factory automatically selects the appropriate platform implementation
vapiClient ??= VapiClient(_publicKeyController.text.trim());
if (!isCallStarted) {
// Start a new call using assistant ID
final call = await vapiClient!
.start(assistantId: _assistantIdController.text.trim());
currentCall = call;
call.onEvent.listen(_handleCallEvents);
} else {
// End the current call
await currentCall?.stop();
}
} catch (e) {
debugPrint('Error: $e');
setState(() {
buttonText = 'Start Call';
isLoading = false;
});
// Show error dialog
if (mounted) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Error'),
content: Text('Failed to start call: $e'),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('OK'),
),
],
),
);
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const SelectableText('Vapi Test App'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SelectableText(
'Vapi Flutter SDK Demo',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
const SizedBox(height: 16),
TextField(
controller: _publicKeyController,
decoration: const InputDecoration(
labelText: 'VAPI Public Key',
border: OutlineInputBorder(),
hintText: 'Enter your VAPI public key',
),
),
const SizedBox(height: 16),
TextField(
controller: _assistantIdController,
decoration: const InputDecoration(
labelText: 'VAPI Assistant ID',
border: OutlineInputBorder(),
hintText: 'Enter your VAPI assistant ID',
),
),
const SizedBox(height: 32),
ElevatedButton(
onPressed: isLoading ? null : _onButtonPressed,
child: Text(buttonText),
),
const SizedBox(height: 16),
if (currentCall != null) ...[
Text('Call Status: ${currentCall!.status}'),
const SizedBox(height: 8),
Text('Call ID: ${currentCall!.id}'),
const SizedBox(height: 8),
Text('Assistant ID: ${currentCall!.assistantId}'),
const SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () {
final isMuted = currentCall!.isMuted;
currentCall!.setMuted(!isMuted);
setState(() {}); // Refresh to update mute status
},
child: Text(currentCall!.isMuted ? 'Unmute' : 'Mute'),
),
ElevatedButton(
onPressed: () async {
await currentCall!.send({
'type': 'add-message',
'message': {
'role': 'system',
'content': 'The user pressed a button!'
}
});
},
child: const Text('Send Message'),
),
],
),
],
],
),
),
);
}
@override
void dispose() {
_publicKeyController.dispose();
_assistantIdController.dispose();
currentCall?.dispose();
vapiClient?.dispose();
super.dispose();
}
}