Commit 5081b259 by VJ Pranay Committed by GitHub

Merge pull request #2 from PonnamKarthik/master

Update fork
parents 42a42d3f 653e7dc3
## [2.2.1]
* default toast style fix #38
## [2.2.0]
* Background color fixed #29
## [2.1.5]
* Merged PR #36 - Fix Number Cast Error for issue #35
## [2.1.4]
* Merged PR #32
## [2.1.2]
* iOS Color Fix
* Background color fix in PIE
## [2.1.1] ## [2.1.1]
* Background color does not fill the whole Toast fixed * Background color does not fill the whole Toast fixed
......
...@@ -11,7 +11,7 @@ Android Toast Library for Flutter ...@@ -11,7 +11,7 @@ Android Toast Library for Flutter
```yaml ```yaml
# add this line to your dependencies # add this line to your dependencies
fluttertoast: ^2.1.1 fluttertoast: ^2.2.1
``` ```
```dart ```dart
...@@ -24,8 +24,8 @@ Fluttertoast.showToast( ...@@ -24,8 +24,8 @@ Fluttertoast.showToast(
toastLength: Toast.LENGTH_SHORT, toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER, gravity: ToastGravity.CENTER,
timeInSecForIos: 1, timeInSecForIos: 1,
bgcolor: "#e74c3c", backgroundColor: Colors.red,
textcolor: '#ffffff' textColor: Colors.white
); );
``` ```
...@@ -35,8 +35,8 @@ msg | String (Not Null)(required) ...@@ -35,8 +35,8 @@ msg | String (Not Null)(required)
toastLength| Toast.LENGTH_SHORT or Toast.LENGTH_LONG (optional) toastLength| Toast.LENGTH_SHORT or Toast.LENGTH_LONG (optional)
gravity | ToastGravity.TOP (or) ToastGravity.CENTER (or) ToastGravity.BOTTOM gravity | ToastGravity.TOP (or) ToastGravity.CENTER (or) ToastGravity.BOTTOM
timeInSecForIos | int (only for ios) timeInSecForIos | int (only for ios)
bgcolor | string (color in hex format) bgcolor | Colors.red
textcolor| '#ffffff' textcolor| Colors.white
## Preview Images ## Preview Images
......
...@@ -8,7 +8,7 @@ buildscript { ...@@ -8,7 +8,7 @@ buildscript {
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.1.2' classpath 'com.android.tools.build:gradle:3.2.1'
} }
} }
...@@ -32,3 +32,7 @@ android { ...@@ -32,3 +32,7 @@ android {
disable 'InvalidPackage' disable 'InvalidPackage'
} }
} }
dependencies {
implementation 'com.android.support:appcompat-v7:27.1.1'
}
package io.github.ponnamkarthik.toast.fluttertoast; package io.github.ponnamkarthik.toast.fluttertoast;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import android.content.Context; import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Paint; import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable; import android.os.Build;
import android.graphics.drawable.shapes.RoundRectShape; import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import android.util.Log;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
/** FluttertoastPlugin */ /** FluttertoastPlugin */
public class FluttertoastPlugin implements MethodCallHandler { public class FluttertoastPlugin implements MethodCallHandler {
Context ctx; Context ctx;
int defaultTextColor = Color.TRANSPARENT;
FluttertoastPlugin(Context context) { FluttertoastPlugin(Context context) {
ctx = context; ctx = context;
} }
...@@ -39,11 +39,14 @@ public class FluttertoastPlugin implements MethodCallHandler { ...@@ -39,11 +39,14 @@ public class FluttertoastPlugin implements MethodCallHandler {
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();
String bgcolor = call.argument("bgcolor").toString(); Number bgcolor = call.argument("bgcolor");
String textcolor = call.argument("textcolor").toString(); Number textcolor = call.argument("textcolor");
Toast toast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT); Toast toast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT);
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 {
...@@ -60,37 +63,25 @@ public class FluttertoastPlugin implements MethodCallHandler { ...@@ -60,37 +63,25 @@ public class FluttertoastPlugin implements MethodCallHandler {
default: default:
toast.setGravity(Gravity.BOTTOM, 0, 100); toast.setGravity(Gravity.BOTTOM, 0, 100);
} }
TextView text = toast.getView().findViewById(android.R.id.message);
if (defaultTextColor == 0) {
defaultTextColor = text.getCurrentTextColor();
}
if (!bgcolor.equals("null")) {
try {
RoundRectShape rectShape = new RoundRectShape(new float[] {100f, 100f, 100f, 100f, 100f, 100f, 100f, 100f}, null, null); TextView text = toast.getView().findViewById(android.R.id.message);
ShapeDrawable shapeDrawable = new ShapeDrawable(rectShape); if(bgcolor != null) {
shapeDrawable.getPaint().setColor(Color.parseColor(bgcolor)); Drawable shapeDrawable = ContextCompat.getDrawable(ctx, R.drawable.toast_bg);
shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
shapeDrawable.getPaint().setAntiAlias(true);
shapeDrawable.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
if (shapeDrawable != null) {
shapeDrawable.setColorFilter(bgcolor.intValue(), PorterDuff.Mode.SRC_IN);
if (Build.VERSION.SDK_INT <= 27) {
toast.getView().setBackground(shapeDrawable); toast.getView().setBackground(shapeDrawable);
// text.setBackground(shapeDrawable); } else {
} catch (Exception e) { text.setBackground(shapeDrawable);
e.printStackTrace();
} }
} }
if (!textcolor.equals("null")) {
try {
text.setTextColor(Color.parseColor(textcolor));
} catch (Exception e) {
e.printStackTrace();
} }
} else {
text.setTextColor(defaultTextColor); if(textcolor != null) {
text.setTextColor(textcolor.intValue());
} }
toast.show(); toast.show();
......
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners
android:radius="100dp">
</corners>
<solid
android:color="#000000">
</solid>
<padding
android:left="24dp"
android:top="15dp"
android:right="24dp"
android:bottom="15dp" />
</shape>
\ No newline at end of file
...@@ -8,7 +8,7 @@ buildscript { ...@@ -8,7 +8,7 @@ buildscript {
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.1.2' classpath 'com.android.tools.build:gradle:3.2.1'
} }
} }
......
#Fri Jun 23 08:50:38 CEST 2017 #Thu Dec 20 12:42:32 IST 2018
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
...@@ -25,8 +25,8 @@ class _MyAppState extends State<MyApp> { ...@@ -25,8 +25,8 @@ class _MyAppState extends State<MyApp> {
Fluttertoast.showToast( Fluttertoast.showToast(
msg: "This is Colored Toast", msg: "This is Colored Toast",
toastLength: Toast.LENGTH_SHORT, toastLength: Toast.LENGTH_SHORT,
bgcolor: "#e74c3c", backgroundColor: Colors.red,
textcolor: '#ffffff'); textColor: Colors.white);
} }
void showShortToast() { void showShortToast() {
......
...@@ -9,81 +9,65 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast"; ...@@ -9,81 +9,65 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast";
} }
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar { + (void)registerWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
FlutterMethodChannel* channel = [FlutterMethodChannel FlutterMethodChannel *channel = [FlutterMethodChannel
methodChannelWithName:CHANNEL_NAME methodChannelWithName:CHANNEL_NAME
binaryMessenger:[registrar messenger]]; binaryMessenger:[registrar messenger]];
UIViewController *viewController = UIViewController *viewController =
[UIApplication sharedApplication].delegate.window.rootViewController; [UIApplication sharedApplication].delegate.window.rootViewController;
FluttertoastPlugin* instance = [[FluttertoastPlugin alloc] init]; FluttertoastPlugin *instance = [[FluttertoastPlugin alloc] init];
[registrar addMethodCallDelegate:instance channel:channel]; [registrar addMethodCallDelegate:instance channel:channel];
} }
- (unsigned int)intFromHexString:(NSString *)hexStr { - (UIColor*) colorWithHex: (NSUInteger)hex {
unsigned int hexInt = 0; CGFloat red, green, blue, alpha;
NSScanner *scanner = [NSScanner scannerWithString:hexStr];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"#"]];
[scanner scanHexInt:&hexInt];
return hexInt;
}
- (UIColor *)getUIColorObjectFromHexString:(NSString *)hexStr alpha:(CGFloat)alpha
{
// Convert hex string to an integer
unsigned int hexint = [self intFromHexString:hexStr];
// Create color object, specifying alpha as well red = ((CGFloat)((hex >> 16) & 0xFF)) / ((CGFloat)0xFF);
UIColor *color = green = ((CGFloat)((hex >> 8) & 0xFF)) / ((CGFloat)0xFF);
[UIColor colorWithRed:((CGFloat) ((hexint & 0xFF0000) >> 16))/255 blue = ((CGFloat)((hex >> 0) & 0xFF)) / ((CGFloat)0xFF);
green:((CGFloat) ((hexint & 0xFF00) >> 8))/255 alpha = hex > 0xFFFFFF ? ((CGFloat)((hex >> 24) & 0xFF)) / ((CGFloat)0xFF) : 1;
blue:((CGFloat) (hexint & 0xFF))/255
alpha:alpha];
return color; return [UIColor colorWithRed: red green:green blue:blue alpha:alpha];
} }
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
if ([@"showToast" isEqualToString:call.method]) { 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"];
NSString *bgcolor = call.arguments[@"bgcolor"]; NSNumber *bgcolor = call.arguments[@"bgcolor"];
NSString *textcolor = call.arguments[@"textcolor"]; NSNumber *textcolor = call.arguments[@"textcolor"];
int time = 1; int time = 1;
@try{ @try {
time = [durationTime intValue]; time = [durationTime intValue];
} @catch(NSException *e){ } @catch (NSException *e) {
time = 3; time = 3;
} }
if(time > 10 ) time = 10; if (time > 10) time = 10;
else if(time < 1) time = 1; else if (time < 1) time = 1;
CSToastStyle *style = [[CSToastStyle alloc] initWithDefaultStyle]; CSToastStyle *style = [[CSToastStyle alloc] initWithDefaultStyle];
if(![bgcolor isEqualToString:@"null"]) {
style.self.backgroundColor = [self getUIColorObjectFromHexString:bgcolor alpha:1.0];
}
if(![textcolor isEqualToString:@"null"]) { style.backgroundColor = [self colorWithHex:bgcolor.unsignedIntegerValue];
style.messageColor = [self getUIColorObjectFromHexString:textcolor alpha:1.0]; style.messageColor = [self colorWithHex:textcolor.unsignedIntegerValue];
}
if([gravity isEqualToString:@"top"]) { if ([gravity isEqualToString:@"top"]) {
[[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];
} 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];
} 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];
} }
...@@ -94,31 +78,4 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast"; ...@@ -94,31 +78,4 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast";
} }
} }
// - (void)showToastView:(FlutterMethodCall*)call:(NSString*)msg {
// [[UIApplication sharedApplication].delegate.window.rootViewController.view makeToast:msg];
// }
// func hexStringToUIColor (hex:String) -> UIColor {
// var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
// if (cString.hasPrefix("#")) {
// cString.remove(at: cString.startIndex)
// }
// if ((cString.count) != 6) {
// return UIColor.gray
// }
// var rgbValue:UInt32 = 0
// Scanner(string: cString).scanHexInt32(&rgbValue)
// return UIColor(
// red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
// green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
// blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
// alpha: CGFloat(1.0)
// )
// }
@end @end
import 'dart:async'; import 'dart:async';
import 'dart:ui';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
enum Toast { LENGTH_SHORT, LENGTH_LONG }
enum Toast { enum ToastGravity { TOP, BOTTOM, CENTER }
LENGTH_SHORT,
LENGTH_LONG
}
enum ToastGravity {
TOP,
BOTTOM,
CENTER
}
class Fluttertoast { class Fluttertoast {
static const MethodChannel _channel = static const MethodChannel _channel =
const MethodChannel('PonnamKarthik/fluttertoast'); const MethodChannel('PonnamKarthik/fluttertoast');
static Future<String> showToast ({ static Future<String> showToast({
@required String msg, @required String msg,
Toast toastLength, Toast toastLength,
int timeInSecForIos = 1, int timeInSecForIos = 1,
ToastGravity gravity, ToastGravity gravity,
String bgcolor = "null", Color backgroundColor,
String textcolor = "null" Color textColor,
}) async { }) async {
String toast = "short"; String toast = "short";
if(toastLength == Toast.LENGTH_LONG) { if (toastLength == Toast.LENGTH_LONG) {
toast = "long"; toast = "long";
} }
String gravityToast = "bottom"; String gravityToast = "bottom";
if(gravity == ToastGravity.TOP) { if (gravity == ToastGravity.TOP) {
gravityToast = "top"; gravityToast = "top";
} else if(gravity == ToastGravity.CENTER) { } else if (gravity == ToastGravity.CENTER) {
gravityToast = "center"; gravityToast = "center";
} else { } else {
gravityToast = "bottom"; gravityToast = "bottom";
} }
final Map<String, dynamic> params = <String, dynamic> { final Map<String, dynamic> params = <String, dynamic>{
'msg': msg, 'msg': msg,
'length': toast, 'length': toast,
'time': timeInSecForIos, 'time': timeInSecForIos,
'gravity': gravityToast, 'gravity': gravityToast,
'bgcolor': bgcolor, 'bgcolor': backgroundColor != null ? backgroundColor.value : backgroundColor,
'textcolor': textcolor 'textcolor': textColor != null ? textColor.value: textColor,
}; };
String res = await _channel.invokeMethod('showToast', params); String res = await _channel.invokeMethod('showToast', params);
return res; return res;
} }
} }
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
// ignore_for_file: non_constant_identifier_names
// ignore_for_file: camel_case_types
// ignore_for_file: prefer_single_quotes
//This file is automatically generated. DO NOT EDIT, all your changes would be lost.
class S implements WidgetsLocalizations {
const S();
static const GeneratedLocalizationsDelegate delegate =
GeneratedLocalizationsDelegate();
static S of(BuildContext context) => Localizations.of<S>(context, S);
@override
TextDirection get textDirection => TextDirection.ltr;
}
class en extends S {
const en();
}
class GeneratedLocalizationsDelegate extends LocalizationsDelegate<S> {
const GeneratedLocalizationsDelegate();
List<Locale> get supportedLocales {
return const <Locale>[
Locale("en", ""),
];
}
LocaleListResolutionCallback listResolution({Locale fallback}) {
return (List<Locale> locales, Iterable<Locale> supported) {
if (locales == null || locales.isEmpty) {
return fallback ?? supported.first;
} else {
return _resolve(locales.first, fallback, supported);
}
};
}
LocaleResolutionCallback resolution({Locale fallback}) {
return (Locale locale, Iterable<Locale> supported) {
return _resolve(locale, fallback, supported);
};
}
Locale _resolve(Locale locale, Locale fallback, Iterable<Locale> supported) {
if (locale == null || !isSupported(locale)) {
return fallback ?? supported.first;
}
final Locale languageLocale = Locale(locale.languageCode, "");
if (supported.contains(locale)) {
return locale;
} else if (supported.contains(languageLocale)) {
return languageLocale;
} else {
final Locale fallbackLocale = fallback ?? supported.first;
return fallbackLocale;
}
}
@override
Future<S> load(Locale locale) {
final String lang = getLang(locale);
if (lang != null) {
switch (lang) {
case "en":
return SynchronousFuture<S>(const en());
default:
// NO-OP.
}
}
return SynchronousFuture<S>(const S());
}
@override
bool isSupported(Locale locale) =>
locale != null && supportedLocales.contains(locale);
@override
bool shouldReload(GeneratedLocalizationsDelegate old) => false;
}
String getLang(Locale l) => l == null
? null
: l.countryCode != null && l.countryCode.isEmpty
? l.languageCode
: l.toString();
name: fluttertoast name: fluttertoast
description: Toast Library for FLutter description: Toast Library for FLutter
version: 2.1.1 version: 2.2.1
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