@@ -2,7 +2,9 @@ package io.github.ponnamkarthik.toast.fluttertoast
2
2
3
3
import android.app.Activity
4
4
import android.content.Context
5
+ import android.content.res.AssetManager
5
6
import android.graphics.PorterDuff
7
+ import android.graphics.Typeface
6
8
import android.graphics.drawable.Drawable
7
9
import android.os.Build
8
10
import android.view.Gravity
@@ -13,7 +15,8 @@ import androidx.core.content.ContextCompat
13
15
import io.flutter.plugin.common.MethodCall
14
16
import io.flutter.plugin.common.MethodChannel
15
17
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
16
- import kotlin.Exception
18
+ import io.flutter.view.FlutterMain
19
+ import java.io.File
17
20
18
21
internal class MethodCallHandlerImpl (private var context : Context ) : MethodCallHandler {
19
22
@@ -22,12 +25,13 @@ internal class MethodCallHandlerImpl(private var context: Context) : MethodCallH
22
25
override fun onMethodCall (call : MethodCall , result : MethodChannel .Result ,) {
23
26
when (call.method) {
24
27
" showToast" -> {
25
- val mMessage = call.argument<Any >(" msg" ,).toString()
26
- val length = call.argument<Any >(" length" ,).toString()
27
- val gravity = call.argument<Any >(" gravity" ,).toString()
28
- val bgcolor = call.argument<Number >(" bgcolor" ,)
29
- val textcolor = call.argument<Number >(" textcolor" ,)
30
- val textSize = call.argument<Number >(" fontSize" ,)
28
+ val mMessage = call.argument<Any >(" msg" ).toString()
29
+ val length = call.argument<Any >(" length" ).toString()
30
+ val gravity = call.argument<Any >(" gravity" ).toString()
31
+ val bgcolor = call.argument<Number >(" bgcolor" )
32
+ val textcolor = call.argument<Number >(" textcolor" )
33
+ val fontSize = call.argument<Number >(" fontSize" )
34
+ val fontAsset = call.argument<String >(" fontAsset" )
31
35
32
36
val mGravity: Int = when (gravity) {
33
37
" top" -> Gravity .TOP
@@ -55,27 +59,38 @@ internal class MethodCallHandlerImpl(private var context: Context) : MethodCallH
55
59
gradientDrawable!! .setColorFilter(bgcolor.toInt(), PorterDuff .Mode .SRC_IN )
56
60
text.background = gradientDrawable
57
61
58
- if (textSize != null ) {
59
- text.textSize = textSize .toFloat()
62
+ if (fontSize != null ) {
63
+ text.textSize = fontSize .toFloat()
60
64
}
61
65
if (textcolor != null ) {
62
66
text.setTextColor(textcolor.toInt())
63
67
}
64
68
65
69
mToast = Toast (context,)
66
70
mToast?.duration = mDuration
71
+
72
+ if (fontAsset != null ) {
73
+ val assetManager: AssetManager = context.assets
74
+ val key = FlutterMain .getLookupKeyForAsset(fontAsset)
75
+ text.typeface = Typeface .createFromAsset(assetManager, key);
76
+ }
67
77
mToast?.view = layout
68
78
} else {
69
- try {
70
- mToast = Toast .makeText(context, mMessage, mDuration,)
71
- val textView: TextView = mToast?.view!! .findViewById(android.R .id.message, )
72
- if (textSize != null ) {
73
- textView.textSize = textSize .toFloat()
79
+ mToast = Toast .makeText(context, mMessage, mDuration)
80
+ if ( Build . VERSION . SDK_INT < Build . VERSION_CODES . R ) {
81
+ val textView: TextView = mToast?.view!! .findViewById(android.R .id.message)
82
+ if (fontSize != null ) {
83
+ textView.textSize = fontSize .toFloat()
74
84
}
75
85
if (textcolor != null ) {
76
86
textView.setTextColor(textcolor.toInt())
77
87
}
78
- } catch (e: Exception ,) { }
88
+ if (fontAsset != null ) {
89
+ val assetManager: AssetManager = context.assets
90
+ val key = FlutterMain .getLookupKeyForAsset(fontAsset)
91
+ textView.typeface = Typeface .createFromAsset(assetManager, key);
92
+ }
93
+ }
79
94
}
80
95
81
96
try {
@@ -91,7 +106,7 @@ internal class MethodCallHandlerImpl(private var context: Context) : MethodCallH
91
106
}
92
107
}
93
108
} catch (e: Exception ,) { }
94
-
109
+
95
110
if (context is Activity ) {
96
111
(context as Activity ).runOnUiThread { mToast?.show() }
97
112
} else {
0 commit comments