Commit ecfaee70 by Ponnam Karthik

Added iOS Support

parent 4cd5b4d1
......@@ -2,6 +2,7 @@ package io.github.ponnamkarthik.toast.fluttertoast
import android.content.Context
import android.util.Log
import android.view.Gravity
import android.widget.Toast
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
......@@ -28,13 +29,26 @@ class FluttertoastPlugin(context: Context): MethodCallHandler {
if (call.method.equals("showToast")) {
val msg: String = call.argument("msg")
val length: String = call.argument("length")
val gravity: String = call.argument("gravity")
var toast: Toast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT);
if(length.equals("long")) {
Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show()
toast.duration = Toast.LENGTH_LONG
} else {
Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show()
toast.duration = Toast.LENGTH_SHORT
}
if(gravity.equals("top")) {
toast.setGravity(Gravity.TOP, 0, 100)
} else if(gravity.equals("center")) {
toast.setGravity(Gravity.CENTER, 0, 0)
} else {
toast.setGravity(Gravity.BOTTOM, 0, 100)
}
toast.show()
result.success("Success")
} else {
result.notImplemented()
......
......@@ -16,11 +16,31 @@ class _MyAppState extends State<MyApp> {
}
void showLongToast() {
Fluttertoast.showToast("This is Long Toast", Toast.LENGTH_LONG);
Fluttertoast.showToast(
msg: "This is Long Toast",
toastLength: Toast.LENGTH_LONG,
);
}
void showShortToast() {
Fluttertoast.showToast("This is Short Toast", Toast.LENGTH_SHORT);
Fluttertoast.showToast(
msg: "This is Long Toast",
toastLength: Toast.LENGTH_SHORT,
);
}
void showTopShortToast() {
Fluttertoast.showToast(
msg: "This is Top Long Toast",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.TOP
);
}
void showCenterShortToast() {
Fluttertoast.showToast(
msg: "This is Center Short Toast",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER
);
}
@override
......@@ -47,6 +67,20 @@ class _MyAppState extends State<MyApp> {
onPressed: showShortToast
),
),
new Padding(
padding: const EdgeInsets.all(10.0),
child: new RaisedButton(
child: new Text('Show Center Short Toast'),
onPressed: showCenterShortToast
),
),
new Padding(
padding: const EdgeInsets.all(10.0),
child: new RaisedButton(
child: new Text('Show Top Short Toast'),
onPressed: showTopShortToast
),
),
],
),
),
......
......@@ -2,15 +2,10 @@
#import "UIView+Toast.h"
// #import <fluttertoast/fluttertoast-Swift.h>
@interface QRCodeReaderPlugin()
@property (nonatomic, strong) UIView *toastview;
@property (nonatomic, retain) UIViewController *viewController;
@property (nonatomic, retain) UIViewController *toastViewController;
@end
static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast";
@implementation FluttertoastPlugin {
FlutterResult _result;
UIViewController *_viewController;
}
......@@ -18,44 +13,43 @@
FlutterMethodChannel* channel = [FlutterMethodChannel
methodChannelWithName:CHANNEL_NAME
binaryMessenger:[registrar messenger]];
// UIViewController *viewController = (UIViewController *)registrar.messenger;
UIViewController *viewController =
[UIApplication sharedApplication].delegate.window.rootViewController;
FluttertoastPlugin* instance = [[FluttertoastPlugin alloc] initWithViewController:viewController];
FluttertoastPlugin* instance = [[FluttertoastPlugin alloc] init];
[registrar addMethodCallDelegate:instance channel:channel];
}
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"showToast" isEqualToString:call.method]) {
[self showToastView:call];
_result = result;
NSString *msg = call.arguments[@"msg"];
NSString *gravity = call.arguments[@"gravity"];
NSString *durationTime = call.arguments[@"time"];
if([gravity isEqualToString:@"top"]) {
[[UIApplication sharedApplication].delegate.window.rootViewController.view makeToast:msg
duration: [durationTime intValue]
position:CSToastPositionTop];
} else if([gravity isEqualToString:@"center"]) {
[[UIApplication sharedApplication].delegate.window.rootViewController.view makeToast:msg
duration: [durationTime intValue]
position:CSToastPositionCenter];
} else {
result(FlutterMethodNotImplemented);
[[UIApplication sharedApplication].delegate.window.rootViewController.view makeToast:msg
duration: [durationTime intValue]
position:CSToastPositionBottom];
}
}
- (instancetype)initWithViewController:(UIViewController *)viewController {
self = [super init];
if (self) {
_viewController = viewController;
//_viewController.view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
_viewController.view.backgroundColor = [UIColor clearColor];
_viewController.view.opaque = NO;
[[ NSNotificationCenter defaultCenter]addObserver: self selector:@selector(rotate:)
name:UIDeviceOrientationDidChangeNotification object:nil];
_result = result;
} else {
result(FlutterMethodNotImplemented);
}
return self;
}
- (void)showToastView:(FlutterMethodCall*)call {
_toastViewController = [[UIViewController alloc] init];
[_viewController presentViewController:_toastViewController animated:NO completion:nil];
_toastview= [[UIView alloc] init];
_toastViewController.view = _toastview;
[self.view makeToast:@"This is a piece of toast."];
- (void)showToastView:(FlutterMethodCall*)call:(NSString*)msg {
[[UIApplication sharedApplication].delegate.window.rootViewController.view makeToast:msg];
}
@end
\ No newline at end of file
import 'package:flutter/services.dart';
import 'package:meta/meta.dart';
enum Toast {
......@@ -6,20 +7,43 @@ enum Toast {
LENGTH_LONG
}
enum ToastGravity {
TOP,
BOTTOM,
CENTER
}
class Fluttertoast {
static const MethodChannel _channel =
const MethodChannel('PonnamKarthik/fluttertoast');
static void showToast(String msg, Toast toastLength) {
static void showToast({
@required String msg,
Toast toastLength,
int timeInSecForIos,
ToastGravity gravity
}) {
String toast = "short";
if(toastLength == Toast.LENGTH_LONG) {
toast = "long";
}
String gravityToast = "bottom";
if(gravity == ToastGravity.TOP) {
gravityToast = "top";
} else if(gravity == ToastGravity.CENTER) {
gravityToast = "center";
} else {
gravityToast = "bottom";
}
final Map<String, dynamic> params = <String, dynamic> {
'msg': msg,
'length': toast
'length': toast,
'time': timeInSecForIos,
'gravity': gravityToast
};
_channel.invokeMethod('showToast', params);
}
......
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