|
| 1 | +library oktoast; |
| 2 | + |
| 3 | +import 'dart:async'; |
| 4 | +import 'dart:collection'; |
| 5 | +import 'dart:ui' as ui; |
| 6 | + |
| 7 | +import 'package:flutter/material.dart'; |
| 8 | + |
| 9 | +import 'position.dart'; |
| 10 | +import 'toast_manager.dart'; |
| 11 | + |
| 12 | +part 'toast_future.dart'; |
| 13 | +part '../widget/theme.dart'; |
| 14 | +part '../widget/oktoast.dart'; |
| 15 | +part '../widget/container.dart'; |
| 16 | + |
| 17 | +LinkedHashMap<_OKToastState, BuildContext> _contextMap = LinkedHashMap(); |
| 18 | +const _defaultDuration = Duration( |
| 19 | + milliseconds: 2300, |
| 20 | +); |
| 21 | + |
| 22 | +const _opacityDuration = Duration(milliseconds: 250); |
| 23 | + |
| 24 | +/// show toast with [msg], |
| 25 | +ToastFuture showToast( |
| 26 | + String msg, { |
| 27 | + BuildContext context, |
| 28 | + Duration duration = _defaultDuration, |
| 29 | + ToastPosition position, |
| 30 | + TextStyle textStyle, |
| 31 | + EdgeInsetsGeometry textPadding, |
| 32 | + Color backgroundColor, |
| 33 | + double radius, |
| 34 | + VoidCallback onDismiss, |
| 35 | + TextDirection textDirection, |
| 36 | + bool dismissOtherToast, |
| 37 | + TextAlign textAlign, |
| 38 | +}) { |
| 39 | + context ??= _contextMap.values.first; |
| 40 | + |
| 41 | + textStyle ??= _ToastTheme.of(context).textStyle ?? TextStyle(fontSize: 15.0); |
| 42 | + |
| 43 | + textAlign = _ToastTheme.of(context).textAlign; |
| 44 | + |
| 45 | + textPadding ??= _ToastTheme.of(context).textPadding; |
| 46 | + |
| 47 | + position ??= _ToastTheme.of(context).position; |
| 48 | + backgroundColor ??= _ToastTheme.of(context).backgroundColor; |
| 49 | + radius ??= _ToastTheme.of(context).radius; |
| 50 | + |
| 51 | + var direction = textDirection ?? |
| 52 | + _ToastTheme.of(context).textDirection ?? |
| 53 | + TextDirection.ltr; |
| 54 | + |
| 55 | + Widget widget = Container( |
| 56 | + margin: const EdgeInsets.all(50.0), |
| 57 | + decoration: BoxDecoration( |
| 58 | + color: backgroundColor, |
| 59 | + borderRadius: BorderRadius.circular(radius), |
| 60 | + ), |
| 61 | + padding: textPadding, |
| 62 | + child: ClipRect( |
| 63 | + child: Text( |
| 64 | + msg, |
| 65 | + style: textStyle, |
| 66 | + textAlign: textAlign, |
| 67 | + ), |
| 68 | + ), |
| 69 | + ); |
| 70 | + |
| 71 | + return showToastWidget( |
| 72 | + widget, |
| 73 | + context: context, |
| 74 | + duration: duration, |
| 75 | + onDismiss: onDismiss, |
| 76 | + position: position, |
| 77 | + dismissOtherToast: dismissOtherToast, |
| 78 | + textDirection: direction, |
| 79 | + ); |
| 80 | +} |
| 81 | + |
| 82 | +/// show [widget] with oktoast |
| 83 | +ToastFuture showToastWidget( |
| 84 | + Widget widget, { |
| 85 | + BuildContext context, |
| 86 | + Duration duration = _defaultDuration, |
| 87 | + ToastPosition position, |
| 88 | + VoidCallback onDismiss, |
| 89 | + bool dismissOtherToast, |
| 90 | + TextDirection textDirection, |
| 91 | + bool handleTouch, |
| 92 | +}) { |
| 93 | + context ??= _contextMap.values.first; |
| 94 | + OverlayEntry entry; |
| 95 | + ToastFuture future; |
| 96 | + position ??= _ToastTheme.of(context).position; |
| 97 | + |
| 98 | + handleTouch ??= _ToastTheme.of(context).handleTouch; |
| 99 | + |
| 100 | + var movingOnWindowChange = |
| 101 | + _ToastTheme.of(context)?.movingOnWindowChange ?? false; |
| 102 | + |
| 103 | + var direction = textDirection ?? |
| 104 | + _ToastTheme.of(context).textDirection ?? |
| 105 | + TextDirection.ltr; |
| 106 | + |
| 107 | + GlobalKey<__ToastContainerState> key = GlobalKey(); |
| 108 | + |
| 109 | + widget = Align( |
| 110 | + child: widget, |
| 111 | + alignment: position.align, |
| 112 | + ); |
| 113 | + |
| 114 | + entry = OverlayEntry(builder: (ctx) { |
| 115 | + return IgnorePointer( |
| 116 | + ignoring: !handleTouch, |
| 117 | + child: _ToastContainer( |
| 118 | + duration: duration, |
| 119 | + position: position, |
| 120 | + movingOnWindowChange: movingOnWindowChange, |
| 121 | + key: key, |
| 122 | + child: Directionality( |
| 123 | + textDirection: direction, |
| 124 | + child: widget, |
| 125 | + ), |
| 126 | + ), |
| 127 | + ); |
| 128 | + }); |
| 129 | + |
| 130 | + dismissOtherToast ??= _ToastTheme.of(context).dismissOtherOnShow ?? false; |
| 131 | + |
| 132 | + if (dismissOtherToast == true) { |
| 133 | + ToastManager().dismissAll(); |
| 134 | + } |
| 135 | + |
| 136 | + future = ToastFuture._(entry, onDismiss, key); |
| 137 | + |
| 138 | + Future.delayed(duration, () { |
| 139 | + future.dismiss(); |
| 140 | + }); |
| 141 | + |
| 142 | + Overlay.of(context).insert(entry); |
| 143 | + ToastManager().addFuture(future); |
| 144 | + |
| 145 | + return future; |
| 146 | +} |
| 147 | + |
| 148 | +/// use the method to dismiss all toast. |
| 149 | +void dismissAllToast({bool showAnim = false}) { |
| 150 | + ToastManager().dismissAll(showAnim: showAnim); |
| 151 | +} |
0 commit comments