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
a930451c
Commit
a930451c
authored
Sep 05, 2019
by
JosephusZhou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimized the custom layout of Android Toast, using GradientDrawable to set the style.
parent
89ee8941
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
92 deletions
+68
-92
android/src/main/java/io/github/ponnamkarthik/toast/fluttertoast/FluttertoastPlugin.java
+49
-78
android/src/main/res/layout/toast_custom.xml
+19
-14
No files found.
android/src/main/java/io/github/ponnamkarthik/toast/fluttertoast/FluttertoastPlugin.java
View file @
a930451c
package
io
.
github
.
ponnamkarthik
.
toast
.
fluttertoast
;
package
io
.
github
.
ponnamkarthik
.
toast
.
fluttertoast
;
import
android.content.Context
;
import
android.content.Context
;
import
android.graphics.PorterDuff
;
import
android.graphics.drawable.GradientDrawable
;
import
android.graphics.drawable.Drawable
;
import
android.view.Gravity
;
import
android.view.Gravity
;
import
android.view.LayoutInflater
;
import
android.view.LayoutInflater
;
import
android.view.View
;
import
android.view.View
;
...
@@ -16,17 +14,21 @@ import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
...
@@ -16,17 +14,21 @@ import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import
io.flutter.plugin.common.MethodChannel.Result
;
import
io.flutter.plugin.common.MethodChannel.Result
;
import
io.flutter.plugin.common.PluginRegistry.Registrar
;
import
io.flutter.plugin.common.PluginRegistry.Registrar
;
/** FluttertoastPlugin */
/**
* FluttertoastPlugin
*/
public
class
FluttertoastPlugin
implements
MethodCallHandler
{
public
class
FluttertoastPlugin
implements
MethodCallHandler
{
private
Context
ctx
;
private
Context
mContext
;
private
Toast
t
oast
;
private
Toast
mT
oast
;
private
FluttertoastPlugin
(
Context
context
)
{
private
FluttertoastPlugin
(
Context
context
)
{
ctx
=
context
;
mContext
=
context
;
}
}
/** Plugin registration. */
/**
* Plugin registration.
*/
public
static
void
registerWith
(
Registrar
registrar
)
{
public
static
void
registerWith
(
Registrar
registrar
)
{
final
MethodChannel
channel
=
new
MethodChannel
(
registrar
.
messenger
(),
"PonnamKarthik/fluttertoast"
);
final
MethodChannel
channel
=
new
MethodChannel
(
registrar
.
messenger
(),
"PonnamKarthik/fluttertoast"
);
channel
.
setMethodCallHandler
(
new
FluttertoastPlugin
(
registrar
.
context
()));
channel
.
setMethodCallHandler
(
new
FluttertoastPlugin
(
registrar
.
context
()));
...
@@ -36,104 +38,68 @@ public class FluttertoastPlugin implements MethodCallHandler {
...
@@ -36,104 +38,68 @@ public class FluttertoastPlugin implements MethodCallHandler {
public
void
onMethodCall
(
MethodCall
call
,
final
Result
result
)
{
public
void
onMethodCall
(
MethodCall
call
,
final
Result
result
)
{
switch
(
call
.
method
)
{
switch
(
call
.
method
)
{
case
"showToast"
:
case
"showToast"
:
String
m
sg
=
call
.
argument
(
"msg"
).
toString
();
String
m
Message
=
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
();
Number
bgcolor
=
call
.
argument
(
"bgcolor"
);
Number
bgcolor
=
call
.
argument
(
"bgcolor"
);
Number
textcolor
=
call
.
argument
(
"textcolor"
);
Number
textcolor
=
call
.
argument
(
"textcolor"
);
Number
textSize
=
call
.
argument
(
"fontSize"
);
Number
textSize
=
call
.
argument
(
"fontSize"
);
Integer
real
Gravity
;
int
m
Gravity
;
switch
(
gravity
)
{
switch
(
gravity
)
{
case
"top"
:
case
"top"
:
real
Gravity
=
Gravity
.
TOP
;
m
Gravity
=
Gravity
.
TOP
;
break
;
break
;
case
"center"
:
case
"center"
:
real
Gravity
=
Gravity
.
CENTER
;
m
Gravity
=
Gravity
.
CENTER
;
break
;
break
;
default
:
default
:
realGravity
=
Gravity
.
BOTTOM
;
mGravity
=
Gravity
.
BOTTOM
;
break
;
}
}
int
mDuration
;
if
(
bgcolor
!=
null
&&
textcolor
!=
null
&&
textSize
!=
null
)
{
LayoutInflater
inflater
=
(
LayoutInflater
)
ctx
.
getSystemService
(
Context
.
LAYOUT_INFLATER_SERVICE
);
View
layout
=
inflater
.
inflate
(
R
.
layout
.
toast_custom
,
null
);
TextView
text
=
layout
.
findViewById
(
R
.
id
.
text
);
toast
=
new
Toast
(
ctx
);
if
(
length
.
equals
(
"long"
))
{
if
(
length
.
equals
(
"long"
))
{
toast
.
setDuration
(
Toast
.
LENGTH_LONG
)
;
mDuration
=
Toast
.
LENGTH_LONG
;
}
else
{
}
else
{
toast
.
setDuration
(
Toast
.
LENGTH_SHORT
)
;
mDuration
=
Toast
.
LENGTH_SHORT
;
}
}
text
.
setText
(
msg
);
if
(
bgcolor
!=
null
&&
textcolor
!=
null
&&
textSize
!=
null
)
{
LayoutInflater
inflater
=
(
LayoutInflater
)
mContext
.
getSystemService
(
Context
.
LAYOUT_INFLATER_SERVICE
);
toast
.
setView
(
layout
);
View
layout
=
inflater
.
inflate
(
R
.
layout
.
toast_custom
,
null
);
TextView
text
=
layout
.
findViewById
(
R
.
id
.
text
);
text
.
setText
(
mMessage
);
if
(
textSize
!=
null
)
// Custom style
GradientDrawable
gradientDrawable
=
new
GradientDrawable
();
gradientDrawable
.
setColor
(
bgcolor
.
intValue
());
gradientDrawable
.
setCornerRadius
(
dp2px
(
4.0f
));
text
.
setBackground
(
gradientDrawable
);
text
.
setTextSize
(
textSize
.
floatValue
());
text
.
setTextSize
(
textSize
.
floatValue
());
Drawable
shapeDrawable
;
// shapeDrawable = ctx.getDrawable(ctx.getResources(), R.drawable.toast_bg, null);
shapeDrawable
=
ctx
.
getResources
().
getDrawable
(
R
.
drawable
.
toast_bg
);
if
(
bgcolor
!=
null
&&
shapeDrawable
!=
null
)
{
shapeDrawable
.
setColorFilter
(
bgcolor
.
intValue
(),
PorterDuff
.
Mode
.
SRC_IN
);
}
layout
.
setBackground
(
shapeDrawable
);
// if(bgcolor != null) {
// Drawable shapeDrawable;
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// shapeDrawable = ctx.getResources().getDrawable(R.drawable.toast_bg,ctx.getTheme());
// } else {
// shapeDrawable = ctx.getResources().getDrawable(R.drawable.toast_bg);
// }
//
// if (shapeDrawable != null) {
// shapeDrawable.setColorFilter(bgcolor.intValue(), PorterDuff.Mode.SRC_IN);
// if (Build.VERSION.SDK_INT <= 27) {
// toast.getView().setBackground(shapeDrawable);
// } else {
// toast.getView().setBackground(null);
// text.setBackground(shapeDrawable);
// }
// }
// }
if
(
textcolor
!=
null
)
{
text
.
setTextColor
(
textcolor
.
intValue
());
text
.
setTextColor
(
textcolor
.
intValue
());
}
toast
.
setGravity
(
realGravity
,
0
,
0
);
mToast
=
new
Toast
(
mContext
);
toast
.
show
();
mToast
.
setDuration
(
mDuration
);
}
else
{
mToast
.
setView
(
layout
);
int
toastLength
;
if
(
length
.
equals
(
"long"
))
{
toastLength
=
Toast
.
LENGTH_LONG
;
}
else
{
}
else
{
toastLength
=
Toast
.
LENGTH_SHORT
;
mToast
=
Toast
.
makeText
(
mContext
,
mMessage
,
mDuration
);
}
Toast
toast
=
Toast
.
makeText
(
ctx
,
msg
,
toastLength
);
toast
.
setGravity
(
realGravity
,
0
,
0
);
toast
.
show
();
}
}
if
(
mGravity
==
Gravity
.
CENTER
)
{
mToast
.
setGravity
(
mGravity
,
0
,
0
);
}
else
if
(
mGravity
==
Gravity
.
TOP
)
{
mToast
.
setGravity
(
mGravity
,
0
,
100
);
}
else
{
mToast
.
setGravity
(
mGravity
,
0
,
100
);
}
mToast
.
show
();
result
.
success
(
true
);
result
.
success
(
true
);
break
;
break
;
case
"cancel"
:
case
"cancel"
:
if
(
toast
!=
null
)
if
(
mToast
!=
null
)
{
toast
.
cancel
();
mToast
.
cancel
();
}
result
.
success
(
true
);
result
.
success
(
true
);
break
;
break
;
default
:
default
:
...
@@ -141,4 +107,9 @@ public class FluttertoastPlugin implements MethodCallHandler {
...
@@ -141,4 +107,9 @@ public class FluttertoastPlugin implements MethodCallHandler {
break
;
break
;
}
}
}
}
private
float
dp2px
(
float
dp
)
{
final
float
scale
=
mContext
.
getResources
().
getDisplayMetrics
().
density
;
return
dp
*
scale
+
0.5f
;
}
}
}
android/src/main/res/layout/toast_custom.xml
View file @
a930451c
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"
<FrameLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"
android:id=
"@+id/custom_toast_container"
xmlns:tools=
"http://schemas.android.com/tools"
android:orientation=
"horizontal"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
android:padding=
"8dp"
android:background=
"#DAAA"
>
<TextView
android:id=
"@+id/text"
android:layout_width=
"wrap_content"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_height=
"wrap_content"
android:textColor=
"#FFF"
android:layout_gravity=
"center_horizontal"
android:singleLine=
"false"
android:layout_marginStart=
"50dp"
/>
android:layout_marginEnd=
"50dp"
>
</LinearLayout>
\ No newline at end of file
<TextView
android:id=
"@+id/text"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:background=
"#CC000000"
android:paddingStart=
"16dp"
android:paddingTop=
"10dp"
android:paddingEnd=
"16dp"
android:paddingBottom=
"10dp"
android:textColor=
"#FFFFFF"
tools:text=
"Toast should be short."
/>
</FrameLayout>
\ 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