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 { ...@@ -9,7 +9,8 @@ buildscript {
} }
dependencies { 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" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
} }
} }
...@@ -25,31 +26,32 @@ apply plugin: 'com.android.library' ...@@ -25,31 +26,32 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android' apply plugin: 'kotlin-android'
android { android {
compileSdkVersion 31 compileSdkVersion 33
compileOptions { compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8 sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_11
} }
kotlinOptions { kotlinOptions {
jvmTarget = '1.8' jvmTarget = '11'
} }
sourceSets { sourceSets {
main.java.srcDirs += 'src/main/kotlin' main.java.srcDirs += 'src/main/kotlin'
} }
defaultConfig { defaultConfig {
// from flutter sdk 3.2 minSdkVersion
minSdkVersion 19 minSdkVersion 19
targetSdkVersion 33
if (project.android.hasProperty('namespace')) { if (project.android.hasProperty('namespace')) {
namespace 'io.github.ponnamkarthik.toast.fluttertoast' namespace 'io.github.ponnamkarthik.toast.fluttertoast'
} }
} }
lintOptions { lintOptions {
disable 'InvalidPackage' disable 'InvalidPackage'
} }
} }
dependencies { dependencies {
......
...@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME ...@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists 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 { ...@@ -13,22 +13,22 @@ public class FlutterToastPlugin: FlutterPlugin {
private var channel: MethodChannel? = null private var channel: MethodChannel? = null
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding,) {
setupChannel(binding.binaryMessenger, binding.applicationContext) setupChannel(binding.binaryMessenger, binding.applicationContext,)
} }
override fun onDetachedFromEngine(p0: FlutterPlugin.FlutterPluginBinding) { override fun onDetachedFromEngine(p0: FlutterPlugin.FlutterPluginBinding,) {
teardownChannel(); teardownChannel();
} }
private fun setupChannel(messenger: BinaryMessenger, context: Context) { private fun setupChannel(messenger: BinaryMessenger, context: Context,) {
channel = MethodChannel(messenger, "PonnamKarthik/fluttertoast") channel = MethodChannel(messenger, "PonnamKarthik/fluttertoast",)
val handler = MethodCallHandlerImpl(context) val handler = MethodCallHandlerImpl(context,)
channel?.setMethodCallHandler(handler) channel?.setMethodCallHandler(handler,)
} }
private fun teardownChannel() { private fun teardownChannel() {
channel?.setMethodCallHandler(null) channel?.setMethodCallHandler(null,)
channel = null channel = null
} }
......
...@@ -19,15 +19,15 @@ internal class MethodCallHandlerImpl(private var context: Context) : MethodCallH ...@@ -19,15 +19,15 @@ internal class MethodCallHandlerImpl(private var context: Context) : MethodCallH
private var mToast: Toast? = null private var mToast: Toast? = null
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { override fun onMethodCall(call: MethodCall, result: MethodChannel.Result,) {
when (call.method) { when (call.method) {
"showToast" -> { "showToast" -> {
val mMessage = call.argument<Any>("msg").toString() val mMessage = call.argument<Any>("msg",).toString()
val length = call.argument<Any>("length").toString() val length = call.argument<Any>("length",).toString()
val gravity = call.argument<Any>("gravity").toString() val gravity = call.argument<Any>("gravity",).toString()
val bgcolor = call.argument<Number>("bgcolor") val bgcolor = call.argument<Number>("bgcolor",)
val textcolor = call.argument<Number>("textcolor") val textcolor = call.argument<Number>("textcolor",)
val textSize = call.argument<Number>("fontSize") val textSize = call.argument<Number>("fontSize",)
val mGravity: Int = when (gravity) { val mGravity: Int = when (gravity) {
"top" -> Gravity.TOP "top" -> Gravity.TOP
...@@ -41,81 +41,81 @@ internal class MethodCallHandlerImpl(private var context: Context) : MethodCallH ...@@ -41,81 +41,81 @@ internal class MethodCallHandlerImpl(private var context: Context) : MethodCallH
Toast.LENGTH_SHORT Toast.LENGTH_SHORT
} }
if (bgcolor != null && Build.VERSION.SDK_INT <= 31) { if (bgcolor != null) {
val layout = (context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater).inflate(R.layout.toast_custom, 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) val text = layout.findViewById<TextView>(R.id.text,)
text.text = mMessage 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)!! context.getDrawable(R.drawable.corner)!!
} else { } else {
// context.resources.getDrawable(R.drawable.corner) // context.resources.getDrawable(R.drawable.corner)
ContextCompat.getDrawable(context, R.drawable.corner) ContextCompat.getDrawable(context, R.drawable.corner)
} }
gradientDrawable!!.setColorFilter(bgcolor.toInt(), PorterDuff.Mode.SRC_IN) gradientDrawable!!.setColorFilter(bgcolor.toInt(), PorterDuff.Mode.SRC_IN)
text.background = gradientDrawable text.background = gradientDrawable
if (textSize != null) { if (textSize != null) {
text.textSize = textSize.toFloat() text.textSize = textSize.toFloat()
} }
if (textcolor != null) { if (textcolor != null) {
text.setTextColor(textcolor.toInt()) text.setTextColor(textcolor.toInt())
} }
mToast = Toast(context)
mToast = Toast(context,)
mToast?.duration = mDuration mToast?.duration = mDuration
mToast?.view = layout mToast?.view = layout
} else { } else {
mToast = Toast.makeText(context, mMessage, mDuration) try {
if (Build.VERSION.SDK_INT <= 31) { mToast = Toast.makeText(context, mMessage, mDuration,)
try { val textView: TextView = mToast?.view!!.findViewById(android.R.id.message,)
val textView: TextView = mToast?.view!!.findViewById(android.R.id.message) if (textSize != null) {
textView.textSize = textSize.toFloat()
if (textSize != null) {
textView.textSize = textSize.toFloat()
}
if (textcolor != null) {
textView.setTextColor(textcolor.toInt())
}
} catch (_: Exception) {
} }
} if (textcolor != null) {
textView.setTextColor(textcolor.toInt())
}
} catch (e: Exception,) { }
} }
if(Build.VERSION.SDK_INT <= 31) {
try {
when (mGravity) { when (mGravity) {
Gravity.CENTER -> { Gravity.CENTER -> {
mToast?.setGravity(mGravity, 0, 0) mToast?.setGravity(mGravity, 0, 0,)
} }
Gravity.TOP -> { Gravity.TOP -> {
mToast?.setGravity(mGravity, 0, 100) mToast?.setGravity(mGravity, 0, 100,)
} }
else -> { else -> {
mToast?.setGravity(mGravity, 0, 100) mToast?.setGravity(mGravity, 0, 100,)
} }
} }
} } catch (e: Exception,) { }
if (context is Activity) { if (context is Activity) {
(context as Activity).runOnUiThread { mToast?.show() } (context as Activity).runOnUiThread { mToast?.show() }
} else { } else {
mToast?.show() mToast?.show()
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
mToast?.addCallback(object : Toast.Callback() { mToast?.addCallback(
override fun onToastHidden() { object : Toast.Callback() {
super.onToastHidden() override fun onToastHidden() {
mToast = null super.onToastHidden()
} mToast = null
}) }
},
)
} }
result.success(true) result.success(true,)
} }
"cancel" -> { "cancel" -> {
if (mToast != null) { if (mToast != null) {
mToast?.cancel() mToast?.cancel()
mToast = null mToast = null
} }
result.success(true) result.success(true,)
} }
else -> result.notImplemented() 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