Skip to content
Open
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
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.DS_Store
.dart_tool/
example/.dart_tool/

.packages
.pub/

build/
example/ios/Podfile.lock
example/ios/Pods/
flutter/tools/local.json
flutter/ios_crash/raw_crash

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# Visual Studio Code related
.vscode/settings.json
.settings/
.project
example/android/.gradle/4.10.2/vcsMetadata-1/
35 changes: 4 additions & 31 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,4 @@
## [0.5.6] - 13-05-2019

* added null check in Android layout callback
* changed behavior on dispose
* catching exceptions if callbacks are not unsubscribed properly

## [0.5.5] - 11-05-2019

* Changed README.md and formatted Dart code

## [0.5.4] - 11-05-2019

* Fixed plugin registration bug

## [0.5.3] - 09-05-2019

* Fixed exception call bug on dispose
* Change behavior of plugin registration

## [0.5.2] - 12-03-2019

* Fixed possible bug on dispose
* On iOS the keyboard pop up message is already being sent when keyboard starts popping up

## [0.5.1] - 06-01-2019

* Fixed bug when using multiple listeners on same page

## [0.5.0] - 06-12-2018

* Initial release, working on Android and iOS
## [0.0.1] - 适配混合栈
clone from https://github.com/adee42/flutter_keyboard_visibility
在已有的基础上适配了混合栈
监听了Android ios的键盘弹出
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
# keyboard_visibility
# keyboard_visibility_hybrid

clone from https://github.com/adee42/flutter_keyboard_visibility
在原有库的基础上适配了Android混合栈,给原先的库提了pr,但貌似已经没人维护了,自己发个
Notification service for soft-keyboard visibility

# Usage

Add the dependency to your pubspec.yaml file in the root folder of your project.
Look for the 'dependencies:'-line and add the following line after this line:
```
keyboard_visibility: any
```
or
```
keyboard_visibility: ^[CURRENT VERSION NUMBER]
keyboard_visibility_hybrid: ^0.0.1
```

