Commit ec1b3b26 by Ponnam Karthik

Added color support for android

parent dfa8c09c
...@@ -11,7 +11,7 @@ Android Toast Library for Flutter ...@@ -11,7 +11,7 @@ Android Toast Library for Flutter
```yaml ```yaml
# add this line to your dependencies # add this line to your dependencies
fluttertoast: ^2.0.3 fluttertoast: ^2.1.0
``` ```
```dart ```dart
...@@ -19,7 +19,9 @@ Fluttertoast.showToast( ...@@ -19,7 +19,9 @@ Fluttertoast.showToast(
msg: "This is Center Short Toast", msg: "This is Center Short Toast",
toastLength: Toast.LENGTH_SHORT, toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER, gravity: ToastGravity.CENTER,
timeInSecForIos: 1 timeInSecForIos: 1,
bgcolor: "#e74c3c",
textcolor: '#ffffff'
); );
``` ```
...@@ -29,4 +31,6 @@ msg | String (Not Null)(required) ...@@ -29,4 +31,6 @@ msg | String (Not Null)(required)
toastLength| Toast.LENGTH_SHORT or Toast.LENGTH_LONG (optional) toastLength| Toast.LENGTH_SHORT or Toast.LENGTH_LONG (optional)
gravity | ToastGravity.TOP (or) ToastGravity.CENTER (or) ToastGravity.BOTTOM gravity | ToastGravity.TOP (or) ToastGravity.CENTER (or) ToastGravity.BOTTOM
timeInSecForIos | int (only for ios) timeInSecForIos | int (only for ios)
bgcolor | string (color in hex format)
textcolor: '#ffffff'
...@@ -4,11 +4,22 @@ import android.content.Context ...@@ -4,11 +4,22 @@ import android.content.Context
import android.util.Log import android.util.Log
import android.view.Gravity import android.view.Gravity
import android.widget.Toast import android.widget.Toast
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.PluginRegistry.Registrar import io.flutter.plugin.common.PluginRegistry.Registrar
import android.widget.TextView
import android.graphics.Paint
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.RoundRectShape
class FluttertoastPlugin(context: Context): MethodCallHandler { class FluttertoastPlugin(context: Context): MethodCallHandler {
...@@ -30,6 +41,8 @@ class FluttertoastPlugin(context: Context): MethodCallHandler { ...@@ -30,6 +41,8 @@ class FluttertoastPlugin(context: Context): MethodCallHandler {
val msg: String = call.argument("msg") val msg: String = call.argument("msg")
val length: String = call.argument("length") val length: String = call.argument("length")
val gravity: String = call.argument("gravity") val gravity: String = call.argument("gravity")
val bgcolor: String = call.argument("bgcolor")
val textcolor: String = call.argument("textcolor")
var toast: Toast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT); var toast: Toast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT);
...@@ -47,6 +60,32 @@ class FluttertoastPlugin(context: Context): MethodCallHandler { ...@@ -47,6 +60,32 @@ class FluttertoastPlugin(context: Context): MethodCallHandler {
toast.setGravity(Gravity.BOTTOM, 0, 100) toast.setGravity(Gravity.BOTTOM, 0, 100)
} }
val text: TextView = toast.view.findViewById(android.R.id.message)
if(bgcolor != "null") {
try {
val rectShape = RoundRectShape(floatArrayOf(50f, 50f, 50f, 50f, 50f, 50f, 50f, 50f), null, null)
val shapeDrawable = ShapeDrawable(rectShape)
shapeDrawable.paint.color = Color.parseColor(bgcolor)
shapeDrawable.paint.style = Paint.Style.FILL
shapeDrawable.paint.isAntiAlias = true
shapeDrawable.paint.flags = Paint.ANTI_ALIAS_FLAG
text.setBackgroundDrawable(shapeDrawable)
} catch (e: Exception) {
e.printStackTrace()
}
}
if(textcolor != "null") {
try {
text.setTextColor(Color.parseColor(textcolor))
} catch (e: Exception) {
e.printStackTrace()
}
}
toast.show() toast.show()
result.success("Success") result.success("Success")
......
...@@ -23,6 +23,15 @@ class _MyAppState extends State<MyApp> { ...@@ -23,6 +23,15 @@ class _MyAppState extends State<MyApp> {
); );
} }
void showColoredToast() {
Fluttertoast.showToast(
msg: "This is Colored Toast",
toastLength: Toast.LENGTH_SHORT,
bgcolor: "#e74c3c",
textcolor: '#ffffff'
);
}
void showShortToast() { void showShortToast() {
Fluttertoast.showToast( Fluttertoast.showToast(
msg: "This is Short Toast", msg: "This is Short Toast",
...@@ -85,6 +94,13 @@ class _MyAppState extends State<MyApp> { ...@@ -85,6 +94,13 @@ class _MyAppState extends State<MyApp> {
onPressed: showTopShortToast onPressed: showTopShortToast
), ),
), ),
new Padding(
padding: const EdgeInsets.all(10.0),
child: new RaisedButton(
child: new Text('Show Colored Toast'),
onPressed: showColoredToast
),
),
], ],
), ),
), ),
......
...@@ -22,8 +22,8 @@ ...@@ -22,8 +22,8 @@
<content url="file://$MODULE_DIR$/example/android"> <content url="file://$MODULE_DIR$/example/android">
<sourceFolder url="file://$MODULE_DIR$/example/android/app/src/main/java" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/example/android/app/src/main/java" isTestSource="false" />
</content> </content>
<orderEntry type="jdk" jdkName="Android API 25 Platform" jdkType="Android SDK" /> <orderEntry type="jdk" jdkName="Android API 27 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Flutter for Android" level="project" /> <orderEntry type="library" name="Flutter for Android" level="project" />
</component> </component>
</module> </module>
\ No newline at end of file
...@@ -61,4 +61,19 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast"; ...@@ -61,4 +61,19 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast";
[[UIApplication sharedApplication].delegate.window.rootViewController.view makeToast:msg]; [[UIApplication sharedApplication].delegate.window.rootViewController.view makeToast:msg];
} }
- (UIColor *)getUIColorObjectFromHexString:(NSString *)hexStr alpha:(CGFloat)alpha
{
// Convert hex string to an integer
unsigned int hexint = [self intFromHexString:hexStr];
// Create color object, specifying alpha as well
UIColor *color =
[UIColor colorWithRed:((CGFloat) ((hexint & 0xFF0000) >> 16))/255
green:((CGFloat) ((hexint & 0xFF00) >> 8))/255
blue:((CGFloat) (hexint & 0xFF))/255
alpha:alpha];
return color;
}
@end @end
...@@ -25,7 +25,9 @@ class Fluttertoast { ...@@ -25,7 +25,9 @@ class Fluttertoast {
@required String msg, @required String msg,
Toast toastLength, Toast toastLength,
int timeInSecForIos = 1, int timeInSecForIos = 1,
ToastGravity gravity ToastGravity gravity,
String bgcolor = "null",
String textcolor = "null"
}) async { }) async {
String toast = "short"; String toast = "short";
if(toastLength == Toast.LENGTH_LONG) { if(toastLength == Toast.LENGTH_LONG) {
...@@ -45,7 +47,9 @@ class Fluttertoast { ...@@ -45,7 +47,9 @@ class Fluttertoast {
'msg': msg, 'msg': msg,
'length': toast, 'length': toast,
'time': timeInSecForIos, 'time': timeInSecForIos,
'gravity': gravityToast 'gravity': gravityToast,
'bgcolor': bgcolor,
'textcolor': textcolor
}; };
String res = await _channel.invokeMethod('showToast', params); String res = await _channel.invokeMethod('showToast', params);
return res; return res;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment