Files
Aira-Mobile-App/lib/screens/webview_screen.dart
T

241 lines
8.0 KiB
Dart

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:webview_flutter_android/webview_flutter_android.dart';
import 'package:url_launcher/url_launcher.dart';
import '../config/app_config.dart';
class WebViewScreen extends StatefulWidget {
const WebViewScreen({super.key});
@override
State<WebViewScreen> createState() => _WebViewScreenState();
}
class _WebViewScreenState extends State<WebViewScreen> {
late final WebViewController _controller;
bool _isLoading = true;
int _progress = 0;
bool _hasError = false;
String _errorMessage = "";
@override
void initState() {
super.initState();
_initWebViewController();
}
void _initWebViewController() {
late final PlatformWebViewControllerCreationParams params;
if (WebViewPlatform.instance is AndroidWebViewPlatform) {
params = AndroidWebViewControllerCreationParams();
} else {
params = const PlatformWebViewControllerCreationParams();
}
final WebViewController controller =
WebViewController.fromPlatformCreationParams(params);
controller
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(const Color(0xFF09090B))
..setUserAgent(AppConfig.customUserAgent)
..setNavigationDelegate(
NavigationDelegate(
onProgress: (int progress) {
if (mounted) {
setState(() {
_progress = progress;
if (progress >= 100) {
_isLoading = false;
}
});
}
},
onPageStarted: (String url) {
if (mounted) {
setState(() {
_isLoading = true;
_hasError = false;
});
}
},
onPageFinished: (String url) {
if (mounted) {
setState(() {
_isLoading = false;
});
}
},
onWebResourceError: (WebResourceError error) {
// Filter out minor subresource errors
if (error.isForMainFrame ?? true) {
if (mounted) {
setState(() {
_hasError = true;
_errorMessage = error.description;
_isLoading = false;
});
}
}
},
onNavigationRequest: (NavigationRequest request) async {
final uri = Uri.parse(request.url);
// Allow all Vercel and Aira domain navigations
if (request.url.contains("vercel.app") ||
request.url.contains("airagroup.in") ||
request.url.contains("accounts.google.com") ||
request.url.contains("googleapis.com")) {
return NavigationDecision.navigate;
}
// Launch external URLs in default device browser
if (await canLaunchUrl(uri)) {
await launchUrl(uri, mode: LaunchMode.externalApplication);
return NavigationDecision.prevent;
}
return NavigationDecision.navigate;
},
),
);
// Android-specific hardware and permission enhancements
if (controller.platform is AndroidWebViewController) {
final androidController = controller.platform as AndroidWebViewController;
// Auto-grant Camera and Microphone permissions for Voice chat & Image attachments
androidController.setOnPlatformPermissionRequest(
(PlatformWebViewPermissionRequest request) {
request.grant();
},
);
androidController.setMediaPlaybackRequiresUserGesture(false);
}
controller.loadRequest(Uri.parse(AppConfig.webUrl));
_controller = controller;
}
void _reload() {
setState(() {
_isLoading = true;
_hasError = false;
});
_controller.reload();
}
@override
Widget build(BuildContext context) {
return PopScope(
canPop: false,
onPopInvokedWithResult: (bool didPop, dynamic result) async {
if (didPop) return;
// Handle Android back button
if (await _controller.canGoBack()) {
await _controller.goBack();
} else {
// If on root page, minimize or prompt exit
SystemNavigator.pop();
}
},
child: Scaffold(
backgroundColor: const Color(0xFF09090B),
body: SafeArea(
bottom: false,
child: Stack(
children: [
// Core WebView
if (!_hasError)
WebViewWidget(controller: _controller),
// Top Loading Progress Bar
if (_isLoading)
Positioned(
top: 0,
left: 0,
right: 0,
child: LinearProgressIndicator(
value: _progress / 100.0,
minHeight: 2.5,
backgroundColor: Colors.transparent,
valueColor: const AlwaysStoppedAnimation<Color>(
Color(0xFF3B82F6),
),
),
),
// Offline / Network Error Screen
if (_hasError)
Center(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 32.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: 64,
height: 64,
decoration: BoxDecoration(
color: Colors.redAccent.withOpacity(0.1),
shape: BoxShape.circle,
),
child: const Icon(
Icons.wifi_off_rounded,
size: 32,
color: Colors.redAccent,
),
),
const SizedBox(height: 20),
const Text(
"Connection Issue",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
const SizedBox(height: 8),
Text(
_errorMessage.isNotEmpty
? _errorMessage
: "Unable to reach Aira AI servers. Please check your internet connection.",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 13,
color: Colors.white.withOpacity(0.6),
),
),
const SizedBox(height: 24),
ElevatedButton.icon(
onPressed: _reload,
icon: const Icon(Icons.refresh_rounded, size: 18),
label: const Text("Retry Connection"),
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF2563EB),
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(
horizontal: 24,
vertical: 12,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
],
),
),
),
],
),
),
),
);
}
}