Skip to content

Commit 7aeb9df

Browse files
Abestanisnt4f04uNd
andauthored
Allow to use a font asset on Android (#519)
* init * + * Fix compilation with newer Kotlin version * Add new argument to the README --------- Co-authored-by: nt4f04uNd <nt4f04uNd@gmail.com>
1 parent fb8c7b9 commit 7aeb9df

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Fluttertoast.showToast(
6464
| backgroundColor | Colors.red |null |
6565
| textcolor | Colors.white |null |
6666
| fontSize | 16.0 (float) | null |
67+
| fontAsset | Path to a font file in the Flutter app assets folder, e.g. 'assets/path/to/some-font.ttf' (String) | null |
6768
| webShowClose | false (bool) | false |
6869
| webBgColor | String (hex Color) | linear-gradient(to right, #00b09b, #96c93d) |
6970
| webPosition | String (`left`, `center` or `right`) | right |

android/src/main/kotlin/io/github/ponnamkarthik/toast/fluttertoast/MethodCallHandlerImpl.kt

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package io.github.ponnamkarthik.toast.fluttertoast
22

33
import android.app.Activity
44
import android.content.Context
5+
import android.content.res.AssetManager
56
import android.graphics.PorterDuff
7+
import android.graphics.Typeface
68
import android.graphics.drawable.Drawable
79
import android.os.Build
810
import android.view.Gravity
@@ -13,7 +15,8 @@ import androidx.core.content.ContextCompat
1315
import io.flutter.plugin.common.MethodCall
1416
import io.flutter.plugin.common.MethodChannel
1517
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
16-
import kotlin.Exception
18+
import io.flutter.view.FlutterMain
19+
import java.io.File
1720

1821
internal class MethodCallHandlerImpl(private var context: Context) : MethodCallHandler {
1922

@@ -22,12 +25,13 @@ internal class MethodCallHandlerImpl(private var context: Context) : MethodCallH
2225
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result,) {
2326
when (call.method) {
2427
"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")
3135

3236
val mGravity: Int = when (gravity) {
3337
"top" -> Gravity.TOP
@@ -55,27 +59,38 @@ internal class MethodCallHandlerImpl(private var context: Context) : MethodCallH
5559
gradientDrawable!!.setColorFilter(bgcolor.toInt(), PorterDuff.Mode.SRC_IN)
5660
text.background = gradientDrawable
5761

58-
if (textSize != null) {
59-
text.textSize = textSize.toFloat()
62+
if (fontSize != null) {
63+
text.textSize = fontSize.toFloat()
6064
}
6165
if (textcolor != null) {
6266
text.setTextColor(textcolor.toInt())
6367
}
6468

6569
mToast = Toast(context,)
6670
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+
}
6777
mToast?.view = layout
6878
} 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()
7484
}
7585
if (textcolor != null) {
7686
textView.setTextColor(textcolor.toInt())
7787
}
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+
}
7994
}
8095

8196
try {
@@ -91,7 +106,7 @@ internal class MethodCallHandlerImpl(private var context: Context) : MethodCallH
91106
}
92107
}
93108
} catch (e: Exception,) { }
94-
109+
95110
if (context is Activity) {
96111
(context as Activity).runOnUiThread { mToast?.show() }
97112
} else {

lib/fluttertoast.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,22 @@ class Fluttertoast {
4343
return res;
4444
}
4545

46-
/// Summons the platform's showToast which will display the message
46+
/// Show the [msg] via native platform's toast.
4747
///
48-
/// Wraps the platform's native Toast for android.
49-
/// Wraps the Plugin https://github.com/scalessec/Toast for iOS
50-
/// Wraps the https://github.com/apvarun/toastify-js for Web
48+
/// On Android uses Toast.
49+
/// On iOS uses https://github.com/scalessec/Toast plugin.
50+
/// On web uses https://github.com/apvarun/toastify-js library.
5151
///
52-
/// Parameter [msg] is required and all remaining are optional
52+
/// Parameter [msg] is required and all remaining are optional.
53+
///
54+
/// The [fontAsset] is the path to your Flutter asset to use in toast.
55+
/// If not specified platform's default font will be used.
5356
static Future<bool?> showToast({
5457
required String msg,
5558
Toast? toastLength,
5659
int timeInSecForIosWeb = 1,
5760
double? fontSize,
61+
String? fontAsset,
5862
ToastGravity? gravity,
5963
Color? backgroundColor,
6064
Color? textColor,
@@ -93,6 +97,7 @@ class Fluttertoast {
9397
'textcolor': textColor.value,
9498
'iosTextcolor': textColor.value,
9599
'fontSize': fontSize,
100+
'fontAsset': fontAsset,
96101
'webShowClose': webShowClose,
97102
'webBgColor': webBgColor,
98103
'webPosition': webPosition

0 commit comments

Comments
 (0)