Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

Fix issuses with Android plugin after Flutter update to 1.12 #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
@@ -1,21 +1,57 @@
package com.fuyumi.flutterstatusbarcolor.flutterstatusbarcolor

import android.os.Build
import android.app.Activity
import android.view.View
import android.animation.ValueAnimator
import io.flutter.embedding.engine.plugins.FlutterPlugin
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.Registrar
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import androidx.annotation.NonNull

class FlutterStatusbarcolorPlugin: MethodCallHandler, FlutterPlugin, ActivityAware {

private var activity: android.app.Activity? = null
lateinit var myplugin: FlutterStatusbarcolorPlugin

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
myplugin = FlutterStatusbarcolorPlugin()
val channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "plugins.fuyumi.com/statusbar")
channel.setMethodCallHandler(myplugin)
}

override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
}

override fun onAttachedToActivity(activityPluginBinding: ActivityPluginBinding) {
myplugin.activity = activityPluginBinding.getActivity()
}

override fun onDetachedFromActivityForConfigChanges() {
}

override fun onReattachedToActivityForConfigChanges(activityPluginBinding: ActivityPluginBinding) {
}

override fun onDetachedFromActivity() {
}

class FlutterStatusbarcolorPlugin private constructor(private val activity: Activity?) : MethodCallHandler {
companion object {
@JvmStatic
fun registerWith(registrar: Registrar): Unit {
val channel = MethodChannel(registrar.messenger(), "plugins.fuyumi.com/statusbar")
channel.setMethodCallHandler(FlutterStatusbarcolorPlugin(registrar.activity()))
val plugin = FlutterStatusbarcolorPlugin()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
plugin.activity = registrar.activity()
}

channel.setMethodCallHandler(FlutterStatusbarcolorPlugin())
}
}

Expand All @@ -26,7 +62,7 @@ class FlutterStatusbarcolorPlugin private constructor(private val activity: Acti
"getstatusbarcolor" -> {
var statusBarColor: Int = 0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
statusBarColor = activity.window.statusBarColor
statusBarColor = activity!!.window.statusBarColor
}
result.success(statusBarColor)
}
Expand All @@ -35,12 +71,12 @@ class FlutterStatusbarcolorPlugin private constructor(private val activity: Acti
val animate: Boolean = call.argument("animate")!!
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (animate) {
val colorAnim = ValueAnimator.ofArgb(activity.window.statusBarColor, statusBarColor)
colorAnim.addUpdateListener { anim -> activity.window.statusBarColor = anim.animatedValue as Int }
val colorAnim = ValueAnimator.ofArgb(activity!!.window.statusBarColor, statusBarColor)
colorAnim.addUpdateListener { anim -> activity!!.window.statusBarColor = anim.animatedValue as Int }
colorAnim.setDuration(300)
colorAnim.start()
} else {
activity.window.statusBarColor = statusBarColor
activity!!.window.statusBarColor = statusBarColor
}
}
result.success(null)
Expand All @@ -49,17 +85,17 @@ class FlutterStatusbarcolorPlugin private constructor(private val activity: Acti
val usewhiteforeground: Boolean = call.argument("whiteForeground")!!
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (usewhiteforeground) {
activity.window.decorView.systemUiVisibility = activity.window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
activity!!.window.decorView.systemUiVisibility = activity!!.window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
} else {
activity.window.decorView.systemUiVisibility = activity.window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
activity!!.window.decorView.systemUiVisibility = activity!!.window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}
}
result.success(null)
}
"getnavigationbarcolor" -> {
var navigationBarColor: Int = 0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
navigationBarColor = activity.window.navigationBarColor
navigationBarColor = activity!!.window.navigationBarColor
}
result.success(navigationBarColor)
}
Expand All @@ -68,12 +104,12 @@ class FlutterStatusbarcolorPlugin private constructor(private val activity: Acti
val animate: Boolean = call.argument("animate")!!
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (animate) {
val colorAnim = ValueAnimator.ofArgb(activity.window.navigationBarColor, navigationBarColor)
colorAnim.addUpdateListener { anim -> activity.window.navigationBarColor = anim.animatedValue as Int }
val colorAnim = ValueAnimator.ofArgb(activity!!.window.navigationBarColor, navigationBarColor)
colorAnim.addUpdateListener { anim -> activity!!.window.navigationBarColor = anim.animatedValue as Int }
colorAnim.setDuration(300)
colorAnim.start()
} else {
activity.window.navigationBarColor = navigationBarColor
activity!!.window.navigationBarColor = navigationBarColor
}
}
result.success(null)
Expand All @@ -82,14 +118,14 @@ class FlutterStatusbarcolorPlugin private constructor(private val activity: Acti
val usewhiteforeground: Boolean = call.argument("whiteForeground")!!
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (usewhiteforeground) {
activity.window.decorView.systemUiVisibility = activity.window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR.inv()
activity!!.window.decorView.systemUiVisibility = activity!!.window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR.inv()
} else {
activity.window.decorView.systemUiVisibility = activity.window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
activity!!.window.decorView.systemUiVisibility = activity!!.window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
}
}
result.success(null)
}
else -> result.notImplemented()
}
}
}
}