oktoast
a pure dart toast library.
you can completely customize the style of toast.
versions
3.x.x
starting from the 3.x version, oktoast provides a null-safety version, the specific introduction of null-safety can be viewed in dart or flutter.
the 2.3.2 version is the last version that does not support null-safety.
about version 1.x.x
if you use oktoast 1.x.x ,please use the 1.x branch, and read version readme.
proposed migration to 2.x.x version. the new version does not require buildcontext.
and you can completely customize the style of toast, because now you can use showtoastwidget
.
usage
1. add library to your pubspec.yaml
dependencies:
oktoast: ^3.0.0 # such as version, you need use the latest version of pub.
2. import library in dart file
import 'package:oktoast/oktoast.dart';
3. wrap your app widget
oktoast(
/// set toast style, optional
child:materialapp()
);
tips:
if you happened error like: no mediaquery widget found
you can try to use next code to include oktoast
to your app.
materialapp(builder: (context, widget) {
return oktoast(
child: widget,
);
});
4. call method showtoast
showtoast("content");
// position and second have default value, is optional
showtoastwidget(text('hello oktoast'));
explain
there are two reasons why you need to wrap materialapp
- because this ensures that toast can be displayed in front of all other controls
- context can be cached so that it can be invoked anywhere without passing in context
properties
oktoast params
oktoast have default style, and you also can custom style or other behavior.
name | type | need | desc |
---|---|---|---|
child | widget | required | usually material app |
textstyle | textstyle | optional | |
radius | double | optional | |
backgroundcolor | color | optional | backroundcolor |
position | toastposition | optional | |
dismissotheronshow | bool | optional | if true, other toasts will be dismissed. default false. |
movingonwindowchange | bool | optional | if true, when the size changes, toast is moved. default true. |
textdirection | textdirection | optional | |
textpadding | edgeinsetsgeometry | optional | outer margin of text |
textalign | textalign | optional | when the text wraps, the align of the text. |
handletouch | bool | optional | default is false, if it’s true, can responed use touch event. |
animationbuilder | oktoastanimationbuilder | optional | add animation to show / hide toast. |
animationduration | duration | optional | the duration of animation. |
animationcurve | curve | optional | curve of animation. |
duration | duration | optional | default duration of toast. |
method showtoast
display text on toast.
description of params see oktoast
.
name | type | need | desc |
---|---|---|---|
msg | string | required | text of toast. |
context | buildcontext | optional | |
duration | duration | optional | |
position | toastposition | optional | |
textstyle | textstyle | optional | |
textpadding | edgeinsetsgeometry | optional | |
backgroundcolor | color | optional | |
radius | double | optional | |
ondismiss | function | optional | |
textdirection | textdirection | optional | |
dismissothertoast | bool | optional | |
textalign | textalign | optional | |
animationbuilder | oktoastanimationbuilder | optional | |
animationduration | duration | optional | |
animationcurve | curve | optional |
method showtoastwidget
display custom widgets on toast
description of params see showtoast
.
name | type | need | desc |
---|---|---|---|
widget | widget | required | the widget you want to display. |
context | buildcontext | optional | |
duration | duration | optional | |
position | toastposition | optional | |
ondismiss | function | optional | |
dismissothertoast | bool | optional | |
textdirection | textdirection | optional | |
handletouch | bool | optional | |
animationbuilder | oktoastanimationbuilder | optional | |
animationduration | duration | optional | |
animationcurve | curve | optional |
method dismissalltoast
dismiss all toast.
return value of showtoast
and showtoastwidget
about return type:
showtoast
and showtoastwidget
return type is toastfuture
,
the toastfuture
can be use to dismiss the toast.
for all dismiss toast method
an optional parameter showanim
is added to control whether fading animation is required for dismiss.
the praram default value is false
.
examples
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart'; // 1. import library
void main() => runapp( myapp());
class myapp extends statelesswidget {
@override
widget build(buildcontext context) {
return oktoast(
//2. wrap your app with oktoast
child: materialapp(
title: 'flutter demo',
theme: themedata(
primaryswatch: colors.blue,
),
home: myhomepage(),
),
);
}
}
class myhomepage extends statefulwidget {
myhomepage({key key}) : super(key: key);
@override
_myhomepagestate createstate() => _myhomepagestate();
}
class _myhomepagestate extends state<myhomepage> {
int _counter = 0;
void _incrementcounter() {
_counter++;
// 3.1 use showtoast method
showtoast(
"$_counter",
duration: duration(seconds: 2),
position: toastposition.bottom,
backgroundcolor: colors.black.withopacity(0.8),
radius: 13.0,
textstyle: textstyle(fontsize: 18.0),
);
showtoast(
"$_counter",
duration: duration(seconds: 2),
position: toastposition.top,
backgroundcolor: colors.black.withopacity(0.8),
radius: 3.0,
textstyle: textstyle(fontsize: 30.0),
);
// 3.2 use showtoastwidget method to custom widget
widget widget = center(
child: cliprrect(
borderradius: borderradius.circular(30.0),
child: container(
width: 40.0,
height: 40.0,
color: colors.grey.withopacity(0.3),
child: icon(
icons.add,
size: 30.0,
color: colors.green,
),
),
),
);
toastfuture toastfuture = showtoastwidget(
widget,
duration: duration(seconds: 3),
ondismiss: () {
print("the toast dismiss"); // the method will be called on toast dismiss.
},
);
// can use future
future.delayed(duration(seconds: 1), () {
toastfuture.dismiss(); // dismiss
});
setstate(() {
});
}
@override
widget build(buildcontext context) {
return scaffold(
appbar: appbar(
title: text("ktoast demo"),
),
body: stack(
children: <widget>[
center(
child: listview(
children: <widget>[
text(
'you have pushed the button this many times:',
),
text(
'$_counter',
style: theme.of(context).texttheme.display1,
),
padding(
padding: const edgeinsets.all(8.0),
child: raisedbutton(
onpressed: () {
navigator.push(context,
materialpageroute(builder: (ctx) => myhomepage()));
},
),
),
padding(
padding: const edgeinsets.all(8.0),
child: raisedbutton(
onpressed: _incrementcounter,
child: text('toast'),
),
),
],
),
),
],
),
);
}
}
Comments are closed.