Skip to content

Commit

Permalink
2.2.8
Browse files Browse the repository at this point in the history
  • Loading branch information
ponnamkarthik committed Jan 25, 2019
1 parent f590412 commit bf984e8
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 83 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [2.2.8]

* `Fluttertoast.cancel()` added
* FlutterToast Implementation revert back to previous

## [2.2.7]

* FontSize Can be changed
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ If your project uses androidx then use `fluttertoast` version `2.2.4` or `2.2.5`

```yaml
# add this line to your dependencies
fluttertoast: ^2.2.7
fluttertoast: ^2.2.8
```
```dart
import 'package:fluttertoast/fluttertoast.dart';
```

```dart
Fluttertoast.instance.showToast(
Fluttertoast.showToast(
msg: "This is Center Short Toast",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
Expand All @@ -43,6 +43,12 @@ textcolor| Colors.white
fontSize | 16.0 (float)


### To cancel all the toasts call

```dart
Fluttertoast.cancel()
```

## Preview Images

<img src="https://raw.githubusercontent.com/PonnamKarthik/FlutterToast/master/screenshot/1.png" width="320px" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
/** FluttertoastPlugin */
public class FluttertoastPlugin implements MethodCallHandler {

Context ctx;
private Context ctx;
private Toast toast = null;

FluttertoastPlugin(Context context) {
private FluttertoastPlugin(Context context) {
ctx = context;
}

Expand All @@ -35,7 +36,23 @@ public static void registerWith(Registrar registrar) {

@Override
public void onMethodCall(MethodCall call, final Result result) {
if (call.method.equals("showToast")) {
switch (call.method) {
case "showToast":
showToast(call, result);
break;
case "cancel":
if(toast != null) {
toast.cancel();
}
result.success(true);
break;
default:
result.notImplemented();
break;
}
}

private void showToast(MethodCall call, Result result) {
String msg = call.argument("msg").toString();
String length = call.argument("length").toString();
String gravity = call.argument("gravity").toString();
Expand All @@ -44,33 +61,32 @@ public void onMethodCall(MethodCall call, final Result result) {
Number textSize = call.argument("fontSize");


final Toast toast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT);
//Added to see if

toast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT);

toast.setText(msg);

if(length.equals("long")) {
toast.setDuration(Toast.LENGTH_LONG);
toast.setDuration(Toast.LENGTH_LONG);
} else {
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);
// later
// 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();
// }
// }
// };

} catch (Exception e){
e.printStackTrace();
}
}
};


switch (gravity) {
case "top":
Expand All @@ -81,14 +97,14 @@ public void run() {
break;
default:
toast.setGravity(Gravity.BOTTOM, 0, 100);
}
}

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);

if (shapeDrawable != null) {
Expand All @@ -101,33 +117,31 @@ public void run() {
}

}
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;
}
});
//later
// 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) {
text.setTextColor(textcolor.intValue());
}

toast.show();
handler.postDelayed(run,toast.getDuration()*1000);

} else {
result.notImplemented();
}
result.success(true);
// handler.postDelayed(run,toast.getDuration()*1000);
}
}
21 changes: 16 additions & 5 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,47 @@ class _MyAppState extends State<MyApp> {
}

void showLongToast() {
Fluttertoast.instance.showToast(
Fluttertoast.showToast(
msg: "This is Long Toast",
toastLength: Toast.LENGTH_LONG,
);
}

void showColoredToast() {
Fluttertoast.instance.showToast(
Fluttertoast.showToast(
msg: "This is Colored Toast",
toastLength: Toast.LENGTH_SHORT,
backgroundColor: Colors.red,
textColor: Colors.white);
}

void showShortToast() {
Fluttertoast.instance.showToast(
Fluttertoast.showToast(
msg: "This is Short Toast",
toastLength: Toast.LENGTH_SHORT,
timeInSecForIos: 1);
}

void showTopShortToast() {
Fluttertoast.instance.showToast(
Fluttertoast.showToast(
msg: "This is Top Short Toast",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.TOP,
timeInSecForIos: 1);
}

void showCenterShortToast() {
Fluttertoast.instance.showToast(
Fluttertoast.showToast(
msg: "This is Center Short Toast",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIos: 1);
}

void cancelToast() {
Fluttertoast.cancel();
}

@override
Widget build(BuildContext context) {
return new MaterialApp(
Expand Down Expand Up @@ -92,6 +96,13 @@ class _MyAppState extends State<MyApp> {
child: new Text('Show Colored Toast'),
onPressed: showColoredToast),
),
new Padding(
padding: const EdgeInsets.all(10.0),
child: new RaisedButton(
child: new Text('Cancel Toasts'),
onPressed: cancelToast,
),
),
],
),
),
Expand Down
27 changes: 7 additions & 20 deletions ios/Classes/FluttertoastPlugin.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#import "FluttertoastPlugin.h"
#import "UIView+Toast.h"
// #import <fluttertoast/fluttertoast-Swift.h>

static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast";

Expand Down Expand Up @@ -37,7 +36,9 @@ - (UIColor*) colorWithHex: (NSUInteger)hex {
}

- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
if ([@"showToast" isEqualToString:call.method]) {
if([@"cancel" isEqualToString:call.method]) {
[[UIApplication sharedApplication].delegate.window.rootViewController.view hideAllToasts];
} else if ([@"showToast" isEqualToString:call.method]) {
NSString *msg = call.arguments[@"msg"];
NSString *gravity = call.arguments[@"gravity"];
NSString *durationTime = call.arguments[@"time"];
Expand Down Expand Up @@ -68,35 +69,21 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
duration:time
position:CSToastPositionTop
style:style
completion:^(BOOL didTap){

NSNumber *boolNumber = [NSNumber numberWithBool:didTap];
result(boolNumber);

}];
];
} else if ([gravity isEqualToString:@"center"]) {
[[UIApplication sharedApplication].delegate.window.rootViewController.view makeToast:msg
duration:time
position:CSToastPositionCenter
style:style
completion:^(BOOL didTap){

NSNumber *boolNumber = [NSNumber numberWithBool:didTap];
result(boolNumber);

}];
];
} else {
[[UIApplication sharedApplication].delegate.window.rootViewController.view makeToast:msg
duration:time
position:CSToastPositionBottom
style:style
completion:^(BOOL didTap){

NSNumber *boolNumber = [NSNumber numberWithBool:didTap];
result(boolNumber);

}];
];
}
result(true)

} else {
result(FlutterMethodNotImplemented);
Expand Down
23 changes: 14 additions & 9 deletions lib/fluttertoast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,27 @@ class Fluttertoast {
static const MethodChannel _channel =
const MethodChannel('PonnamKarthik/fluttertoast');

static Fluttertoast _instance;
// for Version 3.x.x

static Fluttertoast get instance {
if (_instance == null) {
_instance =Fluttertoast._create();
}
return _instance;
}
// static Fluttertoast _instance;

// static Fluttertoast get instance {
// if (_instance == null) {
// _instance =Fluttertoast._create();
// }
// return _instance;
// }

// Fluttertoast._create(){
// }

Fluttertoast._create(){
static Future<bool> cancel() async {
bool res = await _channel.invokeMethod("cancel");
return res;
}


Future<bool> showToast({
static Future<bool> showToast({
@required String msg,
Toast toastLength,
int timeInSecForIos = 1,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: fluttertoast
description: Toast Library for FLutter
version: 2.2.7
version: 2.2.8
author: Karthik Ponnam <[email protected]>
homepage: https://github.com/PonnamKarthik/FlutterToast

Expand Down

0 comments on commit bf984e8

Please sign in to comment.