Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fix - fix toast message error when android version is 31+ #492

Merged
merged 2 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 9 additions & 7 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

if (textSize != null) {
textView.textSize = textSize.toFloat()
}
if (textcolor != null) {
textView.setTextColor(textcolor.toInt())
}
} catch (_: Exception) {

try {
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 (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() {
override fun onToastHidden() {
super.onToastHidden()
mToast = null
}
})
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()
}
Expand Down
Loading