Skip to content

Commit 2a2d063

Browse files
WebView control (#2049)
* Supporting `WebView` control (#1865) * Supporting WebView for mobile. * update `MobileWebViewer` with document * remove Unnecessary flutter dependency * Update `MobileWebView` to become `WebView` As Discussed in the PR, we will make the WebView Universal instead of Separating for each platform. * Update python SDK to support the updates about the `WebView` --------- Co-authored-by: Feodor Fitsner <[email protected]> * Added `--host` option to `flet run` command. * WebView control --------- Co-authored-by: Kot <[email protected]>
1 parent 6d0fc80 commit 2a2d063

File tree

12 files changed

+405
-11
lines changed

12 files changed

+405
-11
lines changed

client/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
buildscript {
2-
ext.kotlin_version = '1.6.10'
2+
ext.kotlin_version = '1.7.10'
33
repositories {
44
google()
55
mavenCentral()

client/ios/Podfile.lock

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ PODS:
5252
- SwiftyGif (5.4.3)
5353
- url_launcher_ios (0.0.1):
5454
- Flutter
55+
- webview_flutter_wkwebview (0.0.1):
56+
- Flutter
5557

5658
DEPENDENCIES:
5759
- audioplayers_darwin (from `.symlinks/plugins/audioplayers_darwin/ios`)
@@ -60,8 +62,9 @@ DEPENDENCIES:
6062
- integration_test (from `.symlinks/plugins/integration_test/ios`)
6163
- path_provider_foundation (from `.symlinks/plugins/path_provider_foundation/darwin`)
6264
- sensors_plus (from `.symlinks/plugins/sensors_plus/ios`)
63-
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/ios`)
65+
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)
6466
- url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`)
67+
- webview_flutter_wkwebview (from `.symlinks/plugins/webview_flutter_wkwebview/ios`)
6568

6669
SPEC REPOS:
6770
trunk:
@@ -84,9 +87,11 @@ EXTERNAL SOURCES:
8487
sensors_plus:
8588
:path: ".symlinks/plugins/sensors_plus/ios"
8689
shared_preferences_foundation:
87-
:path: ".symlinks/plugins/shared_preferences_foundation/ios"
90+
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"
8891
url_launcher_ios:
8992
:path: ".symlinks/plugins/url_launcher_ios/ios"
93+
webview_flutter_wkwebview:
94+
:path: ".symlinks/plugins/webview_flutter_wkwebview/ios"
9095

9196
SPEC CHECKSUMS:
9297
audioplayers_darwin: 877d9a4d06331c5c374595e46e16453ac7eafa40
@@ -95,12 +100,13 @@ SPEC CHECKSUMS:
95100
file_picker: ce3938a0df3cc1ef404671531facef740d03f920
96101
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
97102
integration_test: 13825b8a9334a850581300559b8839134b124670
98-
path_provider_foundation: eaf5b3e458fc0e5fbb9940fb09980e853fe058b8
103+
path_provider_foundation: 29f094ae23ebbca9d3d0cec13889cd9060c0e943
99104
SDWebImage: 72f86271a6f3139cc7e4a89220946489d4b9a866
100105
sensors_plus: 5717760720f7e6acd96fdbd75b7428f5ad755ec2
101-
shared_preferences_foundation: 297b3ebca31b34ec92be11acd7fb0ba932c822ca
106+
shared_preferences_foundation: 5b919d13b803cadd15ed2dc053125c68730e5126
102107
SwiftyGif: 6c3eafd0ce693cad58bb63d2b2fb9bacb8552780
103-
url_launcher_ios: ae1517e5e344f5544fb090b079e11f399dfbe4d2
108+
url_launcher_ios: 08a3dfac5fb39e8759aeb0abbd5d9480f30fc8b4
109+
webview_flutter_wkwebview: 2e2d318f21a5e036e2c3f26171342e95908bd60a
104110

105111
PODFILE CHECKSUM: ef19549a9bc3046e7bb7d2fab4d021637c0c58a3
106112

client/ios/Runner.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@
155155
97C146E61CF9000F007C117D /* Project object */ = {
156156
isa = PBXProject;
157157
attributes = {
158-
LastUpgradeCheck = 1300;
158+
LastUpgradeCheck = 1430;
159159
ORGANIZATIONNAME = "";
160160
TargetAttributes = {
161161
97C146ED1CF9000F007C117D = {

client/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1300"
3+
LastUpgradeVersion = "1430"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

client/pubspec.lock

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,38 @@ packages:
779779
url: "https://pub.dev"
780780
source: hosted
781781
version: "3.0.2"
782+
webview_flutter:
783+
dependency: transitive
784+
description:
785+
name: webview_flutter
786+
sha256: "82f6787d5df55907aa01e49bd9644f4ed1cc82af7a8257dd9947815959d2e755"
787+
url: "https://pub.dev"
788+
source: hosted
789+
version: "4.2.4"
790+
webview_flutter_android:
791+
dependency: transitive
792+
description:
793+
name: webview_flutter_android
794+
sha256: ddc167c6676f57c8b367d19fcbee267d6dc6adf81bd6c3cb87981d30746e0a6d
795+
url: "https://pub.dev"
796+
source: hosted
797+
version: "3.10.1"
798+
webview_flutter_platform_interface:
799+
dependency: transitive
800+
description:
801+
name: webview_flutter_platform_interface
802+
sha256: "6d9213c65f1060116757a7c473247c60f3f7f332cac33dc417c9e362a9a13e4f"
803+
url: "https://pub.dev"
804+
source: hosted
805+
version: "2.6.0"
806+
webview_flutter_wkwebview:
807+
dependency: transitive
808+
description:
809+
name: webview_flutter_wkwebview
810+
sha256: "485af05f2c5f83c7f78c20e236b170ad02df7153b299ae9917345be43871d29f"
811+
url: "https://pub.dev"
812+
source: hosted
813+
version: "3.8.0"
782814
win32:
783815
dependency: transitive
784816
description:

package/lib/src/controls/create_control.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import 'textfield.dart';
7676
import 'tooltip.dart';
7777
import 'transparent_pointer.dart';
7878
import 'vertical_divider.dart';
79+
import 'webview.dart';
7980
import 'window_drag_area.dart';
8081

8182
Widget createControl(Control? parent, String id, bool parentDisabled,
@@ -570,6 +571,12 @@ Widget createWidget(Key? key, ControlViewModel controlView, Control? parent,
570571
control: controlView.control,
571572
children: controlView.children,
572573
parentDisabled: parentDisabled);
574+
case "webview":
575+
return WebViewControl(
576+
key: key,
577+
parent: parent,
578+
control: controlView.control,
579+
parentDisabled: parentDisabled);
573580
default:
574581
throw Exception("Unknown control type: ${controlView.control.type}");
575582
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}

package/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ dependencies:
4242
js: ^0.6.5
4343
fl_chart: ^0.64.0
4444

45+
webview_flutter: ^4.2.4
46+
4547
dev_dependencies:
4648
flutter_test:
4749
sdk: flutter

sdk/python/packages/flet-core/src/flet_core/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,5 +204,6 @@
204204
from flet_core.vertical_divider import VerticalDivider
205205
from flet_core.view import View
206206
from flet_core.window_drag_area import WindowDragArea
207+
from flet_core.webview import WebView
207208
from flet_core.range_slider import RangeSlider
208209
from flet_core.badge import Badge

0 commit comments

Comments
 (0)