import 'dart:async'; import 'package:SEFER/constant/box_name.dart'; import 'package:SEFER/controller/home/map_passenger_controller.dart'; import 'package:SEFER/main.dart'; import 'package:SEFER/views/widgets/my_scafold.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:agora_rtc_engine/agora_rtc_engine.dart'; import '../../../../constant/api_key.dart'; import '../../../constant/colors.dart'; import '../../../constant/style.dart'; import '../../../controller/firebase/firbase_messge.dart'; String appId = AK.agoraAppId; class PassengerCallPage extends StatefulWidget { const PassengerCallPage({ super.key, required this.channelName, required this.token, required this.remoteID, }); final String channelName, token, remoteID; @override State createState() => _PassengerCallPageState(); } class _PassengerCallPageState extends State { int uid = 0; int? _remoteUid = 0; // uid of the remote user bool _isJoined = false; // Indicates if the local user has joined the channel late RtcEngine agoraEngine; // Agora engine instance String status = ''; final GlobalKey scaffoldMessengerKey = GlobalKey(); // Global key to access the scaffold showMessage(String message) { scaffoldMessengerKey.currentState?.showSnackBar(SnackBar( content: Text(message), )); } initAgora() async { await setupVoiceSDKEngine(); } @override void initState() { super.initState(); _remoteUid = int.parse(widget.remoteID); uid = int.parse(box.read(BoxName.phone)); // Set up an instance of Agora engine initAgora(); } Future setupVoiceSDKEngine() async { // retrieve or request microphone permission await [Permission.microphone].request(); //create an instance of the Agora engine agoraEngine = createAgoraRtcEngine(); await agoraEngine.initialize(RtcEngineContext(appId: AK.agoraAppId)); // Register the event handler agoraEngine.registerEventHandler( RtcEngineEventHandler( onJoinChannelSuccess: (RtcConnection connection, int elapsed) { showMessage( "Local user uid:${connection.localUid} joined the channel"); setState(() { _isJoined = true; status = 'joined'.tr; }); }, onUserJoined: (RtcConnection connection, int remoteUid, int elapsed) { showMessage("Driver joined the channel".tr); setState(() { status = "Driver joined the channel".tr; _remoteUid = remoteUid; }); }, onUserOffline: (RtcConnection connection, int? remoteUid, UserOfflineReasonType reason) { showMessage("Driver left the channel".tr); setState(() { status = "Driver left the channel".tr; _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: widget.token, channelId: widget.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: MyScafolld( // appBar: AppBar( // title: const Text('Get started with Voice Calling'), // ), title: 'Call Page'.tr, isleading: true, body: [ Positioned( top: Get.height * .2, child: Container( height: 100, width: Get.width, decoration: AppStyle.boxDecoration, child: Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ GestureDetector( onTap: () async { // await callController.initAgoraFull(); // callController.join(); // FirebaseMessagesController().sendNotificationToPassengerToken( // 'Call Income', // '${'You have call from driver'.tr} ${box.read(BoxName.nameDriver)}', // Get.find().tokenPassenger, // [ // callController.token, // callController.channelName, // callController.uid.toString(), // callController.remoteUid.toString(), // ], // ); join(); // callController.fetchToken(); }, child: Container( width: 50, height: 50, decoration: const BoxDecoration( shape: BoxShape.circle, color: AppColor.greenColor), child: const Icon( Icons.phone, size: 35, color: AppColor.secondaryColor, )), ), Column( children: [ Text( status, style: AppStyle.title, ), Text('Driver Name'), ], ), GestureDetector( onTap: () async { FirebaseMessagesController() .sendNotificationToPassengerToken( 'Call End'.tr, 'Call End', Get.find().driverToken, [], 'iphone_ringtone.wav', ); leave(); Get.back(); }, child: Container( width: 50, height: 50, decoration: const BoxDecoration( shape: BoxShape.circle, color: AppColor.redColor), child: const Icon( Icons.phone_disabled_sharp, size: 35, color: AppColor.secondaryColor, )), ) ], ), // ignore: prefer_const_constructors ), ), // 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: Text("Join".tr), // onPressed: () => {join()}, // ), // ), // const SizedBox(width: 10), // Expanded( // child: ElevatedButton( // child: Text("Leave".tr), // onPressed: () => {leave()}, // ), // ), // ], // ), // ], // ), ]), ); } // Widget _status() { // String statusText; // // if (!_isJoined) { // statusText = 'Join a channel'.tr; // } else if (_remoteUid == null) // statusText = 'Waiting for a remote user to join...'; // else // statusText = 'Connected to remote user, uid:$_remoteUid'; // // return Text( // statusText, // ); // } }