diff --git a/README.md b/README.md index f6c115a0..613f2a68 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ Fluttertoast.showToast( | backgroundColor | Colors.red |null | | textcolor | Colors.white |null | | fontSize | 16.0 (float) | null | +| fontAsset | Path to a font file in the Flutter app assets folder, e.g. 'assets/path/to/some-font.ttf' (String) | null | | webShowClose | false (bool) | false | | webBgColor | String (hex Color) | linear-gradient(to right, #00b09b, #96c93d) | | webPosition | String (`left`, `center` or `right`) | right | diff --git a/android/src/main/kotlin/io/github/ponnamkarthik/toast/fluttertoast/MethodCallHandlerImpl.kt b/android/src/main/kotlin/io/github/ponnamkarthik/toast/fluttertoast/MethodCallHandlerImpl.kt index a3953100..0f2c7552 100644 --- a/android/src/main/kotlin/io/github/ponnamkarthik/toast/fluttertoast/MethodCallHandlerImpl.kt +++ b/android/src/main/kotlin/io/github/ponnamkarthik/toast/fluttertoast/MethodCallHandlerImpl.kt @@ -2,7 +2,9 @@ package io.github.ponnamkarthik.toast.fluttertoast import android.app.Activity import android.content.Context +import android.content.res.AssetManager import android.graphics.PorterDuff +import android.graphics.Typeface import android.graphics.drawable.Drawable import android.os.Build import android.view.Gravity @@ -13,7 +15,8 @@ import androidx.core.content.ContextCompat import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel.MethodCallHandler -import kotlin.Exception +import io.flutter.view.FlutterMain +import java.io.File internal class MethodCallHandlerImpl(private var context: Context) : MethodCallHandler { @@ -22,12 +25,13 @@ internal class MethodCallHandlerImpl(private var context: Context) : MethodCallH override fun onMethodCall(call: MethodCall, result: MethodChannel.Result,) { when (call.method) { "showToast" -> { - val mMessage = call.argument("msg",).toString() - val length = call.argument("length",).toString() - val gravity = call.argument("gravity",).toString() - val bgcolor = call.argument("bgcolor",) - val textcolor = call.argument("textcolor",) - val textSize = call.argument("fontSize",) + val mMessage = call.argument("msg").toString() + val length = call.argument("length").toString() + val gravity = call.argument("gravity").toString() + val bgcolor = call.argument("bgcolor") + val textcolor = call.argument("textcolor") + val fontSize = call.argument("fontSize") + val fontAsset = call.argument("fontAsset") val mGravity: Int = when (gravity) { "top" -> Gravity.TOP @@ -55,8 +59,8 @@ internal class MethodCallHandlerImpl(private var context: Context) : MethodCallH gradientDrawable!!.setColorFilter(bgcolor.toInt(), PorterDuff.Mode.SRC_IN) text.background = gradientDrawable - if (textSize != null) { - text.textSize = textSize.toFloat() + if (fontSize != null) { + text.textSize = fontSize.toFloat() } if (textcolor != null) { text.setTextColor(textcolor.toInt()) @@ -64,18 +68,29 @@ internal class MethodCallHandlerImpl(private var context: Context) : MethodCallH mToast = Toast(context,) mToast?.duration = mDuration + + if (fontAsset != null) { + val assetManager: AssetManager = context.assets + val key = FlutterMain.getLookupKeyForAsset(fontAsset) + text.typeface = Typeface.createFromAsset(assetManager, key); + } mToast?.view = layout } else { - try { - mToast = Toast.makeText(context, mMessage, mDuration,) - val textView: TextView = mToast?.view!!.findViewById(android.R.id.message,) - if (textSize != null) { - textView.textSize = textSize.toFloat() + mToast = Toast.makeText(context, mMessage, mDuration) + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { + val textView: TextView = mToast?.view!!.findViewById(android.R.id.message) + if (fontSize != null) { + textView.textSize = fontSize.toFloat() } if (textcolor != null) { textView.setTextColor(textcolor.toInt()) } - } catch (e: Exception,) { } + if (fontAsset != null) { + val assetManager: AssetManager = context.assets + val key = FlutterMain.getLookupKeyForAsset(fontAsset) + textView.typeface = Typeface.createFromAsset(assetManager, key); + } + } } try { @@ -91,7 +106,7 @@ internal class MethodCallHandlerImpl(private var context: Context) : MethodCallH } } } catch (e: Exception,) { } - + if (context is Activity) { (context as Activity).runOnUiThread { mToast?.show() } } else { diff --git a/lib/fluttertoast.dart b/lib/fluttertoast.dart index 582c2864..27d5b08c 100644 --- a/lib/fluttertoast.dart +++ b/lib/fluttertoast.dart @@ -43,18 +43,22 @@ class Fluttertoast { return res; } - /// Summons the platform's showToast which will display the message + /// Show the [msg] via native platform's toast. /// - /// Wraps the platform's native Toast for android. - /// Wraps the Plugin https://github.com/scalessec/Toast for iOS - /// Wraps the https://github.com/apvarun/toastify-js for Web + /// On Android uses Toast. + /// On iOS uses https://github.com/scalessec/Toast plugin. + /// On web uses https://github.com/apvarun/toastify-js library. /// - /// Parameter [msg] is required and all remaining are optional + /// Parameter [msg] is required and all remaining are optional. + /// + /// The [fontAsset] is the path to your Flutter asset to use in toast. + /// If not specified platform's default font will be used. static Future showToast({ required String msg, Toast? toastLength, int timeInSecForIosWeb = 1, double? fontSize, + String? fontAsset, ToastGravity? gravity, Color? backgroundColor, Color? textColor, @@ -93,6 +97,7 @@ class Fluttertoast { 'textcolor': textColor.value, 'iosTextcolor': textColor.value, 'fontSize': fontSize, + 'fontAsset': fontAsset, 'webShowClose': webShowClose, 'webBgColor': webBgColor, 'webPosition': webPosition