Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
[webview_flutter] add gesture navigation for iOS
Browse files Browse the repository at this point in the history
This introduces a boolean for the allowsBackForwardNavigationGestures
setting of the WKWebView in iOS. It has no effect on Android.
  • Loading branch information
wwwdata committed Dec 11, 2019
1 parent 4652270 commit f67b848
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/webview_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.19

* Add setting for iOS to allow gesture based navigation.

## 0.3.18+1

* Be explicit that keyboard is not ready for production in README.md.
Expand Down
1 change: 1 addition & 0 deletions packages/webview_flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class _WebViewExampleState extends State<WebViewExample> {
onPageFinished: (String url) {
print('Page finished loading: $url');
},
allowsBackForwardNavigationGestures: true,
);
}),
floatingActionButton: favoriteButton(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,32 @@ void main() {
expect(currentUrl, 'https://www.google.com/');
});
});

testWidgets('launches with allowsBackForwardNavigationGestures on iOS',
(WidgetTester tester) async {
final Completer<WebViewController> controllerCompleter =
Completer<WebViewController>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: SizedBox(
width: 400,
height: 300,
child: WebView(
key: GlobalKey(),
initialUrl: 'https://flutter.dev/',
allowsBackForwardNavigationGestures: true,
onWebViewCreated: (WebViewController controller) {
controllerCompleter.complete(controller);
},
),
),
),
);
final WebViewController controller = await controllerCompleter.future;
final String currentUrl = await controller.currentUrl();
expect(currentUrl, 'https://flutter.dev/');
});
}

// JavaScript booleans evaluate to different string values on Android and iOS.
Expand Down
4 changes: 4 additions & 0 deletions packages/webview_flutter/ios/Classes/FlutterWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ - (NSString*)applySettings:(NSDictionary<NSString*, id>*)settings {
} else if ([key isEqualToString:@"userAgent"]) {
NSString* userAgent = settings[key];
[self updateUserAgent:[userAgent isEqual:[NSNull null]] ? nil : userAgent];
} else if ([key isEqualToString:@"allowsBackForwardNavigationGestures"]) {
NSNumber* allowsBackForwardNavigationGestures = settings[key];
_webView.allowsBackForwardNavigationGestures =
[allowsBackForwardNavigationGestures boolValue];
} else {
[unknownKeys addObject:key];
}
Expand Down
8 changes: 7 additions & 1 deletion packages/webview_flutter/lib/platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ class WebSettings {
this.hasNavigationDelegate,
this.debuggingEnabled,
@required this.userAgent,
this.allowsBackForwardNavigationGestures,
}) : assert(userAgent != null);

/// The JavaScript execution mode to be used by the webview.
Expand All @@ -255,9 +256,14 @@ class WebSettings {
/// See also [WebView.userAgent].
final WebSetting<String> userAgent;

/// Whether to allow swipe based navigation in iOS.
///
/// See also: [WebView.allowsBackForwardNavigationGestures]
final bool allowsBackForwardNavigationGestures;

@override
String toString() {
return 'WebSettings(javascriptMode: $javascriptMode, hasNavigationDelegate: $hasNavigationDelegate, debuggingEnabled: $debuggingEnabled, userAgent: $userAgent,)';
return 'WebSettings(javascriptMode: $javascriptMode, hasNavigationDelegate: $hasNavigationDelegate, debuggingEnabled: $debuggingEnabled, userAgent: $userAgent, allowsBackForwardNavigationGestures: $allowsBackForwardNavigationGestures)';
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/webview_flutter/lib/src/webview_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ class MethodChannelWebViewPlatform implements WebViewPlatformController {
_addIfNonNull('jsMode', settings.javascriptMode?.index);
_addIfNonNull('hasNavigationDelegate', settings.hasNavigationDelegate);
_addIfNonNull('debuggingEnabled', settings.debuggingEnabled);
_addIfNonNull('allowsBackForwardNavigationGestures',
settings.allowsBackForwardNavigationGestures);
_addSettingIfPresent('userAgent', settings.userAgent);
return map;
}
Expand Down
10 changes: 10 additions & 0 deletions packages/webview_flutter/lib/webview_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class WebView extends StatefulWidget {
this.userAgent,
this.initialMediaPlaybackPolicy =
AutoMediaPlaybackPolicy.require_user_action_for_all_media_types,
this.allowsBackForwardNavigationGestures,
}) : assert(javascriptMode != null),
assert(initialMediaPlaybackPolicy != null),
super(key: key);
Expand Down Expand Up @@ -311,6 +312,13 @@ class WebView extends StatefulWidget {
/// The default policy is [AutoMediaPlaybackPolicy.require_user_action_for_all_media_types].
final AutoMediaPlaybackPolicy initialMediaPlaybackPolicy;

/// A Boolean value indicating whether horizontal swipe gestures will trigger back-forward list navigations.
///
/// This only works on iOS.
///
/// By default `allowsBackForwardNavigationGestures` is false.
final bool allowsBackForwardNavigationGestures;

@override
State<StatefulWidget> createState() => _WebViewState();
}
Expand Down Expand Up @@ -384,6 +392,8 @@ WebSettings _webSettingsFromWidget(WebView widget) {
hasNavigationDelegate: widget.navigationDelegate != null,
debuggingEnabled: widget.debuggingEnabled,
userAgent: WebSetting<String>.of(widget.userAgent),
allowsBackForwardNavigationGestures:
widget.allowsBackForwardNavigationGestures,
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/webview_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: webview_flutter
description: A Flutter plugin that provides a WebView widget on Android and iOS.
version: 0.3.18+1
version: 0.3.19
homepage: https://github.com/flutter/plugins/tree/master/packages/webview_flutter

environment:
Expand Down
6 changes: 5 additions & 1 deletion packages/webview_flutter/test/webview_flutter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ void main() {
await tester.pumpWidget(
const WebView(
initialUrl: 'https://youtube.com',
allowsBackForwardNavigationGestures: true,
),
);

Expand All @@ -837,6 +838,7 @@ void main() {
hasNavigationDelegate: false,
debuggingEnabled: false,
userAgent: WebSetting<String>.of(null),
allowsBackForwardNavigationGestures: true,
),
// TODO(iskakaushik): Remove this when collection literals makes it to stable.
// ignore: prefer_collection_literals
Expand Down Expand Up @@ -1193,7 +1195,9 @@ class MatchesWebSettings extends Matcher {
_webSettings.hasNavigationDelegate ==
webSettings.hasNavigationDelegate &&
_webSettings.debuggingEnabled == webSettings.debuggingEnabled &&
_webSettings.userAgent == webSettings.userAgent;
_webSettings.userAgent == webSettings.userAgent &&
_webSettings.allowsBackForwardNavigationGestures ==
webSettings.allowsBackForwardNavigationGestures;
}
}

Expand Down

0 comments on commit f67b848

Please sign in to comment.