import 'dart:developer'; import 'package:wanisclient/config/enum/ride_enum.dart'; import 'package:flutter/material.dart'; import 'package:overlay_support/overlay_support.dart'; import '../models/ride_model.dart'; import '../services/agouranotificationservice.dart'; import '../services/notificaitons_services.dart'; import '../utils/colors.dart'; import 'video_call_widget.dart'; class BeemKidsVideoCall extends StatefulWidget { final RideDto ride; final bool isDriver; const BeemKidsVideoCall({ Key? key, required this.ride, required this.isDriver, }) : super(key: key); @override State createState() => _BeemKidsVideoCallState(); } class _BeemKidsVideoCallState extends State { bool _isCallActive = false; OverlaySupportEntry? _callOverlay; @override void initState() { super.initState(); // Check status when the widget is first built WidgetsBinding.instance.addPostFrameCallback((_) { if (mounted) _checkRideStatus(); }); } // ADD THIS LIFECYCLE METHOD @override void didUpdateWidget(BeemKidsVideoCall oldWidget) { super.didUpdateWidget(oldWidget); // Check if the ride status has changed from something else TO "RIDE_START" if (widget.ride.currentridetypesocket != oldWidget.ride.currentridetypesocket && widget.ride.currentridetypesocket == "RIDE_START") { if (mounted) { _checkRideStatus(); } } } void _checkRideStatus() { // Listen to ride status changes if (widget.ride.iskidsride == true && 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(autoAnswer: true), duration: const Duration(seconds: 10), ); }); } void _startCall({bool autoAnswer = false}) { log("start !!!!!!!!!!!!!!!!!!. $_isCallActive $_callOverlay"); // Avoid creating multiple overlays if (_isCallActive || _callOverlay != null) return; setState(() { _isCallActive = true; }); log("start !!!!!!!!!!!!!!!!!! 2 . $_isCallActive $_callOverlay"); // 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) { if (mounted) { setState(() => _isCallActive = isActive); } }, ), duration: Duration.zero, // Persistent overlay ); } void _onCallEnded() { if (mounted) { setState(() { _isCallActive = false; }); } _callOverlay?.dismiss(animate: true); _callOverlay = null; } @override void dispose() { _callOverlay?.dismiss(); super.dispose(); } @override Widget build(BuildContext context) { if (widget.ride.iskidsride != true) { return const SizedBox.shrink(); } // This UI is now driven by the _isCallActive state, // which is correctly managed by the lifecycle methods. 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), 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(autoAnswer: true), icon: const Icon(Icons.video_call), label: Text(_isCallActive ? 'Call Active' : 'Start Video Call'), style: ElevatedButton.styleFrom( backgroundColor: AppNewColors.primaryColor, 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 Text( 'Video call active', style: TextStyle(color: Colors.green, fontSize: 12), ), const Spacer(), if (!widget.isDriver) TextButton( onPressed: _onCallEnded, child: const Text('End Call', style: TextStyle(color: Colors.red)), ), ], ), ), ], ), ); } }