Skip to content

Commit

Permalink
Merge branch 'disable_enable_input' of https://github.com/Mecharyry/f…
Browse files Browse the repository at this point in the history
…lutter_inappwebview into Mecharyry-disable_enable_input
  • Loading branch information
pichillilorenzo committed Nov 11, 2024
2 parents abf9572 + 263e33a commit b9af92d
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 10 deletions.
23 changes: 23 additions & 0 deletions flutter_inappwebview/example/lib/in_app_webiew_example.screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:collection';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:url_launcher/url_launcher.dart';

Expand Down Expand Up @@ -30,6 +31,7 @@ class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
String url = "";
double progress = 0;
final urlController = TextEditingController();
bool inputDisabled = false;

@override
void initState() {
Expand Down Expand Up @@ -220,6 +222,27 @@ class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
webViewController?.reload();
},
),
if (defaultTargetPlatform == TargetPlatform.android ||
defaultTargetPlatform == TargetPlatform.iOS)
ElevatedButton(
child: inputDisabled
? Icon(Icons.keyboard)
: Icon(Icons.keyboard_hide),
onPressed: () {
if (inputDisabled) {
webViewController
?.enableInputMethod()
.then((_) => webViewController?.showInputMethod());
} else {
webViewController
?.disableInputMethod()
.then((_) => webViewController?.hideInputMethod());
}
setState(() {
inputDisabled = !inputDisabled;
});
},
),
],
),
])));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'dart:core';

import 'package:flutter/services.dart';

import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart';

import '../print_job/main.dart';
import '../web_message/main.dart';
import '../web_storage/web_storage.dart';

import 'android/in_app_webview_controller.dart';
import 'apple/in_app_webview_controller.dart';

