Commit d42677ec by yuchan2215 Committed by GitHub

Fix fadeDuration (#390)

* Fix fadeDuration

* format

* Fix redundant code
parent 08fd963f
import 'dart:async'; import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
/// Toast Length /// Toast Length
...@@ -132,6 +132,7 @@ class FToast { ...@@ -132,6 +132,7 @@ class FToast {
OverlayEntry? _entry; OverlayEntry? _entry;
List<_ToastEntry> _overlayQueue = []; List<_ToastEntry> _overlayQueue = [];
Timer? _timer; Timer? _timer;
Timer? _fadeTimer;
/// Internal function which handles the adding /// Internal function which handles the adding
/// the overlay to the screen /// the overlay to the screen
...@@ -147,8 +148,8 @@ class FToast { ...@@ -147,8 +148,8 @@ class FToast {
throw ("Error: Context is null, Please call init(context) before showing toast."); throw ("Error: Context is null, Please call init(context) before showing toast.");
Overlay.of(context!)?.insert(_entry!); Overlay.of(context!)?.insert(_entry!);
_timer = Timer(_toastEntry.duration!, () { _timer = Timer(_toastEntry.duration, () {
Future.delayed(Duration(milliseconds: 360), () { _fadeTimer = Timer(_toastEntry.fadeDuration, () {
removeCustomToast(); removeCustomToast();
}); });
}); });
...@@ -158,7 +159,9 @@ class FToast { ...@@ -158,7 +159,9 @@ class FToast {
/// call removeCustomToast to hide the toast immediately /// call removeCustomToast to hide the toast immediately
removeCustomToast() { removeCustomToast() {
_timer?.cancel(); _timer?.cancel();
_fadeTimer?.cancel();
_timer = null; _timer = null;
_fadeTimer = null;
if (_entry != null) _entry!.remove(); if (_entry != null) _entry!.remove();
_entry = null; _entry = null;
_showOverlay(); _showOverlay();
...@@ -171,7 +174,9 @@ class FToast { ...@@ -171,7 +174,9 @@ class FToast {
/// call removeCustomToast to hide the toast immediately /// call removeCustomToast to hide the toast immediately
removeQueuedCustomToasts() { removeQueuedCustomToasts() {
_timer?.cancel(); _timer?.cancel();
_fadeTimer?.cancel();
_timer = null; _timer = null;
_fadeTimer = null;
_overlayQueue.clear(); _overlayQueue.clear();
if (_entry != null) _entry!.remove(); if (_entry != null) _entry!.remove();
_entry = null; _entry = null;
...@@ -181,19 +186,18 @@ class FToast { ...@@ -181,19 +186,18 @@ class FToast {
/// calls _showOverlay to display toast /// calls _showOverlay to display toast
/// ///
/// Paramenter [child] is requried /// Paramenter [child] is requried
/// toastDuration default is 2 seconds
/// fadeDuration default is 350 milliseconds /// fadeDuration default is 350 milliseconds
void showToast({ void showToast({
required Widget child, required Widget child,
PositionedToastBuilder? positionedToastBuilder, PositionedToastBuilder? positionedToastBuilder,
Duration? toastDuration, Duration toastDuration = const Duration(seconds: 2),
ToastGravity? gravity, ToastGravity? gravity,
int fadeDuration = 350, Duration fadeDuration = const Duration(milliseconds: 350),
}) { }) {
if (context == null) if (context == null)
throw ("Error: Context is null, Please call init(context) before showing toast."); throw ("Error: Context is null, Please call init(context) before showing toast.");
Widget newChild = _ToastStateFul( Widget newChild = _ToastStateFul(child, toastDuration, fadeDuration);
child, toastDuration ?? Duration(seconds: 2),
fadeDuration: fadeDuration);
/// Check for keyboard open /// Check for keyboard open
/// If open will ignore the gravity bottom and change it to center /// If open will ignore the gravity bottom and change it to center
...@@ -208,9 +212,8 @@ class FToast { ...@@ -208,9 +212,8 @@ class FToast {
return positionedToastBuilder(context, newChild); return positionedToastBuilder(context, newChild);
return _getPostionWidgetBasedOnGravity(newChild, gravity); return _getPostionWidgetBasedOnGravity(newChild, gravity);
}); });
_overlayQueue.add(_ToastEntry( _overlayQueue.add(_ToastEntry(
entry: newEntry, duration: toastDuration ?? Duration(seconds: 2))); entry: newEntry, duration: toastDuration, fadeDuration: fadeDuration));
if (_timer == null) _showOverlay(); if (_timer == null) _showOverlay();
} }
...@@ -253,21 +256,26 @@ class FToast { ...@@ -253,21 +256,26 @@ class FToast {
/// each [OverlayEntry] and [Duration] for every toast user /// each [OverlayEntry] and [Duration] for every toast user
/// triggered /// triggered
class _ToastEntry { class _ToastEntry {
final OverlayEntry? entry; final OverlayEntry entry;
final Duration? duration; final Duration duration;
final Duration fadeDuration;
_ToastEntry({this.entry, this.duration}); _ToastEntry({
required this.entry,
required this.duration,
required this.fadeDuration,
});
} }
/// internal [StatefulWidget] which handles the show and hide /// internal [StatefulWidget] which handles the show and hide
/// animations for [FToast] /// animations for [FToast]
class _ToastStateFul extends StatefulWidget { class _ToastStateFul extends StatefulWidget {
_ToastStateFul(this.child, this.duration, {Key? key, this.fadeDuration = 350}) _ToastStateFul(this.child, this.duration, this.fadeDuration, {Key? key})
: super(key: key); : super(key: key);
final Widget child; final Widget child;
final Duration duration; final Duration duration;
final int fadeDuration; final Duration fadeDuration;
@override @override
ToastStateFulState createState() => ToastStateFulState(); ToastStateFulState createState() => ToastStateFulState();
...@@ -297,7 +305,7 @@ class ToastStateFulState extends State<_ToastStateFul> ...@@ -297,7 +305,7 @@ class ToastStateFulState extends State<_ToastStateFul>
void initState() { void initState() {
_animationController = AnimationController( _animationController = AnimationController(
vsync: this, vsync: this,
duration: Duration(milliseconds: widget.fadeDuration), duration: widget.fadeDuration,
); );
_fadeAnimation = _fadeAnimation =
CurvedAnimation(parent: _animationController!, curve: Curves.easeIn); CurvedAnimation(parent: _animationController!, curve: Curves.easeIn);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment