Commit f92a6be8 by MAHMOUD MOURAD Committed by GitHub

bug fix - now toast message appears with decoration even if the android version…

bug fix - now toast message appears with decoration even if the android version of the device is bigger than 30 (#492)

Co-authored-by: Karthik Ponnam <ponnamkarthik3@gmail.com>
parent 7399508d
......@@ -9,7 +9,8 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:7.4.0'
classpath 'com.android.tools.build:gradle:7.1.3'
//noinspection GradleDependency
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
......@@ -25,31 +26,32 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 31
compileSdkVersion 33
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = '1.8'
jvmTarget = '11'
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
// from flutter sdk 3.2 minSdkVersion
minSdkVersion 19
targetSdkVersion 33
if (project.android.hasProperty('namespace')) {
namespace 'io.github.ponnamkarthik.toast.fluttertoast'
}
}
lintOptions {
disable 'InvalidPackage'
}
}
dependencies {
......
......@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip
......@@ -13,22 +13,22 @@ public class FlutterToastPlugin: FlutterPlugin {
private var channel: MethodChannel? = null
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
setupChannel(binding.binaryMessenger, binding.applicationContext)
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding,) {
setupChannel(binding.binaryMessenger, binding.applicationContext,)
}
override fun onDetachedFromEngine(p0: FlutterPlugin.FlutterPluginBinding) {
override fun onDetachedFromEngine(p0: FlutterPlugin.FlutterPluginBinding,) {
teardownChannel();
}
private fun setupChannel(messenger: BinaryMessenger, context: Context) {
channel = MethodChannel(messenger, "PonnamKarthik/fluttertoast")
val handler = MethodCallHandlerImpl(context)
channel?.setMethodCallHandler(handler)
private fun setupChannel(messenger: BinaryMessenger, context: Context,) {
channel = MethodChannel(messenger, "PonnamKarthik/fluttertoast",)
val handler = MethodCallHandlerImpl(context,)
channel?.setMethodCallHandler(handler,)
}
private fun teardownChannel() {
channel?.setMethodCallHandler(null)
channel?.setMethodCallHandler(null,)
channel = null
}
......
......@@ -19,15 +19,15 @@ internal class MethodCallHandlerImpl(private var context: Context) : MethodCallH
private var mToast: Toast? = null
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result,) {
when (call.method) {
"showToast" -> {
val mMessage = call.argument<Any>("msg").toString()
val length = call.argument<Any>("length").toString()
val gravity = call.argument<Any>("gravity").toString()
val bgcolor = call.argument<Number>("bgcolor")
val textcolor = call.argument<Number>("textcolor")
val textSize = call.argument<Number>("fontSize")
val mMessage = call.argument<Any>("msg",).toString()
val length = call.argument<Any>("length",).toString()
val gravity = call.argument<Any>("gravity",).toString()
val bgcolor = call.argument<Number>("bgcolor",)
val textcolor = call.argument<Number>("textcolor",)
val textSize = call.argument<Number>("fontSize",)
val mGravity: Int = when (gravity) {
"top" -> Gravity.TOP
......@@ -41,81 +41,81 @@ internal class MethodCallHandlerImpl(private var context: Context) : MethodCallH
Toast.LENGTH_SHORT
}
if (bgcolor != null && Build.VERSION.SDK_INT <= 31) {
val layout = (context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater).inflate(R.layout.toast_custom, null)
val text = layout.findViewById<TextView>(R.id.text)
if (bgcolor != null) {
val layout = (context.getSystemService(Context.LAYOUT_INFLATER_SERVICE,) as LayoutInflater).inflate(R.layout.toast_custom, null,)
val text = layout.findViewById<TextView>(R.id.text,)
text.text = mMessage
val gradientDrawable: Drawable? = if (Build.VERSION.SDK_INT >= 21) {
val gradientDrawable: Drawable? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
context.getDrawable(R.drawable.corner)!!
} else {
// context.resources.getDrawable(R.drawable.corner)
ContextCompat.getDrawable(context, R.drawable.corner)
}
gradientDrawable!!.setColorFilter(bgcolor.toInt(), PorterDuff.Mode.SRC_IN)
text.background = gradientDrawable
if (textSize != null) {
text.textSize = textSize.toFloat()
}
if (textcolor != null) {
text.setTextColor(textcolor.toInt())
}
mToast = Toast(context)
mToast = Toast(context,)
mToast?.duration = mDuration
mToast?.view = layout
} else {
mToast = Toast.makeText(context, mMessage, mDuration)
if (Build.VERSION.SDK_INT <= 31) {
try {
val textView: TextView = mToast?.view!!.findViewById(android.R.id.message)
mToast = Toast.makeText(context, mMessage, mDuration,)
val textView: TextView = mToast?.view!!.findViewById(android.R.id.message,)
if (textSize != null) {
textView.textSize = textSize.toFloat()
}
if (textcolor != null) {
textView.setTextColor(textcolor.toInt())
}
} catch (_: Exception) {
}
} catch (e: Exception,) { }
}
}
if(Build.VERSION.SDK_INT <= 31) {
try {
when (mGravity) {
Gravity.CENTER -> {
mToast?.setGravity(mGravity, 0, 0)
mToast?.setGravity(mGravity, 0, 0,)
}
Gravity.TOP -> {
mToast?.setGravity(mGravity, 0, 100)
mToast?.setGravity(mGravity, 0, 100,)
}
else -> {
mToast?.setGravity(mGravity, 0, 100)
}
mToast?.setGravity(mGravity, 0, 100,)
}
}
} catch (e: Exception,) { }
if (context is Activity) {
(context as Activity).runOnUiThread { mToast?.show() }
} else {
mToast?.show()
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
mToast?.addCallback(object : Toast.Callback() {
mToast?.addCallback(
object : Toast.Callback() {
override fun onToastHidden() {
super.onToastHidden()
mToast = null
}
})
},
)
}
result.success(true)
result.success(true,)
}
"cancel" -> {
if (mToast != null) {
mToast?.cancel()
mToast = null
}
result.success(true)
result.success(true,)
}
else -> result.notImplemented()
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment