You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
154 lines
5.1 KiB
Dart
154 lines
5.1 KiB
Dart
import 'package:easy_localization/easy_localization.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
|
|
import 'package:mohem_flutter_app/classes/consts.dart';
|
|
import 'package:mohem_flutter_app/config/routes.dart';
|
|
import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
|
|
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
class AppInAppWebViewScreen extends StatefulWidget {
|
|
final String url;
|
|
final String? title;
|
|
|
|
const AppInAppWebViewScreen({
|
|
Key? key,
|
|
required this.url,
|
|
this.title,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
State<AppInAppWebViewScreen> createState() => _AppInAppWebViewScreenState();
|
|
}
|
|
|
|
class _AppInAppWebViewScreenState extends State<AppInAppWebViewScreen> {
|
|
InAppWebViewController? _webViewController;
|
|
double _progress = 0;
|
|
String _title = "";
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_title = widget.title ?? "";
|
|
_requestCameraPermission();
|
|
}
|
|
|
|
Future<void> _requestCameraPermission() async {
|
|
try {
|
|
PermissionStatus cameraStatus = await Permission.camera.status;
|
|
if (!cameraStatus.isGranted) {
|
|
await Permission.camera.request();
|
|
}
|
|
} catch (e) {
|
|
debugPrint("Error requesting camera permission: $e");
|
|
}
|
|
}
|
|
|
|
void _onHomeTapped() {
|
|
if (ApiConsts.isDependetLogin) {
|
|
Navigator.popUntil(context, ModalRoute.withName(AppRoutes.dashboardScreenForDependet));
|
|
} else {
|
|
Navigator.popUntil(context, ModalRoute.withName(AppRoutes.dashboard));
|
|
}
|
|
}
|
|
|
|
Future<void> _handleBack() async {
|
|
if (_webViewController != null && await _webViewController!.canGoBack()) {
|
|
await _webViewController!.goBack();
|
|
} else {
|
|
if (mounted) {
|
|
Navigator.pop(context);
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
String displayTitle = _title.isNotEmpty ? _title : LocaleKeys.offerDetails.tr();
|
|
|
|
return PopScope(
|
|
canPop: false,
|
|
onPopInvokedWithResult: (bool didPop, Object? result) async {
|
|
if (didPop) return;
|
|
await _handleBack();
|
|
},
|
|
child: Scaffold(
|
|
backgroundColor: Colors.white,
|
|
appBar: AppBarWidget(
|
|
context,
|
|
title: displayTitle,
|
|
showHomeButton: true,
|
|
onHomeTapped: _onHomeTapped,
|
|
onBackTapped: () async {
|
|
await _handleBack();
|
|
},
|
|
),
|
|
body: SafeArea(
|
|
child: Stack(
|
|
children: <Widget>[
|
|
InAppWebView(
|
|
initialUrlRequest: URLRequest(url: WebUri(widget.url)),
|
|
initialSettings: InAppWebViewSettings(
|
|
javaScriptEnabled: true,
|
|
domStorageEnabled: true,
|
|
supportZoom: true,
|
|
mediaPlaybackRequiresUserGesture: false,
|
|
allowsInlineMediaPlayback: true,
|
|
javaScriptCanOpenWindowsAutomatically: true,
|
|
transparentBackground: false,
|
|
useShouldOverrideUrlLoading: false,
|
|
cacheEnabled: true,
|
|
clearCache: false,
|
|
),
|
|
onWebViewCreated: (InAppWebViewController controller) {
|
|
_webViewController = controller;
|
|
},
|
|
onPermissionRequest: (InAppWebViewController controller, PermissionRequest permissionRequest) async {
|
|
debugPrint("InAppWebView onPermissionRequest: ${permissionRequest.resources}");
|
|
try {
|
|
PermissionStatus status = await Permission.camera.status;
|
|
if (!status.isGranted) {
|
|
await Permission.camera.request();
|
|
}
|
|
} catch (e) {
|
|
debugPrint("Error requesting camera permission: $e");
|
|
}
|
|
return PermissionResponse(
|
|
resources: permissionRequest.resources,
|
|
action: PermissionResponseAction.GRANT,
|
|
);
|
|
},
|
|
onProgressChanged: (InAppWebViewController controller, int progress) {
|
|
if (mounted) {
|
|
setState(() {
|
|
_progress = progress / 100;
|
|
});
|
|
}
|
|
},
|
|
onTitleChanged: (InAppWebViewController controller, String? title) {
|
|
if (widget.title == null || widget.title!.isEmpty) {
|
|
if (title != null && title.isNotEmpty && !title.startsWith("http")) {
|
|
if (mounted) {
|
|
setState(() {
|
|
_title = title;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
},
|
|
),
|
|
if (_progress < 1.0)
|
|
LinearProgressIndicator(
|
|
value: _progress,
|
|
color: const Color(0xFF168A89),
|
|
backgroundColor: Colors.transparent,
|
|
minHeight: 2.5,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|