Commit e457a4f2 by Karthik Ponnam Committed by GitHub

Merge pull request #32 from s2nventures/master

A few fixes and improvements
parents da7609f5 1739d1a4
......@@ -8,7 +8,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.android.tools.build:gradle:3.2.1'
}
}
......
......@@ -8,14 +8,11 @@ import io.flutter.plugin.common.PluginRegistry.Registrar;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Build;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
/** FluttertoastPlugin */
public class FluttertoastPlugin implements MethodCallHandler {
......@@ -40,64 +37,61 @@ public class FluttertoastPlugin implements MethodCallHandler {
String msg = call.argument("msg").toString();
String length = call.argument("length").toString();
String gravity = call.argument("gravity").toString();
String bgcolor = call.argument("bgcolor").toString();
String textcolor = call.argument("textcolor").toString();
Long bgcolor = call.argument("bgcolor");
Long textcolor = call.argument("textcolor");
Toast toast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT);
toast.setText(msg);
if(length.equals("long")) {
toast.setDuration(Toast.LENGTH_LONG);
} else {
toast.setDuration(Toast.LENGTH_SHORT);
}
switch (gravity) {
case "top":
toast.setGravity(Gravity.TOP, 0, 100);
break;
case "center":
toast.setGravity(Gravity.CENTER, 0, 0);
break;
default:
toast.setGravity(Gravity.BOTTOM, 0, 100);
}
TextView text = toast.getView().findViewById(android.R.id.message);
if (defaultTextColor == 0) {
defaultTextColor = text.getCurrentTextColor();
}
if (!bgcolor.equals("null")) {
try {
RoundRectShape rectShape = new RoundRectShape(new float[] {100f, 100f, 100f, 100f, 100f, 100f, 100f, 100f}, null, null);
ShapeDrawable shapeDrawable = new ShapeDrawable(rectShape);
shapeDrawable.getPaint().setColor(Color.parseColor(bgcolor));
shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
shapeDrawable.getPaint().setAntiAlias(true);
shapeDrawable.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
if (android.os.Build.VERSION.SDK_INT <= 27) {
toast.getView().setBackground(shapeDrawable);
} else {
text.setBackground(shapeDrawable);
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (!textcolor.equals("null")) {
try {
text.setTextColor(Color.parseColor(textcolor));
} catch (Exception e) {
e.printStackTrace();
}
} else {
text.setTextColor(defaultTextColor);
toast.setText(msg);
if(length.equals("long")) {
toast.setDuration(Toast.LENGTH_LONG);
} else {
toast.setDuration(Toast.LENGTH_SHORT);
}
switch (gravity) {
case "top":
toast.setGravity(Gravity.TOP, 0, 100);
break;
case "center":
toast.setGravity(Gravity.CENTER, 0, 0);
break;
default:
toast.setGravity(Gravity.BOTTOM, 0, 100);
}
toast.show();
TextView text = toast.getView().findViewById(android.R.id.message);
if (defaultTextColor == 0) {
defaultTextColor = text.getCurrentTextColor();
}
try {
RoundRectShape rectShape = new RoundRectShape(new float[] {100f, 100f, 100f, 100f, 100f, 100f, 100f, 100f}, null, null);
ShapeDrawable shapeDrawable = new ShapeDrawable(rectShape);
shapeDrawable.getPaint().setColor(bgcolor != null ? bgcolor.intValue() : Color.BLACK);
shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
shapeDrawable.getPaint().setAntiAlias(true);
shapeDrawable.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
if (android.os.Build.VERSION.SDK_INT <= 27) {
toast.getView().setBackground(shapeDrawable);
} else {
text.setBackground(shapeDrawable);
}
} catch (Exception e) {
e.printStackTrace();
}
try {
text.setTextColor(textcolor != null ? textcolor.intValue() : defaultTextColor);
} catch (Exception e) {
e.printStackTrace();
}
toast.show();
result.success("Success");
......
......@@ -25,8 +25,8 @@ class _MyAppState extends State<MyApp> {
Fluttertoast.showToast(
msg: "This is Colored Toast",
toastLength: Toast.LENGTH_SHORT,
bgcolor: "#e74c3c",
textcolor: '#ffffff');
backgroundColor: Colors.red,
textColor: Colors.white);
}
void showShortToast() {
......
......@@ -9,116 +9,73 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast";
}
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
FlutterMethodChannel* channel = [FlutterMethodChannel
methodChannelWithName:CHANNEL_NAME
binaryMessenger:[registrar messenger]];
+ (void)registerWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
FlutterMethodChannel *channel = [FlutterMethodChannel
methodChannelWithName:CHANNEL_NAME
binaryMessenger:[registrar messenger]];
UIViewController *viewController =
[UIApplication sharedApplication].delegate.window.rootViewController;
FluttertoastPlugin* instance = [[FluttertoastPlugin alloc] init];
[UIApplication sharedApplication].delegate.window.rootViewController;
FluttertoastPlugin *instance = [[FluttertoastPlugin alloc] init];
[registrar addMethodCallDelegate:instance channel:channel];
}
- (unsigned int)intFromHexString:(NSString *)hexStr {
unsigned int hexInt = 0;
NSScanner *scanner = [NSScanner scannerWithString:hexStr];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"#"]];
[scanner scanHexInt:&hexInt];
return hexInt;
}
- (UIColor *)getUIColorObjectFromHexString:(NSString *)hexStr alpha:(CGFloat)alpha
{
// Convert hex string to an integer
unsigned int hexint = [self intFromHexString:hexStr];
- (UIColor*) colorWithHex: (NSUInteger)hex {
CGFloat red, green, blue, alpha;
// 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];
red = ((CGFloat)((hex >> 16) & 0xFF)) / ((CGFloat)0xFF);
green = ((CGFloat)((hex >> 8) & 0xFF)) / ((CGFloat)0xFF);
blue = ((CGFloat)((hex >> 0) & 0xFF)) / ((CGFloat)0xFF);
alpha = hex > 0xFFFFFF ? ((CGFloat)((hex >> 24) & 0xFF)) / ((CGFloat)0xFF) : 1;
return color;
return [UIColor colorWithRed: red green:green blue:blue alpha:alpha];
}
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
if ([@"showToast" isEqualToString:call.method]) {
NSString *msg = call.arguments[@"msg"];
NSString *gravity = call.arguments[@"gravity"];
NSString *durationTime = call.arguments[@"time"];
NSString *bgcolor = call.arguments[@"bgcolor"];
NSString *textcolor = call.arguments[@"textcolor"];
NSNumber *bgcolor = call.arguments[@"bgcolor"];
NSNumber *textcolor = call.arguments[@"textcolor"];
int time = 1;
@try{
@try {
time = [durationTime intValue];
} @catch(NSException *e){
} @catch (NSException *e) {
time = 3;
}
if(time > 10 ) time = 10;
else if(time < 1) time = 1;
if (time > 10) time = 10;
else if (time < 1) time = 1;
CSToastStyle *style = [[CSToastStyle alloc] initWithDefaultStyle];
if(![bgcolor isEqualToString:@"null"]) {
style.self.backgroundColor = [self getUIColorObjectFromHexString:bgcolor alpha:1.0];
}
if(![textcolor isEqualToString:@"null"]) {
style.messageColor = [self getUIColorObjectFromHexString:textcolor alpha:1.0];
}
if([gravity isEqualToString:@"top"]) {
style.backgroundColor = [self colorWithHex:bgcolor.unsignedIntegerValue];
style.messageColor = [self colorWithHex:textcolor.unsignedIntegerValue];
if ([gravity isEqualToString:@"top"]) {
[[UIApplication sharedApplication].delegate.window.rootViewController.view makeToast:msg
duration: time
duration:time
position:CSToastPositionTop
style:style];
} else if([gravity isEqualToString:@"center"]) {
style:style];
} else if ([gravity isEqualToString:@"center"]) {
[[UIApplication sharedApplication].delegate.window.rootViewController.view makeToast:msg
duration: time
duration:time
position:CSToastPositionCenter
style:style];
style:style];
} else {
[[UIApplication sharedApplication].delegate.window.rootViewController.view makeToast:msg
duration: time
duration:time
position:CSToastPositionBottom
style:style];
style:style];
}
result(@"done");
} else {
result(FlutterMethodNotImplemented);
}
}
// - (void)showToastView:(FlutterMethodCall*)call:(NSString*)msg {
// [[UIApplication sharedApplication].delegate.window.rootViewController.view makeToast:msg];
// }
// func hexStringToUIColor (hex:String) -> UIColor {
// var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
// if (cString.hasPrefix("#")) {
// cString.remove(at: cString.startIndex)
// }
// if ((cString.count) != 6) {
// return UIColor.gray
// }
// var rgbValue:UInt32 = 0
// Scanner(string: cString).scanHexInt32(&rgbValue)
// return UIColor(
// red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
// green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
// blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
// alpha: CGFloat(1.0)
// )
// }
@end
import 'dart:async';
import 'dart:ui';
import 'package:flutter/services.dart';
import 'package:meta/meta.dart';
enum Toast { LENGTH_SHORT, LENGTH_LONG }
enum Toast {
LENGTH_SHORT,
LENGTH_LONG
}
enum ToastGravity {
TOP,
BOTTOM,
CENTER
}
enum ToastGravity { TOP, BOTTOM, CENTER }
class Fluttertoast {
static const MethodChannel _channel =
const MethodChannel('PonnamKarthik/fluttertoast');
const MethodChannel('PonnamKarthik/fluttertoast');
static Future<String> showToast ({
static Future<String> showToast({
@required String msg,
Toast toastLength,
int timeInSecForIos = 1,
ToastGravity gravity,
String bgcolor = "null",
String textcolor = "null"
Color backgroundColor = const Color.fromARGB(255, 0, 0, 0),
Color textColor = const Color.fromARGB(255, 255, 255, 255),
}) async {
String toast = "short";
if(toastLength == Toast.LENGTH_LONG) {
if (toastLength == Toast.LENGTH_LONG) {
toast = "long";
}
String gravityToast = "bottom";
if(gravity == ToastGravity.TOP) {
if (gravity == ToastGravity.TOP) {
gravityToast = "top";
} else if(gravity == ToastGravity.CENTER) {
} else if (gravity == ToastGravity.CENTER) {
gravityToast = "center";
} else {
gravityToast = "bottom";
}
final Map<String, dynamic> params = <String, dynamic> {
final Map<String, dynamic> params = <String, dynamic>{
'msg': msg,
'length': toast,
'time': timeInSecForIos,
'gravity': gravityToast,
'bgcolor': bgcolor,
'textcolor': textcolor
'bgcolor': backgroundColor.value,
'textcolor': textColor.value,
};
String res = await _channel.invokeMethod('showToast', params);
return res;
}
}
\ No newline at end of file
}
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