import 'dart:developer'; import 'package:wanisdriver/models/Ride.dart'; import 'package:wanisdriver/utils/colors.dart'; import 'package:flutter/material.dart'; import 'package:overlay_support/overlay_support.dart'; import '../services/agouranotificationservice.dart'; import '../services/notificaitons_services.dart'; import 'video_call_widget.dart'; import 'custom_text.dart'; class WanisKidsVideoCall extends StatefulWidget { final Ride ride; final bool isDriver; const WanisKidsVideoCall({ Key? key, required this.ride, required this.isDriver, }) : super(key: key); @override State createState() => _WanisKidsVideoCallState(); } class _WanisKidsVideoCallState extends State { bool _isCallActive = false; bool _showCallWidget = false; OverlaySupportEntry? _callOverlay; @override void initState() { log("initState --- 000 "); super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) { log("addPostFrameCallback --- "); if (mounted) _checkRideStatus(); }); // _checkRideStatus(); } void _checkRideStatus() { log("_checkRideStatus --- ${widget.ride.iskidsride}"); log("_checkRideStatus --- ${widget.ride.iskidsride}"); log("_checkRideStatus --- ${widget.ride.currentridetypesocket}"); // Listen to ride status changes if (widget.ride.iskidsride!=null && widget.ride.iskidsride! && widget.ride.currentridetypesocket == "RIDE_START") { if (widget.isDriver) { // Auto-answer for driver _startCall(autoAnswer: true); } else { // Show call option for parent _showCallOption(); } } } void _showCallOption() { WidgetsBinding.instance.addPostFrameCallback((_) { if (!mounted) return; AgoraNotificationService.showCallNotification( title: 'Wanis Kids', subtitle: 'Tap to start video call with driver', onTap: () => _startCall(), duration: const Duration(seconds: 5), ); }); } void _startCall({bool autoAnswer = false}) { log("_startCall --- 01"); setState(() { _showCallWidget = true; _isCallActive = true; }); // Create overlay for background calling _callOverlay = showOverlay( (context, progress) => VideoCallWidget( channelName: 'wanis_kids_${widget.ride.id}', isDriver: widget.isDriver, autoAnswer: autoAnswer, onCallEnded: _onCallEnded, onCallStateChanged: (isActive) { setState(() => _isCallActive = isActive); }, ), duration: Duration.zero, // Persistent overlay ); } void _onCallEnded() { setState(() { _isCallActive = false; _showCallWidget = false; }); _callOverlay?.dismiss(animate: true); _callOverlay = null; } @override Widget build(BuildContext context) { if (widget.ride.iskidsride==null || widget.ride.iskidsride==false) { return const SizedBox.shrink(); } return Container( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Row( children: [ Icon(Icons.child_care, color: Colors.orange), SizedBox(width: 8), Txt( text: 'Wanis Kids Ride', style: TextStyle( fontSize: 16, fontWeight: FontWeight.bold, color: Colors.orange, ), ), ], ), const SizedBox(height: 8), if (widget.ride.currentridetypesocket == "RIDE_START" && !widget.isDriver) ElevatedButton.icon( onPressed: _isCallActive ? null : () => _startCall(), icon: const Icon(Icons.video_call), label: Txt(text: _isCallActive ? 'Call Active' : 'Start Video Call'), style: ElevatedButton.styleFrom( backgroundColor: AppColors.primaryNewColor, foregroundColor: Colors.white, ), ), if (_isCallActive) Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration( color: Colors.green.withOpacity(0.1), borderRadius: BorderRadius.circular(8), border: Border.all(color: Colors.green), ), child: Row( children: [ const Icon(Icons.video_call, color: Colors.green, size: 16), const SizedBox(width: 8), const Txt( text: 'Video call active', style: TextStyle(color: Colors.green, fontSize: 12), ), const Spacer(), if (!widget.isDriver) TextButton( onPressed: _onCallEnded, child: const Txt(text: 'End Call', style: TextStyle(color: Colors.red)), ), ], ), ), ], ), ); } @override void dispose() { _callOverlay?.dismiss(); super.dispose(); } }