voip_sendbird_plugin 0.0.1 voip_sendbird_plugin: ^0.0.1 copied to clipboard
flutter voip plugin base on Sendbird SDK.
import 'dart:convert';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:voip_sendbird_plugin/voip_sendbird_plugin.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _applicationId = '686039A2-9FE7-4C91-98C6-342A2094569D';
String _fcmToken = "";
String _userId = 'LM03';
String _callId = 'LM04';
String _loginResult;
String _makeCallResult = "waiting for actions ...";
String successStatusCode = '0000';
String logoutStatusCode = '4001';
String dialingStatusCode = '2000';
String riningStatusCode = '2002';
String connectStatusCode = '0000';
String endStatusCode = '4000';
bool _showLoginBtn = true;
bool _showLogOutBtn = false;
bool _showMakeCallBtn = false;
bool _showCallOptionBtn = false;
bool _showRingingBtn = false;
Stream<String> _authListener;
Stream<String> _directCallListener;
Stream<String> _ringingListener;
TextEditingController textUserIdController = TextEditingController();
TextEditingController textCalleeController = TextEditingController();
@override
void initState() {
super.initState();
_initialFirebase();
connectRingingChannel();
connectAuthChannel();
connectDirectCallChannel();
}
_initialFirebase() async {
await Firebase.initializeApp();
FirebaseMessaging.instance.getToken().then((value) async {
assert(value != null);
setState(() {
_fcmToken = value;
});
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
if (message != null) {
try {
String data = json.encode(message.data);
VoipSendbirdPlugin.onMessageReceived(data);
} catch (_) {
print('Error: ${_.message}');
}
print('Message data: ${message.data[1]}');
}
});
}
_initialSendBird() async {
await VoipSendbirdPlugin.initApp(_applicationId);
}
_onLogin() async {
if (textUserIdController.text == null || textUserIdController.text == '') {
await VoipSendbirdPlugin.signIn(_userId, _fcmToken);
} else {
await VoipSendbirdPlugin.signIn(textUserIdController.text, _fcmToken);
}
}
_onLogOut() async {
await VoipSendbirdPlugin.signOut();
}
_onToggleAudioDevice() async {
await VoipSendbirdPlugin.toggleAudioDevice();
}
_onEndCall() async {
await VoipSendbirdPlugin.endCall();
}
_onCall(String callId) async {
await VoipSendbirdPlugin.call(callId);
}
_onAcceptCall() async {
await VoipSendbirdPlugin.acceptCall();
}
connectAuthChannel() {
_authListener = VoipSendbirdPlugin.authListener('authChannel')
..listen((result) {
if (result == successStatusCode) {
setState(() {
_showLoginBtn = false;
_showLogOutBtn = true;
_showMakeCallBtn = true;
});
} else if (result == logoutStatusCode) {
setState(() {
_showLoginBtn = true;
_showLogOutBtn = false;
_showMakeCallBtn = false;
});
}
});
}
connectDirectCallChannel() {
_directCallListener =
VoipSendbirdPlugin.directCallListener('directCallChannel')
..listen((result) {
setState(() {
_makeCallResult = result;
if (result == dialingStatusCode) {
_showLogOutBtn = false;
_showCallOptionBtn = true;
_showMakeCallBtn = false;
} else if (result == endStatusCode) {
_showLogOutBtn = true;
_showCallOptionBtn = false;
_showMakeCallBtn = true;
}
});
});
}
connectRingingChannel() {
_ringingListener = VoipSendbirdPlugin.ringingListener('ringingChannel')
..listen((result) {
print(result);
setState(() {
if (result == riningStatusCode) {
_showRingingBtn = true;
} else if (result == connectStatusCode) {
_showLogOutBtn = false;
_showCallOptionBtn = true;
_showMakeCallBtn = false;
_showRingingBtn = false;
} else if (result == endStatusCode) {
_showLogOutBtn = true;
_showCallOptionBtn = false;
_showMakeCallBtn = true;
_showRingingBtn = false;
}
});
});
_initialSendBird();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurpleAccent,
title: Text('SendBird - Flutter VOIP demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Text('UserId : '),
),
Container(
margin: EdgeInsets.all(5),
width: 200,
child: TextField(
decoration: InputDecoration(
hintText: _userId,
hintStyle: TextStyle(color: Colors.black12),
contentPadding: EdgeInsets.symmetric(vertical: 0),
),
controller: textUserIdController,
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Text('CalleeId : '),
),
Container(
margin: EdgeInsets.all(5),
width: 200,
child: TextField(
decoration: InputDecoration(
hintText: _callId,
hintStyle: TextStyle(color: Colors.black12),
contentPadding: EdgeInsets.symmetric(vertical: 0),
),
controller: textCalleeController,
),
),
],
),
Container(
margin: EdgeInsets.only(top: 40),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
margin: EdgeInsets.all(5),
child: Visibility(
visible: _showLogOutBtn,
child: RaisedButton(
padding: EdgeInsets.all(5),
onPressed: _onLogOut,
child: Text('LogOut'),
),
),
),
Container(
margin: EdgeInsets.all(5),
child: Visibility(
visible: _showLoginBtn,
child: RaisedButton(
padding: EdgeInsets.all(5),
onPressed: _onLogin,
child: Text('Login'),
),
),
),
Container(
margin: EdgeInsets.all(5),
child: Visibility(
visible: _showMakeCallBtn,
child: RaisedButton(
padding: EdgeInsets.all(5),
onPressed: () {
if (textCalleeController.text == null ||
textCalleeController.text == '') {
_onCall(_callId);
} else {
_onCall(textCalleeController.text);
}
},
child: Text('MakeCall'),
),
),
)
],
),
),
Visibility(
visible: _showRingingBtn,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
margin: EdgeInsets.all(5),
child: IconButton(
padding: EdgeInsets.all(5),
onPressed: () {
_onAcceptCall();
},
icon: Icon(
Icons.call,
color: Colors.white,
)),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.green,
),
),
SizedBox(
width: 80,
),
Container(
margin: EdgeInsets.all(5),
child: IconButton(
padding: EdgeInsets.all(5),
onPressed: () {
_onEndCall();
},
icon: Icon(
Icons.call_end,
color: Colors.white,
)),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.red,
),
),
],
),
),
Visibility(
visible: _showCallOptionBtn,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
margin: EdgeInsets.all(5),
child: IconButton(
padding: EdgeInsets.all(5),
onPressed: () {
_onEndCall();
},
icon: Icon(
Icons.call_end,
color: Colors.white,
)),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.red,
),
),
SizedBox(
width: 80,
),
Container(
margin: EdgeInsets.all(5),
child: IconButton(
padding: EdgeInsets.all(5),
onPressed: () {
_onToggleAudioDevice();
},
icon: Icon(
Icons.volume_up,
color: Colors.white,
),
),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.black54,
),
),
],
),
),
Container(
margin: EdgeInsets.only(top: 20),
child: Text("Call status : $_makeCallResult")),
],
),
),
),
),
);
}
}