voxera_calls_kit 0.2.1
voxera_calls_kit: ^0.2.1 copied to clipboard
One-to-one voice and video calls for Flutter on Voxera: ringing, answering, camera and speaker controls, and ready-made call screens.
example/lib/main.dart
import 'dart:async';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:voxera_calls_kit/voxera_calls_kit.dart';
/// Run with your project's values:
///
/// flutter run --dart-define=VOXERA_URL=https://... --dart-define=VOXERA_APP_ID=...
/// --dart-define=VOXERA_APP_KEY=... --dart-define=VOXERA_USER=sam
///
/// Two options are off unless you ask for them, as they are in the kit:
/// --dart-define=VOXERA_OUTGOING_ON_SYSTEM_SCREEN=true and
/// --dart-define=VOXERA_OS_PIP=true.
const _config = VoxeraCallConfig(
baseUrl: String.fromEnvironment('VOXERA_URL'),
appId: String.fromEnvironment('VOXERA_APP_ID'),
appKey: String.fromEnvironment('VOXERA_APP_KEY'),
outgoingOnSystemScreen: bool.fromEnvironment('VOXERA_OUTGOING_ON_SYSTEM_SCREEN'),
minimize: VoxeraCallMinimizeConfig(enableOSPip: bool.fromEnvironment('VOXERA_OS_PIP')),
);
const _userId = String.fromEnvironment('VOXERA_USER', defaultValue: 'sam');
/// A call declined while the app is closed.
@pragma('vm:entry-point')
Future<void> onBackgroundCallEvent(CallEvent event) async {
WidgetsFlutterBinding.ensureInitialized();
await VoxeraCalls.handleBackgroundEvent(event, config: _config);
}
/// A push that arrives while the app is in the background or closed.
@pragma('vm:entry-point')
Future<void> onBackgroundMessage(RemoteMessage message) async {
await VoxeraCalls.handlePush(message.data);
}
/// Android rings from Firebase. On iOS it stops the ringing when the caller gives up.
///
/// Your own app can call Firebase directly. The example checks first so it still runs without
/// google-services.json or GoogleService-Info.plist, which aren't in the repository. Calls then
/// ring only while the app is open on Android, and on iOS through VoIP pushes alone.
Future<bool> _startFirebase() async {
try {
await Firebase.initializeApp();
} catch (error) {
debugPrint('Firebase is not set up: $error');
return false;
}
FirebaseMessaging.onBackgroundMessage(onBackgroundMessage);
FirebaseMessaging.onMessage.listen((message) => VoxeraCalls.handlePush(message.data));
return true;
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
VoxeraCalls.registerBackgroundHandler(onBackgroundCallEvent);
final hasFirebase = await _startFirebase();
runApp(ExampleApp(hasFirebase: hasFirebase));
}
class ExampleApp extends StatefulWidget {
const ExampleApp({super.key, required this.hasFirebase});
final bool hasFirebase;
@override
State<ExampleApp> createState() => _ExampleAppState();
}
class _ExampleAppState extends State<ExampleApp> {
final _calls = VoxeraCallController(config: _config, userId: _userId);
final _callee = TextEditingController();
StreamSubscription<String>? _tokenRefresh;
@override
void initState() {
super.initState();
_start();
}
Future<void> _start() async {
// As early as possible: a call answered on the system's screen waits for this.
await _calls.connect();
await VoxeraCalls.requestRingPermissions();
// The VoIP token on iOS, which doesn't wait for Firebase. Does nothing on Android.
await _calls.registerPush();
if (!widget.hasFirebase) return;
// ignore: deprecated_member_use
_tokenRefresh = FirebaseMessaging.instance.onTokenRefresh.listen(
(token) => _calls.registerPush(fcmToken: token),
);
final token = await _firebaseToken();
if (token != null) await _calls.registerPush(fcmToken: token);
}
/// Null until iOS has given Firebase its APNs token, which never happens in the Simulator.
/// `onTokenRefresh` brings the token if it comes later. These calls are deprecated for Firebase's
/// install IDs, but the engine sends to tokens.
Future<String?> _firebaseToken() async {
try {
// ignore: deprecated_member_use
return await FirebaseMessaging.instance.getToken();
} catch (error) {
debugPrint('No Firebase token yet: $error');
return null;
}
}
@override
void dispose() {
_tokenRefresh?.cancel();
_callee.dispose();
_calls.dispose();
super.dispose();
}
void _call(VoxeraCallType type) {
final callee = _callee.text.trim();
if (callee.isNotEmpty) _calls.startCall(callee, type: type);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
builder: (context, child) => VoxeraCallHost(controller: _calls, child: child!),
home: Scaffold(
appBar: AppBar(title: const Text('$_userId calling')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
TextField(
controller: _callee,
decoration: const InputDecoration(labelText: 'Who to call'),
),
const SizedBox(height: 16),
Row(
children: [
Expanded(
child: FilledButton(
onPressed: () => _call(VoxeraCallType.voice),
child: const Text('Voice'),
),
),
const SizedBox(width: 12),
Expanded(
child: FilledButton(
onPressed: () => _call(VoxeraCallType.video),
child: const Text('Video'),
),
),
],
),
],
),
),
),
);
}
}