Skip to content

Commit c0a5352

Browse files
url change for platform interface (#3323)
1 parent b6668e4 commit c0a5352

File tree

9 files changed

+81
-59
lines changed

9 files changed

+81
-59
lines changed

packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 2.1.0
22

3+
* Adds support to track url changes. See `PlatformNavigationDelegate.setOnUrlChange`.
34
* Aligns Dart and Flutter SDK constraints.
45

56
## 2.0.2

packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_navigation_delegate.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ typedef ProgressCallback = void Function(int progress);
2222
/// Signature for callbacks that report a resource loading error.
2323
typedef WebResourceErrorCallback = void Function(WebResourceError error);
2424

25+
/// Signature for callbacks that notify the host application of a change to the
26+
/// url of the web view.
27+
typedef UrlChangeCallback = void Function(UrlChange change);
28+
2529
/// An interface defining navigation events that occur on the native platform.
2630
///
2731
/// The [PlatformWebViewController] is notifying this delegate on events that
@@ -105,4 +109,13 @@ abstract class PlatformNavigationDelegate extends PlatformInterface {
105109
throw UnimplementedError(
106110
'setOnWebResourceError is not implemented on the current platform.');
107111
}
112+
113+
/// Invoked when the underlying web view changes to a new url.
114+
///
115+
/// See [PlatformWebViewController.setPlatformNavigationDelegate].
116+
Future<void> setOnUrlChange(UrlChangeCallback onUrlChange) {
117+
throw UnimplementedError(
118+
'setOnUrlChange is not implemented on the current platform.',
119+
);
120+
}
108121
}

packages/webview_flutter/webview_flutter_platform_interface/lib/src/types/types.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ export 'platform_navigation_delegate_creation_params.dart';
1111
export 'platform_webview_controller_creation_params.dart';
1212
export 'platform_webview_cookie_manager_creation_params.dart';
1313
export 'platform_webview_widget_creation_params.dart';
14+
export 'url_change.dart';
1415
export 'web_resource_error.dart';
1516
export 'webview_cookie.dart';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/cupertino.dart';
6+
7+
/// Details of the change to a web view's url.
8+
///
9+
/// Platform specific implementations can add additional fields by extending
10+
/// this class.
11+
///
12+
/// This example demonstrates how to extend the [UrlChange] to provide
13+
/// additional platform specific parameters:
14+
///
15+
/// ```dart
16+
/// class AndroidUrlChange extends UrlChange {
17+
/// const AndroidUrlChange({required super.url, required this.isReload});
18+
///
19+
/// final bool isReload;
20+
/// }
21+
/// ```
22+
@immutable
23+
class UrlChange {
24+
/// Creates a new [UrlChange].
25+
const UrlChange({required this.url});
26+
27+
/// The new url of the web view.
28+
final String? url;
29+
}

packages/webview_flutter/webview_flutter_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/packages/tree/main/packages/webview_flutt
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 2.0.2
7+
version: 2.1.0
88

99
environment:
1010
sdk: ">=2.17.0 <3.0.0"

packages/webview_flutter/webview_flutter_platform_interface/test/platform_navigation_delegate_test.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ void main() {
5151
});
5252

