Commit bf984e85 by Karthik

2.2.8

parent f5904128
## [2.2.8]
* `Fluttertoast.cancel()` added
* FlutterToast Implementation revert back to previous
## [2.2.7]
* FontSize Can be changed
......
......@@ -13,7 +13,7 @@ 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
......@@ -21,7 +21,7 @@ import 'package:fluttertoast/fluttertoast.dart';
```
```dart
Fluttertoast.instance.showToast(
Fluttertoast.showToast(
msg: "This is Center Short Toast",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
......@@ -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" />
......
......@@ -21,9 +21,10 @@ import io.flutter.plugin.common.PluginRegistry.Registrar;
/** FluttertoastPlugin */
public class FluttertoastPlugin implements MethodCallHandler {
Context ctx;
private Context ctx;
private Toast toast = null;
FluttertoastPlugin(Context context) {
private FluttertoastPlugin(Context context) {
ctx = context;
}
......@@ -35,7 +36,23 @@ public class FluttertoastPlugin implements MethodCallHandler {
@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();
......@@ -44,33 +61,32 @@ public class FluttertoastPlugin implements MethodCallHandler {
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":
......@@ -81,14 +97,14 @@ public class FluttertoastPlugin implements MethodCallHandler {
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) {
......@@ -101,33 +117,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;
}
});
//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);
}
}
......@@ -15,14 +15,14 @@ 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,
......@@ -30,14 +30,14 @@ class _MyAppState extends State<MyApp> {
}
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,
......@@ -45,13 +45,17 @@ class _MyAppState extends State<MyApp> {
}
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(
......@@ -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,
),
),
],
),
),
......
#import "FluttertoastPlugin.h"
#import "UIView+Toast.h"
// #import <fluttertoast/fluttertoast-Swift.h>
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 {
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"];
......@@ -68,35 +69,21 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast";
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);
......
......@@ -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,
......
name: fluttertoast
description: Toast Library for FLutter
version: 2.2.7
version: 2.2.8
author: Karthik Ponnam <ponnamkarthik3@gmail.com>
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