Expand Down Expand Up @@ -294,6 +296,18 @@ class InAppWebViewController {
///{@macro flutter_inappwebview_platform_interface.PlatformInAppWebViewController.clearFocus}
Future<void> clearFocus() => platform.clearFocus();

///{@macro flutter_inappwebview_platform_interface.PlatformInAppWebViewController.disableInputMethod}
Future<void> disableInputMethod() => platform.disableInputMethod();

///{@macro flutter_inappwebview_platform_interface.PlatformInAppWebViewController.enableInputMethod}
Future<void> enableInputMethod() => platform.enableInputMethod();

///{@macro flutter_inappwebview_platform_interface.PlatformInAppWebViewController.showInputMethod}
Future<void> showInputMethod() => platform.showInputMethod();

///{@macro flutter_inappwebview_platform_interface.PlatformInAppWebViewController.hideInputMethod}
Future<void> hideInputMethod() => platform.hideInputMethod();

///{@macro flutter_inappwebview_platform_interface.PlatformInAppWebViewController.setContextMenu}
Future<void> setContextMenu(ContextMenu? contextMenu) =>
platform.setContextMenu(contextMenu);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,8 @@ void loadDataWithBaseURL(String baseUrl, String data,
@Nullable
WebViewChannelDelegate getChannelDelegate();
void setChannelDelegate(@Nullable WebViewChannelDelegate eventWebViewChannelDelegate);
void disableInputMethod();
void enableInputMethod();
void hideInputMethod();
void showInputMethod();
}
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,31 @@ public void onReceiveValue(Boolean value) {
webView.clearFormData();
}
result.success(true);
case disableInputMethod:
if (webView != null) {
webView.disableInputMethod();
}
result.success(true);
break;
case enableInputMethod:
if (webView != null) {
webView.enableInputMethod();
}
result.success(true);
break;
case hideInputMethod:
if (webView != null) {
webView.hideInputMethod();
}
result.success(true);
break;
case showInputMethod:
if (webView != null) {
webView.showInputMethod();
}
result.success(true);
break;

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,9 @@ public enum WebViewChannelDelegateMethods {
canScrollVertically,
canScrollHorizontally,
isInFullscreen,
clearFormData
clearFormData,
disableInputMethod,
enableInputMethod,
hideInputMethod,
showInputMethod
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import android.animation.PropertyValuesHolder;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.PackageInfo;
import android.graphics.Bitmap;
import android.graphics.Canvas;
Expand Down Expand Up @@ -37,6 +39,7 @@
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;
Expand Down Expand Up @@ -2066,6 +2069,40 @@ public InAppWebViewSettings getCustomSettings() {
return customSettings;
}

public void enableInputMethod() {
Activity activity = getActivity(getContext());
if(activity != null) {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}
}

public void showInputMethod() {
hideInputMethod();
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(INPUT_METHOD_SERVICE);
imm.showSoftInput(this, 0);
}

public void disableInputMethod() {
Activity activity = getActivity(getContext());
if(activity != null) {
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}
}

public void hideInputMethod() {
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(this.getWindowToken(), 0);
}

public static Activity getActivity(Context context) {
if (context == null) return null;
if (context instanceof Activity) return (Activity) context;
if (context instanceof ContextWrapper) return getActivity(((ContextWrapper)context).getBaseContext());
return null;
}

@Override
public void dispose() {
if (channelDelegate != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart';

import '../in_app_browser/in_app_browser.dart';
import '../print_job/main.dart';
import '../web_message/main.dart';
import '../web_storage/web_storage.dart';

import '_static_channel.dart';
import 'headless_in_app_webview.dart';

Expand Down Expand Up @@ -2256,6 +2258,30 @@ class AndroidInAppWebViewController extends PlatformInAppWebViewController
return await channel?.invokeMethod('clearFocus', args);
}

@override
Future<void> disableInputMethod() async {
Map<String, dynamic> args = <String, dynamic>{};
return channel?.invokeMethod('disableInputMethod', args);
}

@override
Future<void> enableInputMethod() async {
Map<String, dynamic> args = <String, dynamic>{};
return await channel?.invokeMethod('enableInputMethod', args);
}

@override
Future<void> showInputMethod() async {
Map<String, dynamic> args = <String, dynamic>{};
return await channel?.invokeMethod('showInputMethod', args);
}

@override
Future<void> hideInputMethod() async {
Map<String, dynamic> args = <String, dynamic>{};
return await channel?.invokeMethod('hideInputMethod', args);
}

@override
Future<void> setContextMenu(ContextMenu? contextMenu) async {
Map<String, dynamic> args = <String, dynamic>{};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3380,6 +3380,26 @@ if(window.\(JavaScriptBridgeJS.get_JAVASCRIPT_BRIDGE_NAME())[\(_callHandlerID)]
public override var inputAccessoryView: UIView? {
return settings?.disableInputAccessoryView ?? false ? nil : super.inputAccessoryView
}

var _inputView: UIView? = nil
public func disableInputMethod() {
_inputView = UIView()
for subview in self.scrollView.subviews {
subview.reloadInputViews()
}
}

public func enableInputMethod() {
_inputView = nil
for subview in self.scrollView.subviews {
subview.reloadInputViews()
}
}


public override var inputView: UIView? {
return _inputView != nil ? _inputView : super.inputView
}

public func runWindowBeforeCreatedCallbacks() {
let callbacks = windowBeforeCreatedCallbacks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,12 @@ public class WebViewChannelDelegate: ChannelDelegate {
} else {
result(false)
}
case .disableInputMethod:
webView?.disableInputMethod()
result(true)
case .enableInputMethod:
webView?.enableInputMethod()
result(true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,6 @@ public enum WebViewChannelDelegateMethods: String {
case getMicrophoneCaptureState = "getMicrophoneCaptureState"
case setMicrophoneCaptureState = "setMicrophoneCaptureState"
case loadSimulatedRequest = "loadSimulatedRequest"
case disableInputMethod = "disableInputMethod"
case enableInputMethod = "enableInputMethod"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart';

import '../in_app_browser/in_app_browser.dart';
import '../print_job/main.dart';
import '../web_message/main.dart';
import '../web_storage/web_storage.dart';

import '_static_channel.dart';
import 'headless_in_app_webview.dart';

Expand Down Expand Up @@ -2232,6 +2234,24 @@ class IOSInAppWebViewController extends PlatformInAppWebViewController
return await channel?.invokeMethod('clearFocus', args);
}

@override
Future<void> disableInputMethod() async {
Map<String, dynamic> args = <String, dynamic>{};
return await channel?.invokeMethod('disableInputMethod', args);
}

@override
Future<void> enableInputMethod() async {
Map<String, dynamic> args = <String, dynamic>{};
return await channel?.invokeMethod('enableInputMethod', args);
}

@override
Future<void> showInputMethod() async {}

@override
Future<void> hideInputMethod() async {}

@override
Future<bool?> requestFocus(
{FocusDirection? direction,
Expand Down
Loading

0 comments on commit b9af92d

Please sign in to comment.