5353
test(
54-
// ignore: lines_longer_than_80_chars
5554
'Default implementation of setOnNavigationRequest should throw unimplemented error',
5655
() {
5756
final PlatformNavigationDelegate callbackDelegate =
@@ -66,7 +65,6 @@ void main() {
6665
});
6766

6867
test(
69-
// ignore: lines_longer_than_80_chars
7068
'Default implementation of setOnPageStarted should throw unimplemented error',
7169
() {
7270
final PlatformNavigationDelegate callbackDelegate =
@@ -80,7 +78,6 @@ void main() {
8078
});
8179

8280
test(
83-
// ignore: lines_longer_than_80_chars
8481
'Default implementation of setOnPageFinished should throw unimplemented error',
8582
() {
8683
final PlatformNavigationDelegate callbackDelegate =
@@ -94,7 +91,6 @@ void main() {
9491
});
9592

9693
test(
97-
// ignore: lines_longer_than_80_chars
9894
'Default implementation of setOnProgress should throw unimplemented error',
9995
() {
10096
final PlatformNavigationDelegate callbackDelegate =
@@ -108,7 +104,6 @@ void main() {
108104
});
109105

110106
test(
111-
// ignore: lines_longer_than_80_chars
112107
'Default implementation of setOnWebResourceError should throw unimplemented error',
113108
() {
114109
final PlatformNavigationDelegate callbackDelegate =
@@ -120,6 +115,19 @@ void main() {
120115
throwsUnimplementedError,
121116
);
122117
});
118+
119+
test(
120+
'Default implementation of setOnUrlChange should throw unimplemented error',
121+
() {
122+
final PlatformNavigationDelegate callbackDelegate =
123+
ExtendsPlatformNavigationDelegate(
124+
const PlatformNavigationDelegateCreationParams());
125+
126+
expect(
127+
() => callbackDelegate.setOnUrlChange((UrlChange change) {}),
128+
throwsUnimplementedError,
129+
);
130+
});
123131
}
124132

125133
class MockWebViewPlatformWithMixin extends MockWebViewPlatform

packages/webview_flutter/webview_flutter_platform_interface/test/platform_webview_controller_test.dart

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ void main() {
5757
isNotNull);
5858
});
5959

60-
test(
61-
// ignore: lines_longer_than_80_chars
62-
'Default implementation of loadFile should throw unimplemented error',
60+
test('Default implementation of loadFile should throw unimplemented error',
6361
() {
6462
final PlatformWebViewController controller =
6563
ExtendsPlatformWebViewController(
@@ -72,7 +70,6 @@ void main() {
7270
});
7371

7472
test(
75-
// ignore: lines_longer_than_80_chars
7673
'Default implementation of loadFlutterAsset should throw unimplemented error',
7774
() {
7875
final PlatformWebViewController controller =
@@ -86,7 +83,6 @@ void main() {
8683
});
8784

8885
test(
89-
// ignore: lines_longer_than_80_chars
9086
'Default implementation of loadHtmlString should throw unimplemented error',
9187
() {
9288
final PlatformWebViewController controller =
@@ -99,9 +95,7 @@ void main() {
9995
);
10096
});
10197

102-
test(
103-
// ignore: lines_longer_than_80_chars
104-
'Default implementation of loadRequest should throw unimplemented error',
98+
test('Default implementation of loadRequest should throw unimplemented error',
10599
() {
106100
final PlatformWebViewController controller =
107101
ExtendsPlatformWebViewController(
@@ -113,9 +107,7 @@ void main() {
113107
);
114108
});
115109

116-
test(
117-
// ignore: lines_longer_than_80_chars
118-
'Default implementation of currentUrl should throw unimplemented error',
110+
test('Default implementation of currentUrl should throw unimplemented error',
119111
() {
120112
final PlatformWebViewController controller =
121113
ExtendsPlatformWebViewController(
@@ -127,9 +119,7 @@ void main() {
127119
);
128120
});
129121

130-
test(
131-
// ignore: lines_longer_than_80_chars
132-
'Default implementation of canGoBack should throw unimplemented error',
122+
test('Default implementation of canGoBack should throw unimplemented error',
133123
() {
134124
final PlatformWebViewController controller =
135125
ExtendsPlatformWebViewController(
@@ -142,7 +132,6 @@ void main() {
142132
});
143133

144134
test(
145-
// ignore: lines_longer_than_80_chars
146135
'Default implementation of canGoForward should throw unimplemented error',
147136
() {
148137
final PlatformWebViewController controller =
@@ -155,9 +144,7 @@ void main() {
155144
);
156145
});
157146

158-
test(
159-
// ignore: lines_longer_than_80_chars
160-
'Default implementation of goBack should throw unimplemented error', () {
147+
test('Default implementation of goBack should throw unimplemented error', () {
161148
final PlatformWebViewController controller =
162149
ExtendsPlatformWebViewController(
163150
const PlatformWebViewControllerCreationParams());
@@ -168,9 +155,7 @@ void main() {
168155
);
169156
});
170157

171-
test(
172-
// ignore: lines_longer_than_80_chars
173-
'Default implementation of goForward should throw unimplemented error',
158+
test('Default implementation of goForward should throw unimplemented error',
174159
() {
175160
final PlatformWebViewController controller =
176161
ExtendsPlatformWebViewController(
@@ -182,9 +167,7 @@ void main() {
182167
);
183168
});
184169

185-
test(
186-
// ignore: lines_longer_than_80_chars
187-
'Default implementation of reload should throw unimplemented error', () {
170+
test('Default implementation of reload should throw unimplemented error', () {
188171
final PlatformWebViewController controller =
189172
ExtendsPlatformWebViewController(
190173
const PlatformWebViewControllerCreationParams());
@@ -195,9 +178,7 @@ void main() {
195178
);
196179
});
197180

198-
test(
199-
// ignore: lines_longer_than_80_chars
200-
'Default implementation of clearCache should throw unimplemented error',
181+
test('Default implementation of clearCache should throw unimplemented error',
201182
() {
202183
final PlatformWebViewController controller =
203184
ExtendsPlatformWebViewController(
@@ -210,7 +191,6 @@ void main() {
210191
});
211192

212193
test(
213-
// ignore: lines_longer_than_80_chars
214194
'Default implementation of clearLocalStorage should throw unimplemented error',
215195
() {
216196
final PlatformWebViewController controller =
@@ -239,7 +219,6 @@ void main() {
239219
);
240220

241221
test(
242-
// ignore: lines_longer_than_80_chars
243222
'Default implementation of runJavaScript should throw unimplemented error',
244223
() {
245224
final PlatformWebViewController controller =
@@ -253,7 +232,6 @@ void main() {
253232
});
254233

255234
test(
256-
// ignore: lines_longer_than_80_chars
257235
'Default implementation of runJavaScriptReturningResult should throw unimplemented error',
258236
() {
259237
final PlatformWebViewController controller =
@@ -267,7 +245,6 @@ void main() {
267245
});
268246

269247
test(
270-
// ignore: lines_longer_than_80_chars
271248
'Default implementation of addJavaScriptChannel should throw unimplemented error',
272249
() {
273250
final PlatformWebViewController controller =
@@ -286,7 +263,6 @@ void main() {
286263
});
287264

288265
test(
289-
// ignore: lines_longer_than_80_chars
290266
'Default implementation of removeJavaScriptChannel should throw unimplemented error',
291267
() {
292268
final PlatformWebViewController controller =
@@ -299,9 +275,7 @@ void main() {
299275
);
300276
});
301277

302-
test(
303-
// ignore: lines_longer_than_80_chars
304-
'Default implementation of getTitle should throw unimplemented error',
278+
test('Default implementation of getTitle should throw unimplemented error',
305279
() {
306280
final PlatformWebViewController controller =
307281
ExtendsPlatformWebViewController(
@@ -313,9 +287,7 @@ void main() {
313287
);
314288
});
315289

316-
test(
317-
// ignore: lines_longer_than_80_chars
318-
'Default implementation of scrollTo should throw unimplemented error',
290+
test('Default implementation of scrollTo should throw unimplemented error',
319291
() {
320292
final PlatformWebViewController controller =
321293
ExtendsPlatformWebViewController(
@@ -327,9 +299,7 @@ void main() {
327299
);
328300
});
329301

330-
test(
331-
// ignore: lines_longer_than_80_chars
332-
'Default implementation of scrollBy should throw unimplemented error',
302+
test('Default implementation of scrollBy should throw unimplemented error',
333303
() {
334304
final PlatformWebViewController controller =
335305
ExtendsPlatformWebViewController(
@@ -342,7 +312,6 @@ void main() {
342312
});
343313

344314
test(
345-
// ignore: lines_longer_than_80_chars
346315
'Default implementation of getScrollPosition should throw unimplemented error',
347316
() {
348317
final PlatformWebViewController controller =
@@ -355,9 +324,7 @@ void main() {
355324
);
356325
});
357326

358-
test(
359-
// ignore: lines_longer_than_80_chars
360-
'Default implementation of enableZoom should throw unimplemented error',
327+
test('Default implementation of enableZoom should throw unimplemented error',
361328
() {
362329
final PlatformWebViewController controller =
363330
ExtendsPlatformWebViewController(
@@ -370,7 +337,6 @@ void main() {
370337
});
371338

372339
test(
373-
// ignore: lines_longer_than_80_chars
374340
'Default implementation of setBackgroundColor should throw unimplemented error',
375341
() {
376342
final PlatformWebViewController controller =
@@ -384,7 +350,6 @@ void main() {
384350
});
385351

386352
test(
387-
// ignore: lines_longer_than_80_chars
388353
'Default implementation of setJavaScriptMode should throw unimplemented error',
389354
() {
390355
final PlatformWebViewController controller =
@@ -398,7 +363,6 @@ void main() {
398363
});
399364

400365
test(
401-
// ignore: lines_longer_than_80_chars
402366
'Default implementation of setUserAgent should throw unimplemented error',
403367
() {
404368
final PlatformWebViewController controller =

packages/webview_flutter/webview_flutter_platform_interface/test/platform_webview_controller_test.mocks.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,14 @@ class MockPlatformNavigationDelegate extends _i1.Mock
103103
returnValue: _i4.Future<void>.value(),
104104
returnValueForMissingStub: _i4.Future<void>.value(),
105105
) as _i4.Future<void>);
106+
@override
107+
_i4.Future<void> setOnUrlChange(_i3.UrlChangeCallback? onUrlChange) =>
108+
(super.noSuchMethod(
109+
Invocation.method(
110+
#setOnUrlChange,
111+
[onUrlChange],
112+
),
113+
returnValue: _i4.Future<void>.value(),
114+
returnValueForMissingStub: _i4.Future<void>.value(),
115+
) as _i4.Future<void>);
106116
}

0 commit comments

Comments
 (0)