Commit eb33fb4c by Caijinglong

the android toast use single instance

parent c1160f2b
package io.github.ponnamkarthik.toast.fluttertoast package io.github.ponnamkarthik.toast.fluttertoast
import android.content.Context import android.content.Context
import android.util.Log
import android.view.Gravity
import android.widget.Toast
import android.graphics.Color import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.RoundRectShape
import android.view.Gravity
import android.widget.TextView
import android.widget.Toast
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.PluginRegistry.Registrar import io.flutter.plugin.common.PluginRegistry.Registrar
import android.widget.TextView
import android.graphics.Paint
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.RoundRectShape
class FluttertoastPlugin(context: Context) : MethodCallHandler {
var ctx: Context? = context
class FluttertoastPlugin(context: Context): MethodCallHandler {
var ctx: Context? = context
var toast: Toast? = null
companion object { companion object {
var toast: Toast? = null
@JvmStatic @JvmStatic
fun registerWith(registrar: Registrar): Unit { fun registerWith(registrar: Registrar): Unit {
val channel = MethodChannel(registrar.messenger(), "PonnamKarthik/fluttertoast") val channel = MethodChannel(registrar.messenger(), "PonnamKarthik/fluttertoast")
channel.setMethodCallHandler(FluttertoastPlugin(registrar.context())) channel.setMethodCallHandler(FluttertoastPlugin(registrar.context()))
} }
} }
var defaultTextColor: Int = Color.TRANSPARENT
override fun onMethodCall(call: MethodCall, result: Result): Unit { override fun onMethodCall(call: MethodCall, result: Result): Unit {
if (call.method.equals("showToast")) { if (call.method == "showToast") {
val msg: String = call.argument("msg") val msg: String = call.argument("msg")
val length: String = call.argument("length") val length: String = call.argument("length")
val gravity: String = call.argument("gravity") val gravity: String = call.argument("gravity")
val bgcolor: String = call.argument("bgcolor") val bgcolor: String = call.argument("bgcolor")
val textcolor: String = call.argument("textcolor") val textcolor: String = call.argument("textcolor")
val toast: Toast = this.toast?:Toast.makeText(ctx, msg, Toast.LENGTH_SHORT); val toast: Toast = FluttertoastPlugin.toast ?: Toast.makeText(ctx, msg, Toast.LENGTH_SHORT);
this.toast = toast FluttertoastPlugin.toast = toast
toast.setText(msg) toast.setText(msg)
if(length.equals("long")) { if (length.equals("long")) {
toast.duration = Toast.LENGTH_LONG toast.duration = Toast.LENGTH_LONG
} else { } else {
toast.duration = Toast.LENGTH_SHORT toast.duration = Toast.LENGTH_SHORT
} }
if(gravity.equals("top")) { when (gravity) {
toast.setGravity(Gravity.TOP, 0, 100) "top" -> toast.setGravity(Gravity.TOP, 0, 100)
} else if(gravity.equals("center")) { "center" -> toast.setGravity(Gravity.CENTER, 0, 0)
toast.setGravity(Gravity.CENTER, 0, 0) else -> toast.setGravity(Gravity.BOTTOM, 0, 100)
} else {
toast.setGravity(Gravity.BOTTOM, 0, 100)
} }
val text: TextView = toast.view.findViewById(android.R.id.message) val text: TextView = toast.view.findViewById(android.R.id.message)
if(bgcolor != "null") { if (defaultTextColor == 0) {
defaultTextColor = text.currentTextColor
}
if (bgcolor != "null") {
try { try {
...@@ -78,13 +77,17 @@ class FluttertoastPlugin(context: Context): MethodCallHandler { ...@@ -78,13 +77,17 @@ class FluttertoastPlugin(context: Context): MethodCallHandler {
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
} }
} else {
text.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
} }
if(textcolor != "null") { if (textcolor != "null") {
try { try {
text.setTextColor(Color.parseColor(textcolor)) text.setTextColor(Color.parseColor(textcolor))
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
} }
} else {
text.setTextColor(defaultTextColor)
} }
toast.show() toast.show()
......
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