This commit is contained in:
Noah Schairer
2023-04-28 15:21:41 -04:00
parent f54d552798
commit c2ecf63462
82 changed files with 2143 additions and 0 deletions

9
android/.gitignore vendored Normal file
View File

@@ -0,0 +1,9 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.cxx

46
android/build.gradle Normal file
View File

@@ -0,0 +1,46 @@
group 'com.nschairer.keyboard_height_plugin'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.7.10'
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.2.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 31
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
minSdkVersion 16
}
}

1
android/settings.gradle Normal file
View File

@@ -0,0 +1 @@
rootProject.name = 'keyboard_height_plugin'

View File

@@ -0,0 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nschairer.keyboard_height_plugin">
</manifest>

View File

@@ -0,0 +1,77 @@
package com.nschairer.keyboard_height_plugin
import android.graphics.Rect
import androidx.annotation.NonNull
import android.view.ViewTreeObserver
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.EventChannel
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
class KeyboardHeightPlugin : FlutterPlugin, EventChannel.StreamHandler, ActivityAware {
private val keyboardHeightEventChannelName = "keyboardHeightEventChannel"
private var eventSink: EventChannel.EventSink? = null
private var eventChannel: EventChannel? = null
private var activityPluginBinding: ActivityPluginBinding? = null
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
eventChannel = EventChannel(flutterPluginBinding.binaryMessenger, keyboardHeightEventChannelName)
eventChannel?.setStreamHandler(this)
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
eventChannel?.setStreamHandler(null)
}
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
eventSink = events
val rootView = activityPluginBinding?.activity?.window?.decorView?.rootView
rootView?.viewTreeObserver?.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val r = Rect()
rootView.getWindowVisibleDisplayFrame(r)
val screenHeight = rootView.height
val keypadHeight = screenHeight - r.bottom - getNavigationBarHeight()
val displayMetrics = activityPluginBinding?.activity?.resources?.displayMetrics
val logicalKeypadHeight = keypadHeight / (displayMetrics?.density ?: 1f)
if (keypadHeight > screenHeight * 0.15) {
events?.success(logicalKeypadHeight.toDouble())
} else {
events?.success(0.0)
}
}
})
}
override fun onCancel(arguments: Any?) {
eventSink = null
}
private fun getNavigationBarHeight(): Int {
val resourceId = activityPluginBinding?.activity?.resources?.getIdentifier("navigation_bar_height", "dimen", "android")
return if (resourceId != null && resourceId > 0) {
activityPluginBinding?.activity?.resources?.getDimensionPixelSize(resourceId) ?: 0
} else {
0
}
}
// Implement ActivityAware methods
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activityPluginBinding = binding
}
override fun onDetachedFromActivityForConfigChanges() {
activityPluginBinding = null
}
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
activityPluginBinding = binding
}
override fun onDetachedFromActivity() {
activityPluginBinding = null
}
}