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 {
...
@@ -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
'
}
}
}
}
...
...
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;
...
@@ -8,14 +8,11 @@ import io.flutter.plugin.common.PluginRegistry.Registrar;
import
android.content.Context
;
import
android.content.Context
;
import
android.graphics.Color
;
import
android.graphics.Color
;
import
android.graphics.Paint
;
import
android.graphics.Paint
;
import
android.graphics.drawable.ColorDrawable
;
import
android.graphics.drawable.ShapeDrawable
;
import
android.graphics.drawable.ShapeDrawable
;
import
android.graphics.drawable.shapes.RoundRectShape
;
import
android.graphics.drawable.shapes.RoundRectShape
;
import
android.os.Build
;
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
;
/** FluttertoastPlugin */
/** FluttertoastPlugin */
public
class
FluttertoastPlugin
implements
MethodCallHandler
{
public
class
FluttertoastPlugin
implements
MethodCallHandler
{
...
@@ -40,64 +37,61 @@ public class FluttertoastPlugin implements MethodCallHandler {
...
@@ -40,64 +37,61 @@ 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
(
);
Long
bgcolor
=
call
.
argument
(
"bgcolor"
);
String
textcolor
=
call
.
argument
(
"textcolor"
).
toString
(
);
Long
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
);
if
(
length
.
equals
(
"long"
))
{
toast
.
setDuration
(
Toast
.
LENGTH_LONG
);
}
else
{
toast
.
setDuration
(
Toast
.
LENGTH_SHORT
);
}
switch
(
gravity
)
{
toast
.
setText
(
msg
);
case
"top"
:
toast
.
setGravity
(
Gravity
.
TOP
,
0
,
100
);
if
(
length
.
equals
(
"long"
))
{
break
;
toast
.
setDuration
(
Toast
.
LENGTH_LONG
);
case
"center"
:
}
else
{
toast
.
setGravity
(
Gravity
.
CENTER
,
0
,
0
);
toast
.
setDuration
(
Toast
.
LENGTH_SHORT
);
break
;
}
default
:
toast
.
setGravity
(
Gravity
.
BOTTOM
,
0
,
100
);
switch
(
gravity
)
{
}
case
"top"
:
TextView
text
=
toast
.
getView
().
findViewById
(
android
.
R
.
id
.
message
);
toast
.
setGravity
(
Gravity
.
TOP
,
0
,
100
);
if
(
defaultTextColor
==
0
)
{
break
;
defaultTextColor
=
text
.
getCurrentTextColor
();
case
"center"
:
}
toast
.
setGravity
(
Gravity
.
CENTER
,
0
,
0
);
if
(!
bgcolor
.
equals
(
"null"
))
{
break
;
default
:
try
{
toast
.
setGravity
(
Gravity
.
BOTTOM
,
0
,
100
);
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
.
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"
);
result
.
success
(
"Success"
);
...
...
example/lib/main.dart
View file @
e457a4f2
...
@@ -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
,
b
gcolor:
"#e74c3c"
,
b
ackgroundColor:
Colors
.
red
,
text
color:
'#ffffff'
);
text
Color:
Colors
.
white
);
}
}
void
showShortToast
()
{
void
showShortToast
()
{
...
...
ios/Classes/FluttertoastPlugin.m
View file @
e457a4f2
...
@@ -9,116 +9,73 @@ static NSString *const CHANNEL_NAME = @"PonnamKarthik/fluttertoast";
...
@@ -9,116 +9,73 @@ 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
{
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
-
(
UIColor
*
)
colorWithHex
:
(
NSUInteger
)
hex
{
{
CGFloat
red
,
green
,
blue
,
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"
];
NS
String
*
bgcolor
=
call
.
arguments
[
@"bgcolor"
];
NS
Number
*
bgcolor
=
call
.
arguments
[
@"bgcolor"
];
NS
String
*
textcolor
=
call
.
arguments
[
@"textcolor"
];
NS
Number
*
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
];
}
}
result
(
@"done"
);
result
(
@"done"
);
}
else
{
}
else
{
result
(
FlutterMethodNotImplemented
);
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
@end
lib/fluttertoast.dart
View file @
e457a4f2
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
=
const
Color
.
fromARGB
(
255
,
0
,
0
,
0
)
,
String
textcolor
=
"null"
Color
textColor
=
const
Color
.
fromARGB
(
255
,
255
,
255
,
255
),
})
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'
:
b
gcolor
,
'bgcolor'
:
b
ackgroundColor
.
value
,
'textcolor'
:
text
color
'textcolor'
:
text
Color
.
value
,
};
};
String
res
=
await
_channel
.
invokeMethod
(
'showToast'
,
params
);
String
res
=
await
_channel
.
invokeMethod
(
'showToast'
,
params
);
return
res
;
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