Commit 00c74faa by Karthik Ponnam Committed by GitHub

Merge pull request #50 from cqhchan/master

Added ability to register touch events and change font size
parents d4bba0db 11503f43
...@@ -4,7 +4,10 @@ import android.content.Context; ...@@ -4,7 +4,10 @@ import android.content.Context;
import android.graphics.PorterDuff; import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Build; import android.os.Build;
import android.os.Handler;
import android.view.Gravity; import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
...@@ -31,16 +34,19 @@ public class FluttertoastPlugin implements MethodCallHandler { ...@@ -31,16 +34,19 @@ public class FluttertoastPlugin implements MethodCallHandler {
} }
@Override @Override
public void onMethodCall(MethodCall call, Result result) { public void onMethodCall(MethodCall call, final Result result) {
if (call.method.equals("showToast")) { if (call.method.equals("showToast")) {
String msg = call.argument("msg").toString(); String msg = call.argument("msg").toString();
String length = call.argument("length").toString(); String length = call.argument("length").toString();
String gravity = call.argument("gravity").toString(); String gravity = call.argument("gravity").toString();
Number bgcolor = call.argument("bgcolor"); Number bgcolor = call.argument("bgcolor");
Number textcolor = call.argument("textcolor"); Number textcolor = call.argument("textcolor");
Number textSize = call.argument("fontSize");
Toast toast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT); final Toast toast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT);
//Added to see if
toast.setText(msg); toast.setText(msg);
...@@ -50,6 +56,22 @@ public class FluttertoastPlugin implements MethodCallHandler { ...@@ -50,6 +56,22 @@ public class FluttertoastPlugin implements MethodCallHandler {
toast.setDuration(Toast.LENGTH_SHORT); toast.setDuration(Toast.LENGTH_SHORT);
} }
Boolean sent = false;
final Handler handler = new Handler();
final Runnable run = new Runnable() {
@Override
public void run() {
try {
result.success(false);
} catch (Exception e){
e.printStackTrace();
}
}
};
switch (gravity) { switch (gravity) {
case "top": case "top":
toast.setGravity(Gravity.TOP, 0, 100); toast.setGravity(Gravity.TOP, 0, 100);
...@@ -61,9 +83,12 @@ public class FluttertoastPlugin implements MethodCallHandler { ...@@ -61,9 +83,12 @@ public class FluttertoastPlugin implements MethodCallHandler {
toast.setGravity(Gravity.BOTTOM, 0, 100); toast.setGravity(Gravity.BOTTOM, 0, 100);
} }
TextView text = toast.getView().findViewById(android.R.id.message); final TextView text = toast.getView().findViewById(android.R.id.message);
text.setTextSize(textSize.floatValue());
text.setMaxLines(1);
if(bgcolor != null) {
if(bgcolor != null) {
Drawable shapeDrawable = ContextCompat.getDrawable(ctx, R.drawable.toast_bg); Drawable shapeDrawable = ContextCompat.getDrawable(ctx, R.drawable.toast_bg);
if (shapeDrawable != null) { if (shapeDrawable != null) {
...@@ -76,15 +101,31 @@ public class FluttertoastPlugin implements MethodCallHandler { ...@@ -76,15 +101,31 @@ public class FluttertoastPlugin implements MethodCallHandler {
} }
} }
text.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
handler.removeCallbacks(run);
text.setOnTouchListener(null);
toast.cancel();
try {
result.success(true);
} catch (Exception e){
e.printStackTrace();
}
return false;
}
});
if(textcolor != null) { if(textcolor != null) {
text.setTextColor(textcolor.intValue()); text.setTextColor(textcolor.intValue());
} }
toast.show(); toast.show();
handler.postDelayed(run,toast.getDuration()*1000);
result.success("Success");
} else { } else {
result.notImplemented(); result.notImplemented();
} }
......
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
android:color="#000000"> android:color="#000000">
</solid> </solid>
<padding <padding
android:left="24dp" android:left="10dp"
android:top="15dp" android:top="8dp"
android:right="24dp" android:right="10dp"
android:bottom="15dp" /> android:bottom="8dp" />
</shape> </shape>
\ No newline at end of file
...@@ -4,9 +4,13 @@ ...@@ -4,9 +4,13 @@
static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast"; static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast";
@interface FluttertoastPlugin ()
@property(nonatomic, retain) FlutterMethodChannel *channel;
@end
@implementation FluttertoastPlugin { @implementation FluttertoastPlugin {
FlutterResult _result; FlutterResult _result;
} }
+ (void)registerWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar { + (void)registerWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
...@@ -16,6 +20,7 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast"; ...@@ -16,6 +20,7 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast";
UIViewController *viewController = UIViewController *viewController =
[UIApplication sharedApplication].delegate.window.rootViewController; [UIApplication sharedApplication].delegate.window.rootViewController;
FluttertoastPlugin *instance = [[FluttertoastPlugin alloc] init]; FluttertoastPlugin *instance = [[FluttertoastPlugin alloc] init];
instance.channel = channel;
[registrar addMethodCallDelegate:instance channel:channel]; [registrar addMethodCallDelegate:instance channel:channel];
} }
...@@ -38,7 +43,10 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast"; ...@@ -38,7 +43,10 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast";
NSString *durationTime = call.arguments[@"time"]; NSString *durationTime = call.arguments[@"time"];
NSNumber *bgcolor = call.arguments[@"bgcolor"]; NSNumber *bgcolor = call.arguments[@"bgcolor"];
NSNumber *textcolor = call.arguments[@"textcolor"]; NSNumber *textcolor = call.arguments[@"textcolor"];
NSNumber *size = call.arguments[@"size"];
NSNumber *fontSize = call.arguments[@"fontSize"];
CGFloat cgf = [fontSize doubleValue];
int time = 1; int time = 1;
@try { @try {
time = [durationTime intValue]; time = [durationTime intValue];
...@@ -51,7 +59,7 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast"; ...@@ -51,7 +59,7 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast";
CSToastStyle *style = [[CSToastStyle alloc] initWithDefaultStyle]; CSToastStyle *style = [[CSToastStyle alloc] initWithDefaultStyle];
style.messageFont = [UIFont systemFontOfSize:cgf];
style.backgroundColor = [self colorWithHex:bgcolor.unsignedIntegerValue]; style.backgroundColor = [self colorWithHex:bgcolor.unsignedIntegerValue];
style.messageColor = [self colorWithHex:textcolor.unsignedIntegerValue]; style.messageColor = [self colorWithHex:textcolor.unsignedIntegerValue];
...@@ -59,20 +67,37 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast"; ...@@ -59,20 +67,37 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast";
[[UIApplication sharedApplication].delegate.window.rootViewController.view makeToast:msg [[UIApplication sharedApplication].delegate.window.rootViewController.view makeToast:msg
duration:time duration:time
position:CSToastPositionTop position:CSToastPositionTop
style:style]; style:style
completion:^(BOOL didTap){
NSNumber *boolNumber = [NSNumber numberWithBool:didTap];
result(boolNumber);
}];
} else if ([gravity isEqualToString:@"center"]) { } else if ([gravity isEqualToString:@"center"]) {
[[UIApplication sharedApplication].delegate.window.rootViewController.view makeToast:msg [[UIApplication sharedApplication].delegate.window.rootViewController.view makeToast:msg
duration:time duration:time
position:CSToastPositionCenter position:CSToastPositionCenter
style:style]; style:style
completion:^(BOOL didTap){
NSNumber *boolNumber = [NSNumber numberWithBool:didTap];
result(boolNumber);
}];
} else { } else {
[[UIApplication sharedApplication].delegate.window.rootViewController.view makeToast:msg [[UIApplication sharedApplication].delegate.window.rootViewController.view makeToast:msg
duration:time duration:time
position:CSToastPositionBottom position:CSToastPositionBottom
style:style]; style:style
completion:^(BOOL didTap){
NSNumber *boolNumber = [NSNumber numberWithBool:didTap];
result(boolNumber);
}];
} }
result(@"done");
} else { } else {
result(FlutterMethodNotImplemented); result(FlutterMethodNotImplemented);
} }
......
...@@ -89,6 +89,26 @@ extern const NSString * CSToastPositionBottom; ...@@ -89,6 +89,26 @@ extern const NSString * CSToastPositionBottom;
@param duration The toast duration @param duration The toast duration
@param position The toast's center point. Can be one of the predefined CSToastPosition @param position The toast's center point. Can be one of the predefined CSToastPosition
constants or a `CGPoint` wrapped in an `NSValue` object. constants or a `CGPoint` wrapped in an `NSValue` object.
@param style The style. The shared style will be used when nil
@param completion The completion block, executed after the toast view disappears.
didTap will be `YES` if the toast view was dismissed from a tap.
*/
- (void)makeToast:(NSString *)message
duration:(NSTimeInterval)duration
position:(id)position
style:(CSToastStyle *)style
completion:(void(^)(BOOL didTap))completion;
/**
Creates and presents a new toast view with a message, title, and image. Duration,
position, and style can be set explicitly. The completion block executes when the
toast view completes. `didTap` will be `YES` if the toast view was dismissed from
a tap.
@param message The message to be displayed
@param duration The toast duration
@param position The toast's center point. Can be one of the predefined CSToastPosition
constants or a `CGPoint` wrapped in an `NSValue` object.
@param title The title @param title The title
@param image The image @param image The image
@param style The style. The shared style will be used when nil @param style The style. The shared style will be used when nil
......
...@@ -80,6 +80,11 @@ static const NSString * CSToastQueueKey = @"CSToastQueueKey"; ...@@ -80,6 +80,11 @@ static const NSString * CSToastQueueKey = @"CSToastQueueKey";
[self showToast:toast duration:duration position:position completion:nil]; [self showToast:toast duration:duration position:position completion:nil];
} }
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position style:(CSToastStyle *)style completion:(void(^)(BOOL didTap))completion {
UIView *toast = [self toastViewForMessage:message title:nil image:nil style:style];
[self showToast:toast duration:duration position:position completion:completion];
}
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position title:(NSString *)title image:(UIImage *)image style:(CSToastStyle *)style completion:(void(^)(BOOL didTap))completion { - (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position title:(NSString *)title image:(UIImage *)image style:(CSToastStyle *)style completion:(void(^)(BOOL didTap))completion {
UIView *toast = [self toastViewForMessage:message title:title image:image style:style]; UIView *toast = [self toastViewForMessage:message title:title image:image style:style];
[self showToast:toast duration:duration position:position completion:completion]; [self showToast:toast duration:duration position:position completion:completion];
...@@ -470,14 +475,14 @@ static const NSString * CSToastQueueKey = @"CSToastQueueKey"; ...@@ -470,14 +475,14 @@ static const NSString * CSToastQueueKey = @"CSToastQueueKey";
self.maxWidthPercentage = 0.8; self.maxWidthPercentage = 0.8;
self.maxHeightPercentage = 0.8; self.maxHeightPercentage = 0.8;
self.horizontalPadding = 10.0; self.horizontalPadding = 10.0;
self.verticalPadding = 10.0; self.verticalPadding = 8.0;
self.cornerRadius = 10.0; self.cornerRadius = 10.0;
self.titleFont = [UIFont boldSystemFontOfSize:16.0]; self.titleFont = [UIFont boldSystemFontOfSize:16.0];
self.messageFont = [UIFont systemFontOfSize:16.0]; self.messageFont = [UIFont systemFontOfSize:16.0];
self.titleAlignment = NSTextAlignmentLeft; self.titleAlignment = NSTextAlignmentLeft;
self.messageAlignment = NSTextAlignmentLeft; self.messageAlignment = NSTextAlignmentLeft;
self.titleNumberOfLines = 0; self.titleNumberOfLines = 0;
self.messageNumberOfLines = 0; self.messageNumberOfLines = 1;
self.displayShadow = NO; self.displayShadow = NO;
self.shadowOpacity = 0.8; self.shadowOpacity = 0.8;
self.shadowRadius = 6.0; self.shadowRadius = 6.0;
......
...@@ -13,14 +13,32 @@ class Fluttertoast { ...@@ -13,14 +13,32 @@ class Fluttertoast {
static const MethodChannel _channel = static const MethodChannel _channel =
const MethodChannel('PonnamKarthik/fluttertoast'); const MethodChannel('PonnamKarthik/fluttertoast');
static Future<String> showToast({ static Fluttertoast _instance;
static Fluttertoast get instance {
if (_instance == null) {
_instance =Fluttertoast._create();
}
return _instance;
}
Fluttertoast._create(){
}
Future<bool> showToast({
@required String msg, @required String msg,
Toast toastLength, Toast toastLength,
int timeInSecForIos = 1, int timeInSecForIos = 1,
double fontSize = 16.0,
ToastGravity gravity, ToastGravity gravity,
Color backgroundColor, Color backgroundColor,
Color textColor, Color textColor,
// Function(bool) didTap,
}) async { }) async {
// this.didTap = didTap;
String toast = "short"; String toast = "short";
if (toastLength == Toast.LENGTH_LONG) { if (toastLength == Toast.LENGTH_LONG) {
toast = "long"; toast = "long";
...@@ -48,9 +66,11 @@ class Fluttertoast { ...@@ -48,9 +66,11 @@ class Fluttertoast {
'gravity': gravityToast, 'gravity': gravityToast,
'bgcolor': backgroundColor != null ? backgroundColor.value : null, 'bgcolor': backgroundColor != null ? backgroundColor.value : null,
'textcolor': textColor != null ? textColor.value: null, 'textcolor': textColor != null ? textColor.value: null,
'fontSize': fontSize,
}; };
String res = await _channel.invokeMethod('showToast', params); bool 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