Skip to content

Commit eb02f8a

Browse files
Update dependency webview_flutter to v4 (#841)
* Update `io.dart` because of breaking changes Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Dao Hoang Son <[email protected]>
1 parent f8f162c commit eb02f8a

File tree

11 files changed

+223
-293
lines changed

11 files changed

+223
-293
lines changed

demo_app/integration_test/auto_resize_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ class WebViewTestCase {
9292
autoResize: true,
9393
autoResizeIntervals: [interval, interval * 2, interval * 3],
9494
debuggingEnabled: true,
95-
unsupportedWorkaroundForIssue375: issue375,
9695
);
9796
final test = _AspectRatioTest(child: webView);
9897

demo_app/pubspec.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -896,28 +896,28 @@ packages:
896896
name: webview_flutter
897897
url: "https://pub.dartlang.org"
898898
source: hosted
899-
version: "3.0.4"
899+
version: "4.0.1"
900900
webview_flutter_android:
901901
dependency: transitive
902902
description:
903903
name: webview_flutter_android
904904
url: "https://pub.dartlang.org"
905905
source: hosted
906-
version: "2.10.4"
906+
version: "3.1.1"
907907
webview_flutter_platform_interface:
908908
dependency: transitive
909909
description:
910910
name: webview_flutter_platform_interface
911911
url: "https://pub.dartlang.org"
912912
source: hosted
913-
version: "1.9.5"
913+
version: "2.0.0"
914914
webview_flutter_wkwebview:
915915
dependency: transitive
916916
description:
917917
name: webview_flutter_wkwebview
918918
url: "https://pub.dartlang.org"
919919
source: hosted
920-
version: "2.9.5"
920+
version: "3.0.1"
921921
win32:
922922
dependency: transitive
923923
description:

packages/fwfh_webview/example/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import 'package:fwfh_webview/fwfh_webview.dart';
55
void main() => runApp(const MyApp());
66

77
class MyApp extends StatelessWidget {
8-
const MyApp({Key? key}) : super(key: key);
8+
const MyApp({super.key});
99

1010
@override
1111
Widget build(BuildContext context) {
Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,76 @@
11
import 'dart:async';
2-
import 'dart:math';
32

4-
import 'package:flutter/foundation.dart';
53
import 'package:flutter/widgets.dart';
64
import 'package:webview_flutter/webview_flutter.dart' as lib;
5+
import 'package:webview_flutter_android/webview_flutter_android.dart' as lib;
6+
import 'package:webview_flutter_wkwebview/webview_flutter_wkwebview.dart'
7+
as lib;
78

89
import 'web_view.dart';
910

1011
class WebViewState extends State<WebView> {
1112
final _timers = <Timer>[];
13+
late final lib.WebViewController _controller;
1214

1315
late double _aspectRatio;
1416
String? _firstFinishedUrl;
1517
_Issue37? _issue37;
16-
lib.WebViewController? _wvc;
1718

1819
@override
1920
void initState() {
2021
super.initState();
2122
_aspectRatio = widget.aspectRatio;
2223

24+
_initController();
25+
2326
if (widget.unsupportedWorkaroundForIssue37) {
2427
_issue37 = _Issue37(this);
25-
_widgetsBindingInstance?.addObserver(_issue37!);
28+
WidgetsBinding.instance.addObserver(_issue37!);
2629
}
2730
}
2831

29-
@override
30-
Widget build(BuildContext context) {
31-
final webView = _buildWebView();
32-
33-
if (widget.unsupportedWorkaroundForIssue375 &&
34-
defaultTargetPlatform == TargetPlatform.android) {
35-
return LayoutBuilder(
36-
builder: (context, constraints) {
37-
final width = constraints.hasBoundedWidth
38-
? constraints.maxWidth
39-
: MediaQuery.of(context).size.width;
40-
final height = width / _aspectRatio;
41-
return SizedBox(
42-
height: min(
43-
height,
44-
constraints.hasBoundedHeight
45-
? constraints.maxHeight
46-
: MediaQuery.of(context).size.height,
47-
),
48-
width: width,
49-
child: webView,
50-
);
51-
},
32+
void _initController() {
33+
var params = const lib.PlatformWebViewControllerCreationParams();
34+
if (lib.WebViewPlatform.instance is lib.WebKitWebViewPlatform) {
35+
params = lib.WebKitWebViewControllerCreationParams(
36+
allowsInlineMediaPlayback: true,
37+
mediaTypesRequiringUserAction: widget.mediaPlaybackAlwaysAllow
38+
? {}
39+
: {...lib.PlaybackMediaTypes.values},
40+
);
41+
}
42+
43+
_controller = lib.WebViewController.fromPlatformCreationParams(params)
44+
..setJavaScriptMode(
45+
widget.js
46+
? lib.JavaScriptMode.unrestricted
47+
: lib.JavaScriptMode.disabled,
48+
)
49+
..setNavigationDelegate(
50+
lib.NavigationDelegate(
51+
onPageFinished: _onPageFinished,
52+
onNavigationRequest: widget.interceptNavigationRequest != null
53+
? (req) => _interceptNavigationRequest(req)
54+
: null,
55+
),
56+
)
57+
..setUserAgent(widget.userAgent)
58+
..loadRequest(Uri.parse(widget.url));
59+
60+
final platformController = _controller.platform;
61+
if (platformController is lib.AndroidWebViewController) {
62+
lib.AndroidWebViewController.enableDebugging(widget.debuggingEnabled);
63+
platformController.setMediaPlaybackRequiresUserGesture(
64+
!widget.mediaPlaybackAlwaysAllow,
5265
);
5366
}
67+
}
5468

69+
@override
70+
Widget build(BuildContext context) {
5571
return AspectRatio(
5672
aspectRatio: _aspectRatio,
57-
child: webView,
73+
child: _buildWebView(),
5874
);
5975
}
6076

@@ -63,7 +79,7 @@ class WebViewState extends State<WebView> {
6379
super.deactivate();
6480

6581
if (widget.unsupportedWorkaroundForIssue37) {
66-
_wvc?.reload();
82+
_controller.reload();
6783
}
6884
}
6985

@@ -74,18 +90,21 @@ class WebViewState extends State<WebView> {
7490
}
7591

7692
if (_issue37 != null) {
77-
_widgetsBindingInstance?.removeObserver(_issue37!);
93+
WidgetsBinding.instance.removeObserver(_issue37!);
7894
}
7995

8096
super.dispose();
8197
}
8298

8399
Future<String> eval(String js) async {
84100
try {
85-
return await _wvc!.runJavascriptReturningResult(js);
86-
} catch (_) {
87-
return '';
101+
final result = await _controller.runJavaScriptReturningResult(js);
102+
return '$result';
103+
} catch (evalError) {
104+
debugPrint('evalError: $evalError');
88105
}
106+
107+
return '';
89108
}
90109

91110
Future<void> _autoResize() async {
@@ -111,36 +130,22 @@ class WebViewState extends State<WebView> {
111130
}
112131
}
113132

114-
Widget _buildWebView() => lib.WebView(
115-
debuggingEnabled: widget.debuggingEnabled,
116-
initialUrl: widget.url,
117-
initialMediaPlaybackPolicy: widget.mediaPlaybackAlwaysAllow
118-
? lib.AutoMediaPlaybackPolicy.always_allow
119-
: lib.AutoMediaPlaybackPolicy
120-
.require_user_action_for_all_media_types,
121-
javascriptMode: widget.js
122-
? lib.JavascriptMode.unrestricted
123-
: lib.JavascriptMode.disabled,
133+
Widget _buildWebView() => lib.WebViewWidget(
134+
controller: _controller,
124135
key: Key(widget.url),
125-
navigationDelegate: widget.interceptNavigationRequest != null
126-
? (req) => _interceptNavigationRequest(req)
127-
: null,
128-
onPageFinished: _onPageFinished,
129-
onWebViewCreated: (c) => _wvc = c,
130-
userAgent: widget.userAgent,
131136
);
132137

133138
lib.NavigationDecision _interceptNavigationRequest(
134139
lib.NavigationRequest req,
135140
) {
136141
var intercepted = false;
137-
138-
if (widget.interceptNavigationRequest != null &&
142+
final callback = widget.interceptNavigationRequest;
143+
if (callback != null &&
139144
_firstFinishedUrl != null &&
140-
req.isForMainFrame &&
145+
req.isMainFrame &&
141146
req.url != widget.url &&
142147
req.url != _firstFinishedUrl) {
143-
intercepted = widget.interceptNavigationRequest!(req.url);
148+
intercepted = callback(req.url);
144149
}
145150

146151
return intercepted
@@ -165,9 +170,6 @@ class WebViewState extends State<WebView> {
165170
}
166171
}
167172

168-
// TODO: remove workaround when our minimum Flutter version >2.12
169-
WidgetsBinding? get _widgetsBindingInstance => WidgetsBinding.instance;
170-
171173
class _Issue37 with WidgetsBindingObserver {
172174
final WebViewState wvs;
173175

@@ -176,7 +178,7 @@ class _Issue37 with WidgetsBindingObserver {
176178
@override
177179
void didChangeAppLifecycleState(AppLifecycleState state) {
178180
if (state == AppLifecycleState.paused) {
179-
wvs._wvc?.reload();
181+
wvs._controller.reload();
180182
}
181183
}
182184
}

packages/fwfh_webview/lib/src/web_view/web_view.dart

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,6 @@ class WebView extends StatefulWidget {
6868
/// Default: `true`.
6969
final bool unsupportedWorkaroundForIssue37;
7070

71-
/// Controls whether or not to apply workaround for
72-
/// [crash on Android device](https://github.com/daohoangson/flutter_widget_from_html/issues/375)
73-
/// issue.
74-
///
75-
/// If your app targets Android 10, it's better to switch to the new webview
76-
/// implemention: [hybrid composition](https://pub.dev/packages/webview_flutter#using-hybrid-composition).
77-
///
78-
/// Default: `true`.
79-
final bool unsupportedWorkaroundForIssue375;
80-
8171
/// {@template web_view.userAgent}
8272
/// The value used for the HTTP `User-Agent` request header.
8373
///
@@ -100,13 +90,11 @@ class WebView extends StatefulWidget {
10090
this.js = true,
10191
this.mediaPlaybackAlwaysAllow = false,
10292
this.unsupportedWorkaroundForIssue37 = true,
103-
this.unsupportedWorkaroundForIssue375 = true,
10493
this.userAgent,
105-
Key? key,
106-
}) :
94+
super.key,
95+
}) :
10796
// ignore: avoid_bool_literals_in_conditional_expressions
108-
autoResize = js ? (autoResize ?? js) : false,
109-
super(key: key);
97+
autoResize = js ? (autoResize ?? js) : false;
11098

11199
@override
112100
State<WebView> createState() => WebViewState();

packages/fwfh_webview/lib/src/web_view_factory.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ mixin WebViewFactory on WidgetFactory {
4848
sandbox.contains(kAttributeIframeSandboxAllowScripts));
4949
return WebView(
5050
url,
51-
aspectRatio: dimensOk ? width! / height! : 16 / 9,
51+
aspectRatio: dimensOk ? width / height : 16 / 9,
5252
autoResize: !dimensOk && js,
5353
debuggingEnabled: webViewDebuggingEnabled,
5454
interceptNavigationRequest: (newUrl) {

packages/fwfh_webview/pubspec.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ description: WidgetFactory extension to render IFRAME with the official WebView
44
homepage: https://github.com/daohoangson/flutter_widget_from_html
55

66
environment:
7-
flutter: ">=2.5.0"
8-
sdk: ">=2.14.0 <3.0.0"
7+
flutter: ">=3.0.0"
8+
sdk: ">=2.17.0 <3.0.0"
99

1010
dependencies:
1111
flutter:
1212
sdk: flutter
1313
flutter_widget_from_html_core: ">=0.8.0 <0.10.0"
14-
webview_flutter: ">=2.2.0 <4.0.0"
14+
webview_flutter: ^4.0.1
15+
webview_flutter_android: ^3.0.0
16+
webview_flutter_wkwebview: ^3.0.0
1517

1618
dependency_overrides:
1719
flutter_widget_from_html_core:
@@ -24,3 +26,5 @@ dev_dependencies:
2426
sdk: flutter
2527
lint: any
2628
measurer: ^2.1.1
29+
plugin_platform_interface: any
30+
webview_flutter_platform_interface: ^2.0.0

packages/fwfh_webview/test/_.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ String? webViewExplainer(helper.Explainer parent, Widget widget) {
1818
? ',unsupportedWorkaroundForIssue37='
1919
'${widget.unsupportedWorkaroundForIssue37}'
2020
: '';
21-
final unsupportedWorkaroundForIssue375 =
22-
!widget.unsupportedWorkaroundForIssue375
23-
? ',unsupportedWorkaroundForIssue375='
24-
'${widget.unsupportedWorkaroundForIssue375}'
25-
: '';
2621
final userAgent = widget.userAgent?.isNotEmpty == true
2722
? ',userAgent=${widget.userAgent}'
2823
: '';
@@ -34,7 +29,6 @@ String? webViewExplainer(helper.Explainer parent, Widget widget) {
3429
"${!widget.js ? ',js=${widget.js}' : ''}"
3530
'$mediaPlaybackAlwaysAllow'
3631
'$unsupportedWorkaroundForIssue37'
37-
'$unsupportedWorkaroundForIssue375'
3832
'$userAgent'
3933
']';
4034
}

0 commit comments

Comments
 (0)