(Please note that the two spaces in the beginning of the line are important)
Expand Down
8 changes: 8 additions & 0 deletions android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rootProject.allprojects {
apply plugin: 'com.android.library'

android {
compileSdkVersion 27
compileSdkVersion 28

defaultConfig {
minSdkVersion 16
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
package com.github.adee42.keyboardvisibility;

import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.EventChannel.EventSink;
import io.flutter.plugin.common.EventChannel.StreamHandler;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import io.flutter.embedding.android.FlutterView;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.EventChannel.EventSink;
import io.flutter.plugin.common.EventChannel.StreamHandler;
import io.flutter.plugin.common.PluginRegistry.Registrar;


public class KeyboardVisibilityPlugin implements StreamHandler, Application.ActivityLifecycleCallbacks, ViewTreeObserver.OnGlobalLayoutListener {
private static final String STREAM_CHANNEL_NAME = "github.com/adee42/flutter_keyboard_visibility";
Expand All @@ -29,56 +24,89 @@ public class KeyboardVisibilityPlugin implements StreamHandler, Application.Acti


KeyboardVisibilityPlugin(Registrar registrar) {
this.registrar = registrar;
this.registrar = registrar;
eventsSink = null;
}

public static void registerWith(Registrar registrar) {

final EventChannel eventChannel = new EventChannel(registrar.messenger(), STREAM_CHANNEL_NAME);
KeyboardVisibilityPlugin instance = new KeyboardVisibilityPlugin(registrar);
eventChannel.setStreamHandler(instance);
Application application = registrar.activity().getApplication();
application.registerActivityLifecycleCallbacks(instance);
// 引擎是在resume的时候初始化的,所以当第一个打开的页面是flutteractivity时,onResume监听不会回调,需要手动注册,这只是临时方案
// 把插件注册方式升级到v2就可以了
try{
Activity activity = registrar.activity();
if (instance.checkIsFlutterActivity(activity)){
View mainView = ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
mainView.getViewTreeObserver().addOnGlobalLayoutListener(instance);
instance.setMainView(mainView);
}
} catch (Exception e){
e.printStackTrace();
}
}

public void setMainView(View mainView) {
this.mainView = mainView;
}

@Override
public void onGlobalLayout() {
Rect r = new Rect();

if (mainView != null) {
mainView.getWindowVisibleDisplayFrame(r);

// check if the visible part of the screen is less than 85%
// if it is then the keyboard is showing
boolean newState = ((double)r.height() / (double)mainView.getRootView().getHeight()) < 0.85;

if (newState != isVisible) {
isVisible = newState;
if (eventsSink != null) {
eventsSink.success(isVisible ? 1 : 0);
}
}
}
mainView.getWindowVisibleDisplayFrame(r);

// check if the visible part of the screen is less than 85%
// if it is then the keyboard is showing
boolean newState = ((double) r.height() / (double) mainView.getRootView().getHeight()) < 0.85;

if (newState != isVisible) {
isVisible = newState;
if (eventsSink != null) {
eventsSink.success(isVisible ? 1 : 0);
}
}
}
}

@Override
public void onActivityCreated(Activity activity, Bundle bundle) {

}

@Override
public void onActivityStarted(Activity activity) {
try {
mainView = ((ViewGroup)activity.findViewById(android.R.id.content)).getChildAt(0);
mainView.getViewTreeObserver().addOnGlobalLayoutListener(this);
}
catch (Exception e) {
// do nothing
}

}

@Override
public void onActivityResumed(Activity activity) {
if (!checkIsFlutterActivity(activity)) {
return;
}
try {
mainView = ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
mainView.getViewTreeObserver().addOnGlobalLayoutListener(this);
} catch (Exception e) {
// do nothing
}
}

@Override
public void onActivityPaused(Activity activity) {
if (!checkIsFlutterActivity(activity)) {
return;
}
unregisterListener(activity);
}

@Override
public void onActivityStopped(Activity activity) {
unregisterListener();

}

@Override
Expand All @@ -87,24 +115,41 @@ public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

@Override
public void onActivityDestroyed(Activity activity) {

unregisterListener();
}

private void unregisterListener() {
if (mainView != null) {
mainView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
private void unregisterListener(Activity activity) {
if (activity != null) {
View currentMainView = getMainView(activity);
if (currentMainView != null) {
currentMainView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
mainView = null;
}
}

public static void registerWith(Registrar registrar) {
public View getMainView(Activity activity) {
return ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
}

final EventChannel eventChannel = new EventChannel(registrar.messenger(), STREAM_CHANNEL_NAME);
KeyboardVisibilityPlugin instance = new KeyboardVisibilityPlugin(registrar);
eventChannel.setStreamHandler(instance);
public boolean checkIsFlutterActivity(Activity activity) {
return findFlutterView(getMainView(activity));
}

registrar.activity().getApplication().registerActivityLifecycleCallbacks(instance);
public boolean findFlutterView(View contentView) {
boolean hasFlutterView = false;
if (contentView instanceof ViewGroup) {
ViewGroup parentView = (ViewGroup) contentView;
for (int index = 0; index < parentView.getChildCount(); index++) {
hasFlutterView = findFlutterView(parentView.getChildAt(index));
if (hasFlutterView) {
break;
}
}
}
if (contentView instanceof View && !hasFlutterView) {
hasFlutterView = contentView instanceof FlutterView;
}
return hasFlutterView;
}

@Override
Expand Down
37 changes: 37 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
/build/

# Web related
lib/generated_plugin_registrant.dart

# Exceptions to above rules.
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
7 changes: 7 additions & 0 deletions example/android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
gradle-wrapper.jar
/.gradle
/captures/
/gradlew
/gradlew.bat
/local.properties
GeneratedPluginRegistrant.java
4 changes: 2 additions & 2 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 27
compileSdkVersion 28

lintOptions {
disable 'InvalidPackage'
Expand All @@ -35,7 +35,7 @@ android {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.github.adee42.keyboardvisibilityexample"
minSdkVersion 16
targetSdkVersion 27
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand Down
1 change: 1 addition & 0 deletions example/ios/Flutter/Debug.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"
#include "Generated.xcconfig"
10 changes: 10 additions & 0 deletions example/ios/Flutter/Generated.xcconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This is a generated file; do not edit or check into version control.
FLUTTER_ROOT=/Users/weidian215/Library/Flutter/1.17.0+wd002/flutter
FLUTTER_APPLICATION_PATH=/Users/weidian215/flutter_keyboard_visibility/example
FLUTTER_TARGET=lib/main.dart
FLUTTER_BUILD_DIR=build
SYMROOT=${SOURCE_ROOT}/../build/ios
OTHER_LDFLAGS=$(inherited) -framework Flutter
FLUTTER_FRAMEWORK_DIR=/Users/weidian215/Library/Flutter/1.17.0+wd002/flutter/bin/cache/artifacts/engine/ios
FLUTTER_BUILD_NAME=1.0.0
FLUTTER_BUILD_NUMBER=1
1 change: 1 addition & 0 deletions example/ios/Flutter/Release.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
#include "Generated.xcconfig"
11 changes: 11 additions & 0 deletions example/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
# This is a generated file; do not edit or check into version control.
export "FLUTTER_ROOT=/Users/weidian215/Library/Flutter/1.17.0+wd002/flutter"
export "FLUTTER_APPLICATION_PATH=/Users/weidian215/flutter_keyboard_visibility/example"
export "FLUTTER_TARGET=lib/main.dart"
export "FLUTTER_BUILD_DIR=build"
export "SYMROOT=${SOURCE_ROOT}/../build/ios"
export "OTHER_LDFLAGS=$(inherited) -framework Flutter"
export "FLUTTER_FRAMEWORK_DIR=/Users/weidian215/Library/Flutter/1.17.0+wd002/flutter/bin/cache/artifacts/engine/ios"
export "FLUTTER_BUILD_NAME=1.0.0"
export "FLUTTER_BUILD_NUMBER=1"
Loading