Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
fluttertoast
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
songyanzhi
fluttertoast
Commits
e457a4f2
Unverified
Commit
e457a4f2
authored
Dec 08, 2018
by
Karthik Ponnam
Committed by
GitHub
Dec 08, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #32 from s2nventures/master
A few fixes and improvements
parents
da7609f5
1739d1a4
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
101 additions
and
161 deletions
+101
-161
android/build.gradle
+1
-1
android/src/main/java/io/github/ponnamkarthik/toast/fluttertoast/FluttertoastPlugin.java
+50
-56
example/lib/main.dart
+2
-2
ios/Classes/FluttertoastPlugin.m
+34
-77
lib/fluttertoast.dart
+14
-25
No files found.
android/build.gradle
View file @
e457a4f2
...
...
@@ -8,7 +8,7 @@ buildscript {
}
dependencies
{
classpath
'com.android.tools.build:gradle:3.
1.2
'
classpath
'com.android.tools.build:gradle:3.
2.1
'
}
}
...
...
android/src/main/java/io/github/ponnamkarthik/toast/fluttertoast/FluttertoastPlugin.java
View file @
e457a4f2
...
...
@@ -8,14 +8,11 @@ import io.flutter.plugin.common.PluginRegistry.Registrar;
import
android.content.Context
;
import
android.graphics.Color
;
import
android.graphics.Paint
;
import
android.graphics.drawable.ColorDrawable
;
import
android.graphics.drawable.ShapeDrawable
;
import
android.graphics.drawable.shapes.RoundRectShape
;
import
android.os.Build
;
import
android.view.Gravity
;
import
android.widget.TextView
;
import
android.widget.Toast
;
import
android.util.Log
;
/** FluttertoastPlugin */
public
class
FluttertoastPlugin
implements
MethodCallHandler
{
...
...
@@ -40,64 +37,61 @@ public class FluttertoastPlugin implements MethodCallHandler {
String
msg
=
call
.
argument
(
"msg"
).
toString
();
String
length
=
call
.
argument
(
"length"
).
toString
();
String
gravity
=
call
.
argument
(
"gravity"
).
toString
();
String
bgcolor
=
call
.
argument
(
"bgcolor"
).
toString
(
);
String
textcolor
=
call
.
argument
(
"textcolor"
).
toString
(
);
Long
bgcolor
=
call
.
argument
(
"bgcolor"
);
Long
textcolor
=
call
.
argument
(
"textcolor"
);
Toast
toast
=
Toast
.
makeText
(
ctx
,
msg
,
Toast
.
LENGTH_SHORT
);
toast
.
setText
(
msg
);
if
(
length
.
equals
(
"long"
))
{
toast
.
setDuration
(
Toast
.
LENGTH_LONG
);
}
else
{
toast
.
setDuration
(
Toast
.
LENGTH_SHORT
);
}
switch
(
gravity
)
{
case
"top"
:
toast
.
setGravity
(
Gravity
.
TOP
,
0
,
100
);
break
;
case
"center"
:
toast
.
setGravity
(
Gravity
.
CENTER
,
0
,
0
);
break
;
default
:
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
[]
{
100
f
,
100
f
,
100
f
,
100
f
,
100
f
,
100
f
,
100
f
,
100
f
},
null
,
null
);
ShapeDrawable
shapeDrawable
=
new
ShapeDrawable
(
rectShape
);
shapeDrawable
.
getPaint
().
setColor
(
Color
.
parseColor
(
bgcolor
));
shapeDrawable
.
getPaint
().
setStyle
(
Paint
.
Style
.
FILL
);
shapeDrawable
.
getPaint
().
setAntiAlias
(
true
);
shapeDrawable
.
getPaint
().
setFlags
(
Paint
.
ANTI_ALIAS_FLAG
);
if
(
android
.
os
.
Build
.
VERSION
.
SDK_INT
<=
27
)
{
toast
.
getView
().
setBackground
(
shapeDrawable
);
}
else
{
text
.
setBackground
(
shapeDrawable
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
if
(!
textcolor
.
equals
(
"null"
))
{
try
{
text
.
setTextColor
(
Color
.
parseColor
(
textcolor
));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
else
{
text
.
setTextColor
(
defaultTextColor
);
toast
.
setText
(
msg
);
if
(
length
.
equals
(
"long"
))
{
toast
.
setDuration
(
Toast
.
LENGTH_LONG
);
}
else
{
toast
.
setDuration
(
Toast
.
LENGTH_SHORT
);
}
switch
(
gravity
)
{
case
"top"
:
toast
.
setGravity
(
Gravity
.
TOP
,
0
,
100
);
break
;
case
"center"
:
toast
.
setGravity
(
Gravity
.
CENTER
,
0
,
0
);
break
;
default
:
toast
.
setGravity
(
Gravity
.
BOTTOM
,
0
,
100
);
}
toast
.
show
();
TextView
text
=
toast
.
getView
().
findViewById
(
android
.
R
.
id
.
message
);
if
(
defaultTextColor
==
0
)
{
defaultTextColor
=
text
.
getCurrentTextColor
();
}
try
{
RoundRectShape
rectShape
=
new
RoundRectShape
(
new
float
[]
{
100
f
,
100
f
,
100
f
,
100
f
,
100
f
,
100
f
,
100
f
,
100
f
},
null
,
null
);
ShapeDrawable
shapeDrawable
=
new
ShapeDrawable
(
rectShape
);
shapeDrawable
.
getPaint
().
setColor
(
bgcolor
!=
null
?
bgcolor
.
intValue
()
:
Color
.
BLACK
);
shapeDrawable
.
getPaint
().
setStyle
(
Paint
.
Style
.
FILL
);
shapeDrawable
.
getPaint
().
setAntiAlias
(
true
);
shapeDrawable
.
getPaint
().
setFlags
(
Paint
.
ANTI_ALIAS_FLAG
);
if
(
android
.
os
.
Build
.
VERSION
.
SDK_INT
<=
27
)
{
toast
.
getView
().
setBackground
(
shapeDrawable
);
}
else
{
text
.
setBackground
(
shapeDrawable
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
try
{
text
.
setTextColor
(
textcolor
!=
null
?
textcolor
.
intValue
()
:
defaultTextColor
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
toast
.
show
();
result
.
success
(
"Success"
);
...
...
example/lib/main.dart
View file @
e457a4f2
...
...
@@ -25,8 +25,8 @@ class _MyAppState extends State<MyApp> {
Fluttertoast
.
showToast
(
msg:
"This is Colored Toast"
,
toastLength:
Toast
.
LENGTH_SHORT
,
b
gcolor:
"#e74c3c"
,
text
color:
'#ffffff'
);
b
ackgroundColor:
Colors
.
red
,
text
Color:
Colors
.
white
);
}
void
showShortToast
()
{
...
...
ios/Classes/FluttertoastPlugin.m
View file @
e457a4f2
...
...
@@ -9,116 +9,73 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast";
}
+
(
void
)
registerWithRegistrar
:
(
NSObject
<
FlutterPluginRegistrar
>
*
)
registrar
{
FlutterMethodChannel
*
channel
=
[
FlutterMethodChannel
methodChannelWithName
:
CHANNEL_NAME
binaryMessenger
:[
registrar
messenger
]];
+
(
void
)
registerWithRegistrar
:
(
NSObject
<
FlutterPluginRegistrar
>
*
)
registrar
{
FlutterMethodChannel
*
channel
=
[
FlutterMethodChannel
methodChannelWithName
:
CHANNEL_NAME
binaryMessenger
:[
registrar
messenger
]];
UIViewController
*
viewController
=
[
UIApplication
sharedApplication
].
delegate
.
window
.
rootViewController
;
FluttertoastPlugin
*
instance
=
[[
FluttertoastPlugin
alloc
]
init
];
[
UIApplication
sharedApplication
].
delegate
.
window
.
rootViewController
;
FluttertoastPlugin
*
instance
=
[[
FluttertoastPlugin
alloc
]
init
];
[
registrar
addMethodCallDelegate
:
instance
channel
:
channel
];
}
-
(
unsigned
int
)
intFromHexString
:
(
NSString
*
)
hexStr
{
unsigned
int
hexInt
=
0
;
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
];
-
(
UIColor
*
)
colorWithHex
:
(
NSUInteger
)
hex
{
CGFloat
red
,
green
,
blue
,
alpha
;
// Create color object, specifying alpha as well
UIColor
*
color
=
[
UIColor
colorWithRed
:((
CGFloat
)
((
hexint
&
0xFF0000
)
>>
16
))
/
255
green
:((
CGFloat
)
((
hexint
&
0xFF00
)
>>
8
))
/
255
blue
:
((
CGFloat
)
(
hexint
&
0xFF
))
/
255
alpha
:
alpha
];
red
=
((
CGFloat
)((
hex
>>
16
)
&
0xFF
))
/
((
CGFloat
)
0xFF
);
green
=
((
CGFloat
)((
hex
>>
8
)
&
0xFF
))
/
((
CGFloat
)
0xFF
);
blue
=
((
CGFloat
)((
hex
>>
0
)
&
0xFF
))
/
((
CGFloat
)
0xFF
);
alpha
=
hex
>
0xFFFFFF
?
((
CGFloat
)((
hex
>>
24
)
&
0xFF
))
/
((
CGFloat
)
0xFF
)
:
1
;
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
])
{
NSString
*
msg
=
call
.
arguments
[
@"msg"
];
NSString
*
gravity
=
call
.
arguments
[
@"gravity"
];
NSString
*
durationTime
=
call
.
arguments
[
@"time"
];
NS
String
*
bgcolor
=
call
.
arguments
[
@"bgcolor"
];
NS
String
*
textcolor
=
call
.
arguments
[
@"textcolor"
];
NS
Number
*
bgcolor
=
call
.
arguments
[
@"bgcolor"
];
NS
Number
*
textcolor
=
call
.
arguments
[
@"textcolor"
];
int
time
=
1
;
@try
{
@try
{
time
=
[
durationTime
intValue
];
}
@catch
(
NSException
*
e
)
{
}
@catch
(
NSException
*
e
)
{
time
=
3
;
}
if
(
time
>
10
)
time
=
10
;
else
if
(
time
<
1
)
time
=
1
;
if
(
time
>
10
)
time
=
10
;
else
if
(
time
<
1
)
time
=
1
;
CSToastStyle
*
style
=
[[
CSToastStyle
alloc
]
initWithDefaultStyle
];
if
(
!
[
bgcolor
isEqualToString
:
@"null"
])
{
style
.
self
.
backgroundColor
=
[
self
getUIColorObjectFromHexString
:
bgcolor
alpha
:
1
.
0
];
}
if
(
!
[
textcolor
isEqualToString
:
@"null"
])
{
style
.
messageColor
=
[
self
getUIColorObjectFromHexString
:
textcolor
alpha
:
1
.
0
];
}
if
([
gravity
isEqualToString
:
@"top"
])
{
style
.
backgroundColor
=
[
self
colorWithHex
:
bgcolor
.
unsignedIntegerValue
];
style
.
messageColor
=
[
self
colorWithHex
:
textcolor
.
unsignedIntegerValue
];
if
([
gravity
isEqualToString
:
@"top"
])
{
[[
UIApplication
sharedApplication
].
delegate
.
window
.
rootViewController
.
view
makeToast
:
msg
duration
:
time
duration
:
time
position
:
CSToastPositionTop
style
:
style
];
}
else
if
([
gravity
isEqualToString
:
@"center"
])
{
style
:
style
];
}
else
if
([
gravity
isEqualToString
:
@"center"
])
{
[[
UIApplication
sharedApplication
].
delegate
.
window
.
rootViewController
.
view
makeToast
:
msg
duration
:
time
duration
:
time
position
:
CSToastPositionCenter
style
:
style
];
style
:
style
];
}
else
{
[[
UIApplication
sharedApplication
].
delegate
.
window
.
rootViewController
.
view
makeToast
:
msg
duration
:
time
duration
:
time
position
:
CSToastPositionBottom
style
:
style
];
style
:
style
];
}
result
(
@"done"
);
}
else
{
result
(
FlutterMethodNotImplemented
);
}
}
// - (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
lib/fluttertoast.dart
View file @
e457a4f2
import
'dart:async'
;
import
'dart:ui'
;
import
'package:flutter/services.dart'
;
import
'package:meta/meta.dart'
;
enum
Toast
{
LENGTH_SHORT
,
LENGTH_LONG
}
enum
Toast
{
LENGTH_SHORT
,
LENGTH_LONG
}
enum
ToastGravity
{
TOP
,
BOTTOM
,
CENTER
}
enum
ToastGravity
{
TOP
,
BOTTOM
,
CENTER
}
class
Fluttertoast
{
static
const
MethodChannel
_channel
=
const
MethodChannel
(
'PonnamKarthik/fluttertoast'
);
const
MethodChannel
(
'PonnamKarthik/fluttertoast'
);
static
Future
<
String
>
showToast
({
static
Future
<
String
>
showToast
({
@required
String
msg
,
Toast
toastLength
,
int
timeInSecForIos
=
1
,
ToastGravity
gravity
,
String
bgcolor
=
"null"
,
String
textcolor
=
"null"
Color
backgroundColor
=
const
Color
.
fromARGB
(
255
,
0
,
0
,
0
)
,
Color
textColor
=
const
Color
.
fromARGB
(
255
,
255
,
255
,
255
),
})
async
{
String
toast
=
"short"
;
if
(
toastLength
==
Toast
.
LENGTH_LONG
)
{
if
(
toastLength
==
Toast
.
LENGTH_LONG
)
{
toast
=
"long"
;
}
String
gravityToast
=
"bottom"
;
if
(
gravity
==
ToastGravity
.
TOP
)
{
if
(
gravity
==
ToastGravity
.
TOP
)
{
gravityToast
=
"top"
;
}
else
if
(
gravity
==
ToastGravity
.
CENTER
)
{
}
else
if
(
gravity
==
ToastGravity
.
CENTER
)
{
gravityToast
=
"center"
;
}
else
{
gravityToast
=
"bottom"
;
}
final
Map
<
String
,
dynamic
>
params
=
<
String
,
dynamic
>
{
final
Map
<
String
,
dynamic
>
params
=
<
String
,
dynamic
>{
'msg'
:
msg
,
'length'
:
toast
,
'time'
:
timeInSecForIos
,
'gravity'
:
gravityToast
,
'bgcolor'
:
b
gcolor
,
'textcolor'
:
text
color
'bgcolor'
:
b
ackgroundColor
.
value
,
'textcolor'
:
text
Color
.
value
,
};
String
res
=
await
_channel
.
invokeMethod
(
'showToast'
,
params
);
return
res
;
}
}
\ No newline at end of file
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment