Skip to content
Open
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 @@ -8,6 +8,8 @@ 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
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class KeyboardHeightPlugin : FlutterPlugin, EventChannel.StreamHandler, ActivityAware {
private val keyboardHeightEventChannelName = "keyboardHeightEventChannel"
Expand All @@ -33,15 +35,20 @@ class KeyboardHeightPlugin : FlutterPlugin, EventChannel.StreamHandler, Activity
rootView.getWindowVisibleDisplayFrame(r)

val screenHeight = rootView.height
val navigationBarHeight = getNavigationBarHeight();
val insets = ViewCompat.getRootWindowInsets(rootView)
val navigationBarHeight = insets?.getInsets(WindowInsetsCompat.Type.navigationBars())?.bottom ?: 0;
val isNavigationBarVisible = insets?.isVisible(WindowInsetsCompat.Type.navigationBars()) ?: false

var keypadHeight = screenHeight - r.bottom
if (isNavigationBarVisible()) {

if (isNavigationBarVisible) {
keypadHeight -= navigationBarHeight
}

val displayMetrics = activityPluginBinding?.activity?.resources?.displayMetrics
val logicalKeypadHeight = keypadHeight / (displayMetrics?.density ?: 1f)

if (keypadHeight > screenHeight * 0.15) {
if (keypadHeight > screenHeight * 0.02) {
events?.success(logicalKeypadHeight.toDouble())
} else {
events?.success(0.0)
Expand All @@ -54,26 +61,6 @@ class KeyboardHeightPlugin : FlutterPlugin, EventChannel.StreamHandler, Activity
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
}
}

private fun isNavigationBarVisible(): Boolean {
val decorView = activityPluginBinding?.activity?.window?.decorView
val rootWindowInsets = decorView?.rootWindowInsets ?: return false
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
rootWindowInsets.isVisible(android.view.WindowInsets.Type.navigationBars())
} else {
val systemWindowInsetBottom = rootWindowInsets.systemWindowInsetBottom
systemWindowInsetBottom > 0
}
}

// Implement ActivityAware methods
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activityPluginBinding = binding
Expand Down