import 'dart:async'; import 'package:SEFER/constant/box_name.dart'; import 'package:SEFER/main.dart'; import 'package:flutter/material.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:agora_rtc_engine/agora_rtc_engine.dart'; import '../../../../constant/api_key.dart'; const String appId = AK.agoraAppId; class DriverCallPage extends StatefulWidget { const DriverCallPage({super.key}); @override State createState() => _DriverCallPageState(); } class _DriverCallPageState extends State { String channelName = 'sefer300'; String token = "00612994c6e707543e68d5638894d04f989IAACchY2SBwRcuw2mt+BocxbF+fmFKvjOS/hekkirRWfuqMLiA8AAAAAEADs3TvfbjrSZQEAAQAAAAAA"; // int uid = int.parse(box.read(BoxName.phoneDriver)); // uid of the local user int uid = 0; int? _remoteUid; // uid of the remote user bool _isJoined = false; // Indicates if the local user has joined the channel late RtcEngine agoraEngine; // Agora engine instance final GlobalKey scaffoldMessengerKey = GlobalKey(); // Global key to access the scaffold showMessage(String message) { scaffoldMessengerKey.currentState?.showSnackBar(SnackBar( content: Text(message), )); } @override void initState() { super.initState(); _remoteUid = box.read(BoxName.phone) != null ? int.parse(box.read(BoxName.phone)) : int.parse(box.read(BoxName.phoneDriver)); uid = box.read(BoxName.phoneDriver) != null ? int.parse(box.read(BoxName.phoneDriver)) : int.parse(box.read(BoxName.phone)); print('remoteid is $_remoteUid'); print('uid is $uid'); // Set up an instance of Agora engine setupVoiceSDKEngine(); } Future setupVoiceSDKEngine() async { // retrieve or request microphone permission await [Permission.microphone].request(); //create an instance of the Agora engine agoraEngine = createAgoraRtcEngine(); await agoraEngine.initialize(const RtcEngineContext(appId: AK.agoraAppId)); print('eeeeeeeeeeeeeeeeeeee'); print(agoraEngine); // Register the event handler agoraEngine.registerEventHandler( RtcEngineEventHandler( onJoinChannelSuccess: (RtcConnection connection, int elapsed) { showMessage( "Local user uid:${connection.localUid} joined the channel"); setState(() { _isJoined = true; }); }, onUserJoined: (RtcConnection connection, int remoteUid, int elapsed) { showMessage("Remote user uid:$remoteUid joined the channel"); setState(() { _remoteUid = remoteUid; }); }, onUserOffline: (RtcConnection connection, int remoteUid, UserOfflineReasonType reason) { showMessage("Remote user uid:$remoteUid left the channel"); setState(() { _remoteUid = null; }); }, ), ); } void join() async { // Set channel options including the client role and channel profile ChannelMediaOptions options = const ChannelMediaOptions( clientRoleType: ClientRoleType.clientRoleBroadcaster, channelProfile: ChannelProfileType.channelProfileCommunication, ); await agoraEngine.joinChannel( token: token, channelId: channelName, options: options, uid: uid, ); } //https://console.agora.io/invite?sign=5e9e22d06f22caeeada9954c9e908572%253A5ba8aed978a35eab5a5113742502ded2a41478b2a81cb19c71a30776e125b58a void leave() { setState(() { _isJoined = false; _remoteUid = null; }); agoraEngine.leaveChannel(); } // Clean up the resources when you leave @override void dispose() async { await agoraEngine.leaveChannel(); super.dispose(); } // Build UI @override Widget build(BuildContext context) { return MaterialApp( scaffoldMessengerKey: scaffoldMessengerKey, home: Scaffold( appBar: AppBar( title: const Text('Get started with Voice Calling'), ), body: ListView( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4), children: [ // Status text Container(height: 40, child: Center(child: _status())), // Button Row Row( children: [ Expanded( child: ElevatedButton( child: const Text("Join"), onPressed: () => {join()}, ), ), const SizedBox(width: 10), Expanded( child: ElevatedButton( child: const Text("Leave"), onPressed: () => {leave()}, ), ), ], ), ], )), ); } Widget _status() { String statusText; if (!_isJoined) statusText = 'Join a channel'; else if (_remoteUid == null) statusText = 'Waiting for a remote user to join...'; else statusText = 'Connected to remote user, uid:$_remoteUid'; return Text( statusText, ); } }