|
| 1 | +import 'dart:io' show Platform; |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:flutter_redux/flutter_redux.dart'; |
| 6 | +import 'package:webview_flutter/webview_flutter.dart'; |
| 7 | + |
| 8 | +import '../flet_app_services.dart'; |
| 9 | +import '../models/app_state.dart'; |
| 10 | +import '../models/control.dart'; |
| 11 | +import '../models/control_tree_view_model.dart'; |
| 12 | +import '../utils/colors.dart'; |
| 13 | +import 'create_control.dart'; |
| 14 | +import 'error.dart'; |
| 15 | + |
| 16 | +class WebViewControl extends StatelessWidget { |
| 17 | + final Control? parent; |
| 18 | + final Control control; |
| 19 | + final bool parentDisabled; |
| 20 | + |
| 21 | + const WebViewControl( |
| 22 | + {Key? key, |
| 23 | + required this.parent, |
| 24 | + required this.control, |
| 25 | + required this.parentDisabled}) |
| 26 | + : super(key: key); |
| 27 | + |
| 28 | + @override |
| 29 | + Widget build(BuildContext context) { |
| 30 | + var result = StoreConnector<AppState, ControlTreeViewModel>( |
| 31 | + distinct: true, |
| 32 | + converter: (store) => ControlTreeViewModel.fromStore(store, control), |
| 33 | + builder: (context, viewModel) { |
| 34 | + debugPrint("WebViewControl build: ${control.id}"); |
| 35 | + |
| 36 | + String url = control.attrString("url", "")!; |
| 37 | + if (url == "") { |
| 38 | + return const ErrorControl("WebView.url cannot be empty."); |
| 39 | + } |
| 40 | + |
| 41 | + bool javascriptEnabled = |
| 42 | + control.attrBool("javascriptEnabled", false)!; |
| 43 | + var bgcolor = HexColor.fromString( |
| 44 | + Theme.of(context), control.attrString("bgcolor", "")!); |
| 45 | + String preventLink = control.attrString("preventLink", "")!; |
| 46 | + |
| 47 | + if (Platform.isIOS || Platform.isAndroid) { |
| 48 | + var controller = WebViewController() |
| 49 | + ..setJavaScriptMode(javascriptEnabled |
| 50 | + ? JavaScriptMode.unrestricted |
| 51 | + : JavaScriptMode.disabled) |
| 52 | + ..setNavigationDelegate( |
| 53 | + NavigationDelegate( |
| 54 | + onProgress: (int progress) {}, |
| 55 | + onPageStarted: (String url) { |
| 56 | + FletAppServices.of(context).server.sendPageEvent( |
| 57 | + eventTarget: control.id, |
| 58 | + eventName: "page_started", |
| 59 | + eventData: url); |
| 60 | + }, |
| 61 | + onPageFinished: (String url) { |
| 62 | + FletAppServices.of(context).server.sendPageEvent( |
| 63 | + eventTarget: control.id, |
| 64 | + eventName: "page_ended", |
| 65 | + eventData: url); |
| 66 | + }, |
| 67 | + onWebResourceError: (WebResourceError error) { |
| 68 | + FletAppServices.of(context).server.sendPageEvent( |
| 69 | + eventTarget: control.id, |
| 70 | + eventName: "web_resource_error", |
| 71 | + eventData: error.toString()); |
| 72 | + }, |
| 73 | + onNavigationRequest: (NavigationRequest request) { |
| 74 | + if (preventLink != "" && |
| 75 | + request.url.startsWith(preventLink)) { |
| 76 | + return NavigationDecision.prevent; |
| 77 | + } |
| 78 | + return NavigationDecision.navigate; |
| 79 | + }, |
| 80 | + ), |
| 81 | + ); |
| 82 | + if (bgcolor != null) { |
| 83 | + controller.setBackgroundColor(bgcolor); |
| 84 | + } |
| 85 | + controller.loadRequest(Uri.parse(url)); |
| 86 | + return WebViewWidget(controller: controller); |
| 87 | + } else { |
| 88 | + return const ErrorControl( |
| 89 | + "WebView control is not supported on this platform yet."); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + return constrainedControl(context, result, parent, control); |
| 94 | + } |
| 95 | +} |
0 commit comments