Skip to content

Commit

Permalink
Allow to use a font asset on Android (#519)
Browse files Browse the repository at this point in the history
* init

* +

* Fix compilation with newer Kotlin version

* Add new argument to the README

---------

Co-authored-by: nt4f04uNd <nt4f04uNd@gmail.com>
  • Loading branch information
Abestanis and nt4f04uNd authored Aug 14, 2024
1 parent fb8c7b9 commit 7aeb9df
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {

Expand All @@ -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<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 fontSize = call.argument<Number>("fontSize")
val fontAsset = call.argument<String>("fontAsset")

val mGravity: Int = when (gravity) {
"top" -> Gravity.TOP
Expand Down Expand Up @@ -55,27 +59,38 @@ 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())
}

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 {
Expand All @@ -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 {
Expand Down
15 changes: 10 additions & 5 deletions lib/fluttertoast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool?> showToast({
required String msg,
Toast? toastLength,
int timeInSecForIosWeb = 1,
double? fontSize,
String? fontAsset,
ToastGravity? gravity,
Color? backgroundColor,
Color? textColor,
Expand Down Expand Up @@ -93,6 +97,7 @@ class Fluttertoast {
'textcolor': textColor.value,
'iosTextcolor': textColor.value,
'fontSize': fontSize,
'fontAsset': fontAsset,
'webShowClose': webShowClose,
'webBgColor': webBgColor,
'webPosition': webPosition
Expand Down

0 comments on commit 7aeb9df

Please sign in to comment.