Commit bf984e85 by Karthik

2.2.8

parent f5904128
## [2.2.8]
* `Fluttertoast.cancel()` added
* FlutterToast Implementation revert back to previous
## [2.2.7] ## [2.2.7]
* FontSize Can be changed * FontSize Can be changed
......
...@@ -13,7 +13,7 @@ If your project uses androidx then use `fluttertoast` version `2.2.4` or `2.2.5` ...@@ -13,7 +13,7 @@ If your project uses androidx then use `fluttertoast` version `2.2.4` or `2.2.5`
```yaml ```yaml
# add this line to your dependencies # add this line to your dependencies
fluttertoast: ^2.2.7 fluttertoast: ^2.2.8
``` ```
```dart ```dart
...@@ -21,7 +21,7 @@ import 'package:fluttertoast/fluttertoast.dart'; ...@@ -21,7 +21,7 @@ import 'package:fluttertoast/fluttertoast.dart';
``` ```
```dart ```dart
Fluttertoast.instance.showToast( 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,
...@@ -43,6 +43,12 @@ textcolor| Colors.white ...@@ -43,6 +43,12 @@ textcolor| Colors.white
fontSize | 16.0 (float) fontSize | 16.0 (float)
### To cancel all the toasts call
```dart
Fluttertoast.cancel()
```
## Preview Images ## Preview Images
<img src="https://raw.githubusercontent.com/PonnamKarthik/FlutterToast/master/screenshot/1.png" width="320px" /> <img src="https://raw.githubusercontent.com/PonnamKarthik/FlutterToast/master/screenshot/1.png" width="320px" />
......
...@@ -21,9 +21,10 @@ import io.flutter.plugin.common.PluginRegistry.Registrar; ...@@ -21,9 +21,10 @@ import io.flutter.plugin.common.PluginRegistry.Registrar;
/** FluttertoastPlugin */ /** FluttertoastPlugin */
public class FluttertoastPlugin implements MethodCallHandler { public class FluttertoastPlugin implements MethodCallHandler {
Context ctx; private Context ctx;
private Toast toast = null;
FluttertoastPlugin(Context context) { private FluttertoastPlugin(Context context) {
ctx = context; ctx = context;
} }
...@@ -35,7 +36,23 @@ public class FluttertoastPlugin implements MethodCallHandler { ...@@ -35,7 +36,23 @@ public class FluttertoastPlugin implements MethodCallHandler {
@Override @Override
public void onMethodCall(MethodCall call, final Result result) { 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 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();
...@@ -44,33 +61,32 @@ public class FluttertoastPlugin implements MethodCallHandler { ...@@ -44,33 +61,32 @@ public class FluttertoastPlugin implements MethodCallHandler {
Number textSize = call.argument("fontSize"); Number textSize = call.argument("fontSize");
final Toast toast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT); toast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT);
//Added to see if
toast.setText(msg); toast.setText(msg);
if(length.equals("long")) { if(length.equals("long")) {
toast.setDuration(Toast.LENGTH_LONG); toast.setDuration(Toast.LENGTH_LONG);
} else { } else {
toast.setDuration(Toast.LENGTH_SHORT); toast.setDuration(Toast.LENGTH_SHORT);
} }
Boolean sent = false; // later
final Handler handler = new Handler(); // Boolean sent = false;
final Runnable run = new Runnable() { // final Handler handler = new Handler();
// final Runnable run = new Runnable() {
@Override //
public void run() { // @Override
try { // public void run() {
result.success(false); // try {
// result.success(false);
//
// } catch (Exception e){
// e.printStackTrace();
// }
// }
// };
} catch (Exception e){
e.printStackTrace();
}
}
};
switch (gravity) { switch (gravity) {
case "top": case "top":
...@@ -81,14 +97,14 @@ public class FluttertoastPlugin implements MethodCallHandler { ...@@ -81,14 +97,14 @@ public class FluttertoastPlugin implements MethodCallHandler {
break; break;
default: default:
toast.setGravity(Gravity.BOTTOM, 0, 100); toast.setGravity(Gravity.BOTTOM, 0, 100);
} }
final TextView text = toast.getView().findViewById(android.R.id.message); final TextView text = toast.getView().findViewById(android.R.id.message);
text.setTextSize(textSize.floatValue()); text.setTextSize(textSize.floatValue());
text.setMaxLines(1); 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) {
...@@ -101,33 +117,31 @@ public class FluttertoastPlugin implements MethodCallHandler { ...@@ -101,33 +117,31 @@ public class FluttertoastPlugin implements MethodCallHandler {
} }
} }
text.setOnTouchListener(new View.OnTouchListener() { //later
@Override // text.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) { // @Override
handler.removeCallbacks(run); // public boolean onTouch(View v, MotionEvent event) {
text.setOnTouchListener(null); // handler.removeCallbacks(run);
toast.cancel(); // text.setOnTouchListener(null);
try { // toast.cancel();
// try {
result.success(true); //
// result.success(true);
} catch (Exception e){ //
e.printStackTrace(); // } catch (Exception e){
} // e.printStackTrace();
// }
return false; //
} // 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(true);
// handler.postDelayed(run,toast.getDuration()*1000);
} else {
result.notImplemented();
}
} }
} }
...@@ -15,14 +15,14 @@ class _MyAppState extends State<MyApp> { ...@@ -15,14 +15,14 @@ class _MyAppState extends State<MyApp> {
} }
void showLongToast() { void showLongToast() {
Fluttertoast.instance.showToast( Fluttertoast.showToast(
msg: "This is Long Toast", msg: "This is Long Toast",
toastLength: Toast.LENGTH_LONG, toastLength: Toast.LENGTH_LONG,
); );
} }
void showColoredToast() { void showColoredToast() {
Fluttertoast.instance.showToast( Fluttertoast.showToast(
msg: "This is Colored Toast", msg: "This is Colored Toast",
toastLength: Toast.LENGTH_SHORT, toastLength: Toast.LENGTH_SHORT,
backgroundColor: Colors.red, backgroundColor: Colors.red,
...@@ -30,14 +30,14 @@ class _MyAppState extends State<MyApp> { ...@@ -30,14 +30,14 @@ class _MyAppState extends State<MyApp> {
} }
void showShortToast() { void showShortToast() {
Fluttertoast.instance.showToast( Fluttertoast.showToast(
msg: "This is Short Toast", msg: "This is Short Toast",
toastLength: Toast.LENGTH_SHORT, toastLength: Toast.LENGTH_SHORT,
timeInSecForIos: 1); timeInSecForIos: 1);
} }
void showTopShortToast() { void showTopShortToast() {
Fluttertoast.instance.showToast( Fluttertoast.showToast(
msg: "This is Top Short Toast", msg: "This is Top Short Toast",
toastLength: Toast.LENGTH_SHORT, toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.TOP, gravity: ToastGravity.TOP,
...@@ -45,13 +45,17 @@ class _MyAppState extends State<MyApp> { ...@@ -45,13 +45,17 @@ class _MyAppState extends State<MyApp> {
} }
void showCenterShortToast() { void showCenterShortToast() {
Fluttertoast.instance.showToast( 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);
} }
void cancelToast() {
Fluttertoast.cancel();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new MaterialApp( return new MaterialApp(
...@@ -92,6 +96,13 @@ class _MyAppState extends State<MyApp> { ...@@ -92,6 +96,13 @@ class _MyAppState extends State<MyApp> {
child: new Text('Show Colored Toast'), child: new Text('Show Colored Toast'),
onPressed: showColoredToast), onPressed: showColoredToast),
), ),
new Padding(
padding: const EdgeInsets.all(10.0),
child: new RaisedButton(
child: new Text('Cancel Toasts'),
onPressed: cancelToast,
),
),
], ],
), ),
), ),
......
#import "FluttertoastPlugin.h" #import "FluttertoastPlugin.h"
#import "UIView+Toast.h" #import "UIView+Toast.h"
// #import <fluttertoast/fluttertoast-Swift.h>
static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast"; static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast";
...@@ -37,7 +36,9 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast"; ...@@ -37,7 +36,9 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast";
} }
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result { - (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 *msg = call.arguments[@"msg"];
NSString *gravity = call.arguments[@"gravity"]; NSString *gravity = call.arguments[@"gravity"];
NSString *durationTime = call.arguments[@"time"]; NSString *durationTime = call.arguments[@"time"];
...@@ -68,35 +69,21 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast"; ...@@ -68,35 +69,21 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast";
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(true)
} else { } else {
result(FlutterMethodNotImplemented); result(FlutterMethodNotImplemented);
......
...@@ -13,22 +13,27 @@ class Fluttertoast { ...@@ -13,22 +13,27 @@ class Fluttertoast {
static const MethodChannel _channel = static const MethodChannel _channel =
const MethodChannel('PonnamKarthik/fluttertoast'); const MethodChannel('PonnamKarthik/fluttertoast');
static Fluttertoast _instance; // for Version 3.x.x
static Fluttertoast get instance { // static Fluttertoast _instance;
if (_instance == null) {
_instance =Fluttertoast._create();
}
return _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, @required String msg,
Toast toastLength, Toast toastLength,
int timeInSecForIos = 1, int timeInSecForIos = 1,
......
name: fluttertoast name: fluttertoast
description: Toast Library for FLutter description: Toast Library for FLutter
version: 2.2.7 version: 2.2.8
author: Karthik Ponnam <ponnamkarthik3@gmail.com> author: Karthik Ponnam <ponnamkarthik3@gmail.com>
homepage: https://github.com/PonnamKarthik/FlutterToast homepage: https://github.com/PonnamKarthik/FlutterToast
......
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