Skip to content

Commit c6256c5

Browse files
authored
Merge pull request #43 from OpenFlutter/Default-duration
Add duration param to OKToast.
2 parents a66273a + d9bda09 commit c6256c5

File tree

5 files changed

+20
-14
lines changed

5 files changed

+20
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ oktoast have default style, and you also can custom style or other behavior.
9898
| animationBuilder | OKToastAnimationBuilder | optional | Add animation to show / hide toast. |
9999
| animationDuration | Duration | optional | The duration of animation. |
100100
| animationCurve | Curve | optional | Curve of animation. |
101+
| duration | Duration | optional | Default duration of toast. |
101102

102103
### Method `showToast`
103104

example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class MyApp extends StatelessWidget {
2323
animationCurve: Curves.easeIn,
2424
animationBuilder: Miui10AnimBuilder(),
2525
animationDuration: Duration(milliseconds: 200),
26+
duration: Duration(seconds: 2, milliseconds: 500),
2627
);
2728
}
2829
}
@@ -47,7 +48,6 @@ class _MyHomePageState extends State<MyHomePage> {
4748
// 3.1 use showToast method
4849
showToast(
4950
"$_counter",
50-
duration: Duration(seconds: 2),
5151
position: ToastPosition.bottom,
5252
backgroundColor: Colors.black.withOpacity(0.8),
5353
radius: 13.0,

lib/src/core/toast.dart

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const _defaultDuration = Duration(
2727
ToastFuture showToast(
2828
String msg, {
2929
BuildContext context,
30-
Duration duration = _defaultDuration,
30+
Duration duration,
3131
ToastPosition position,
3232
TextStyle textStyle,
3333
EdgeInsetsGeometry textPadding,
@@ -43,17 +43,14 @@ ToastFuture showToast(
4343
}) {
4444
context ??= _contextMap.values.first;
4545

46-
textStyle ??= _ToastTheme.of(context).textStyle ?? TextStyle(fontSize: 15.0);
47-
48-
textAlign ??= _ToastTheme.of(context).textAlign;
49-
50-
textPadding ??= _ToastTheme.of(context).textPadding;
51-
52-
position ??= _ToastTheme.of(context).position;
53-
backgroundColor ??= _ToastTheme.of(context).backgroundColor;
54-
radius ??= _ToastTheme.of(context).radius;
55-
56-
textDirection ??= _ToastTheme.of(context).textDirection ?? TextDirection.ltr;
46+
final theme = _ToastTheme.of(context);
47+
textStyle ??= theme.textStyle ?? TextStyle(fontSize: 15.0);
48+
textAlign ??= theme.textAlign;
49+
textPadding ??= theme.textPadding;
50+
position ??= theme.position;
51+
backgroundColor ??= theme.backgroundColor;
52+
radius ??= theme.radius;
53+
textDirection ??= theme.textDirection ?? TextDirection.ltr;
5754

5855
Widget widget = Container(
5956
margin: const EdgeInsets.all(50.0),
@@ -88,7 +85,7 @@ ToastFuture showToast(
8885
ToastFuture showToastWidget(
8986
Widget widget, {
9087
BuildContext context,
91-
Duration duration = _defaultDuration,
88+
Duration duration,
9289
ToastPosition position,
9390
VoidCallback onDismiss,
9491
bool dismissOtherToast,
@@ -109,6 +106,7 @@ ToastFuture showToastWidget(
109106
animationDuration ??=
110107
theme.animationDuration ?? const Duration(milliseconds: 250);
111108
animationCurve ??= theme.animationCurve ?? Curves.easeIn;
109+
duration ??= theme.duration ?? _defaultDuration;
112110

113111
final movingOnWindowChange = theme?.movingOnWindowChange ?? false;
114112

lib/src/widget/oktoast.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class OKToast extends StatefulWidget {
3434
/// Whether toast can respond to click events.
3535
final bool handleTouth;
3636

37+
final Duration duration;
38+
3739
/// The animation builder of show/hide toast.
3840
final OKToastAnimationBuilder animationBuilder;
3941

@@ -59,6 +61,7 @@ class OKToast extends StatefulWidget {
5961
this.animationBuilder,
6062
this.animationDuration = const Duration(milliseconds: 250),
6163
this.animationCurve,
64+
this.duration,
6265
}) : this.backgroundColor = backgroundColor ?? const Color(0xDD000000),
6366
super(key: key);
6467

@@ -130,6 +133,7 @@ class _OKToastState extends State<OKToast> {
130133
animationBuilder: widget.animationBuilder,
131134
animationDuration: widget.animationDuration,
132135
animationCurve: widget.animationCurve,
136+
duration: widget.duration,
133137
);
134138
}
135139
}

lib/src/widget/theme.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class _ToastTheme extends InheritedWidget {
2727

2828
final Curve animationCurve;
2929

30+
final Duration duration;
31+
3032
@override
3133
bool updateShouldNotify(InheritedWidget oldWidget) => true;
3234

@@ -45,6 +47,7 @@ class _ToastTheme extends InheritedWidget {
4547
this.animationBuilder,
4648
this.animationDuration,
4749
this.animationCurve,
50+
this.duration,
4851
}) : textDirection = textDirection ?? TextDirection.ltr,
4952
super(child: child);
5053

0 commit comments

Comments
 (0)