Skip to content

Fix back button handler to be compatible with the WillPopScope widget. #408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
if (webView.canGoBack()) {
webView.goBack();
} else {
close();
FlutterWebviewPlugin.channel.invokeMethod("onBack", null);
}
return true;
}
Expand Down
7 changes: 7 additions & 0 deletions lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class FlutterWebviewPlugin {

final _channel = const MethodChannel(_kChannel);

final _onBack = StreamController<Null>.broadcast();
final _onDestroy = StreamController<Null>.broadcast();
final _onUrlChanged = StreamController<String>.broadcast();
final _onStateChanged = StreamController<WebViewStateChanged>.broadcast();
Expand All @@ -33,6 +34,9 @@ class FlutterWebviewPlugin {

Future<Null> _handleMessages(MethodCall call) async {
switch (call.method) {
case 'onBack':
_onBack.add(null);
break;
case 'onDestroy':
_onDestroy.add(null);
break;
Expand Down Expand Up @@ -64,6 +68,9 @@ class FlutterWebviewPlugin {
/// Listening the OnDestroy LifeCycle Event for Android
Stream<Null> get onDestroy => _onDestroy.stream;

/// Listening the back key press Event for Android
Stream<Null> get onBack => _onBack.stream;

/// Listening url changed
Stream<String> get onUrlChanged => _onUrlChanged.stream;

Expand Down
21 changes: 15 additions & 6 deletions lib/src/webview_scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,30 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
Timer _resizeTimer;
StreamSubscription<WebViewStateChanged> _onStateChanged;

var _onDestroy;
var _onBack;

@override
void initState() {
super.initState();
webviewReference.close();

_onDestroy = webviewReference.onDestroy.listen((_) {
if (mounted) {
Navigator.of(context).pop();
_onBack = webviewReference.onBack.listen((_) async {
if (!mounted) return;

// Equivalent of Navigator.maybePop(), except that [webviewReference]
// is closed when the pop goes ahead. Whether the pop was performed
// can't be determined from the return value of Navigator.maybePop().
final route = ModalRoute.of(context);
final pop = await route?.willPop();
if (pop == RoutePopDisposition.pop) {
webviewReference.close();
Navigator.pop(context);
}
});

if (widget.hidden) {
_onStateChanged = webviewReference.onStateChanged.listen((WebViewStateChanged state) {
_onStateChanged =
webviewReference.onStateChanged.listen((WebViewStateChanged state) {
if (state.type == WebViewState.finishLoad) {
webviewReference.show();
}
Expand All @@ -94,7 +103,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
@override
void dispose() {
super.dispose();
_onDestroy?.cancel();
_onBack?.cancel();
_resizeTimer?.cancel();
webviewReference.close();
if (widget.hidden) {
Expand Down