Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use a font asset on Android #519

Merged
merged 4 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -42,18 +42,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 @@ -92,6 +96,7 @@ class Fluttertoast {
'textcolor': textColor.value,
'iosTextcolor': textColor.value,
'fontSize': fontSize,
'fontAsset': fontAsset,
'webShowClose': webShowClose,
'webBgColor': webBgColor,
'webPosition': webPosition
Expand Down