kayla_agora_voice_call 0.1.0
kayla_agora_voice_call: ^0.1.0 copied to clipboard
A flexible and customizable Agora voice call package by Kayla.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:kayla_agora_voice_call/kayla_agora_voice_call.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Kayla Agora Voice Call Example',
theme: ThemeData(
primarySwatch: Colors.blue,
useMaterial3: true,
),
home: const CallExampleScreen(),
);
}
}
class CallExampleScreen extends StatefulWidget {
const CallExampleScreen({Key? key}) : super(key: key);
@override
State<CallExampleScreen> createState() => _CallExampleScreenState();
}
class _CallExampleScreenState extends State<CallExampleScreen> {
late KaylaCallService _callService;
bool _isInitialized = false;
bool _isLoading = false;
String _statusMessage = 'Not initialized';
@override
void initState() {
super.initState();
_initializeCallService();
}
Future<void> _initializeCallService() async {
setState(() {
_isLoading = true;
_statusMessage = 'Initializing...';
});
try {
// Configure the call service
final callConfig = KaylaCallConfig(
agoraAppId: 'YOUR_AGORA_APP_ID', // Replace with your Agora App ID
callTimeoutDuration: 60,
tokenGenerator: (channelId, userId) async {
// In a real app, you would generate a token from your server
// For testing, you can use a temporary token or no token for testing channels
return '';
},
);
// Create the call service
_callService = KaylaCallService(
config: callConfig,
database: InMemoryCallDatabase(), // Use in-memory database for example
);
// Initialize the call service
await _callService.initialize();
// Listen for call events
_callService.callEvents.listen(_handleCallEvent);
setState(() {
_isInitialized = true;
_isLoading = false;
_statusMessage = 'Initialized';
});
} catch (e) {
setState(() {
_isLoading = false;
_statusMessage = 'Initialization failed: $e';
});
}
}
void _handleCallEvent(CallEvent event) {
print('Call event: $event');
switch (event) {
case CallEvent.started:
setState(() {
_statusMessage = 'Call started';
});
break;
case CallEvent.ended:
setState(() {
_statusMessage = 'Call ended';
});
break;
case CallEvent.error:
setState(() {
_statusMessage = 'Call error';
});
break;
default:
break;
}
}
Future<void> _startCall() async {
if (!_isInitialized) {
setState(() {
_statusMessage = 'Call service not initialized';
});
return;
}
setState(() {
_isLoading = true;
_statusMessage = 'Starting call...';
});
try {
// Start a call
final call = await _callService.startCall(
callerId: 'user1',
callerName: 'John Doe',
receiverId: 'user2',
receiverName: 'Jane Smith',
callerProfilePic: 'https://example.com/profile1.jpg',
receiverProfilePic: 'https://example.com/profile2.jpg',
);
if (call != null && mounted) {
// Show the call screen
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => KaylaCallScreen(
callService: _callService,
call: call,
uiConfig: CallUIConfig(
showCallerProfilePicture: true,
showCallerName: true,
showCallDuration: true,
useBlurredBackground: true,
useBackgroundGradient: true,
),
onCallEnded: () {
Navigator.pop(context);
setState(() {
_statusMessage = 'Call ended';
});
},
),
),
);
}
setState(() {
_isLoading = false;
});
} catch (e) {
setState(() {
_isLoading = false;
_statusMessage = 'Error starting call: $e';
});
}
}
Future<void> _simulateIncomingCall() async {
if (!_isInitialized) {
setState(() {
_statusMessage = 'Call service not initialized';
});
return;
}
setState(() {
_isLoading = true;
_statusMessage = 'Simulating incoming call...';
});
try {
// Show incoming call UI
await _callService.showIncomingCallUI(
callId: 'incoming-call-${DateTime.now().millisecondsSinceEpoch}',
callerName: 'Jane Smith',
callerId: 'user2',
callerImageUrl: 'https://example.com/profile2.jpg',
);
setState(() {
_isLoading = false;
_statusMessage = 'Incoming call shown';
});
} catch (e) {
setState(() {
_isLoading = false;
_statusMessage = 'Error showing incoming call: $e';
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Kayla Agora Voice Call Example'),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Status: $_statusMessage',
style: const TextStyle(fontSize: 16),
textAlign: TextAlign.center,
),
const SizedBox(height: 30),
if (_isLoading)
const CircularProgressIndicator()
else ...[
ElevatedButton(
onPressed:
_isInitialized ? _startCall : _initializeCallService,
child: Text(_isInitialized
? 'Start Call'
: 'Initialize Call Service'),
),
const SizedBox(height: 20),
if (_isInitialized)
ElevatedButton(
onPressed: _simulateIncomingCall,
child: const Text('Simulate Incoming Call'),
),
],
const SizedBox(height: 40),
const Text(
'Note: This example uses placeholder Agora credentials. '
'Replace them with your own to make real calls.',
style: TextStyle(fontSize: 12, color: Colors.grey),
textAlign: TextAlign.center,
),
],
),
),
),
);
}
@override
void dispose() {
// Clean up resources
if (_isInitialized) {
_callService.dispose();
}
super.dispose();
}
}