|
| 1 | +import 'package:flushbar/flushbar.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | + |
| 4 | +import 'screen_sizes.dart'; |
| 5 | + |
| 6 | +const double smallNotificationWidth = 300; |
| 7 | +const Duration notificationDuration = Duration(seconds: 3); |
| 8 | + |
| 9 | +class NotificationHelper { |
| 10 | + static Flushbar error({ |
| 11 | + @required String message, |
| 12 | + @required NotificationConfig config, |
| 13 | + @required BuildContext context, |
| 14 | + }) { |
| 15 | + final isPhone = isPhoneSize(MediaQuery.of(context).size); |
| 16 | + final width = isPhone ? null : smallNotificationWidth; |
| 17 | + return Flushbar<dynamic>( |
| 18 | + message: message, |
| 19 | + title: config?.title, |
| 20 | + titleText: config?.titleText, |
| 21 | + backgroundColor: config?.backgroundColor, |
| 22 | + leftBarIndicatorColor: config?.leftBarIndicatorColor, |
| 23 | + boxShadows: config?.boxShadows, |
| 24 | + backgroundGradient: config?.backgroundGradient, |
| 25 | + icon: config?.icon ?? |
| 26 | + Icon( |
| 27 | + Icons.warning, |
| 28 | + size: 28.0, |
| 29 | + color: Theme.of(context).colorScheme.error, |
| 30 | + ), |
| 31 | + shouldIconPulse: config?.shouldIconPulse, |
| 32 | + duration: config?.duration ?? notificationDuration, |
| 33 | + isDismissible: config?.isDismissible, |
| 34 | + maxWidth: config?.maxWidth ?? width, |
| 35 | + margin: config?.margin, |
| 36 | + padding: config?.padding, |
| 37 | + borderRadius: config?.borderRadius, |
| 38 | + borderColor: config?.borderColor, |
| 39 | + borderWidth: config?.borderWidth, |
| 40 | + flushbarPosition: |
| 41 | + isPhone ? FlushbarPosition.BOTTOM : FlushbarPosition.TOP, |
| 42 | + dismissDirection: config?.dismissDirection, |
| 43 | + flushbarStyle: FlushbarStyle.GROUNDED, |
| 44 | + forwardAnimationCurve: config?.forwardAnimationCurve, |
| 45 | + reverseAnimationCurve: config?.reverseAnimationCurve, |
| 46 | + animationDuration: config?.animationDuration, |
| 47 | + barBlur: config?.barBlur, |
| 48 | + blockBackgroundInteraction: config?.blockBackgroundInteraction, |
| 49 | + routeBlur: config?.routeBlur, |
| 50 | + routeColor: config?.routeColor, |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + static Flushbar success({ |
| 55 | + @required String message, |
| 56 | + @required NotificationConfig config, |
| 57 | + @required BuildContext context, |
| 58 | + }) { |
| 59 | + final isPhone = isPhoneSize(MediaQuery.of(context).size); |
| 60 | + final width = isPhone ? null : smallNotificationWidth; |
| 61 | + return Flushbar<dynamic>( |
| 62 | + message: message, |
| 63 | + title: config?.title, |
| 64 | + titleText: config?.titleText, |
| 65 | + backgroundColor: config?.backgroundColor, |
| 66 | + leftBarIndicatorColor: config?.leftBarIndicatorColor, |
| 67 | + boxShadows: config?.boxShadows, |
| 68 | + backgroundGradient: config?.backgroundGradient, |
| 69 | + icon: config?.icon ?? |
| 70 | + Icon( |
| 71 | + Icons.check_circle, |
| 72 | + color: Colors.green[300], |
| 73 | + ), |
| 74 | + shouldIconPulse: config?.shouldIconPulse, |
| 75 | + duration: config?.duration ?? notificationDuration, |
| 76 | + isDismissible: config?.isDismissible, |
| 77 | + maxWidth: config?.maxWidth ?? width, |
| 78 | + margin: config?.margin, |
| 79 | + padding: config?.padding, |
| 80 | + borderRadius: config?.borderRadius, |
| 81 | + borderColor: config?.borderColor, |
| 82 | + borderWidth: config?.borderWidth, |
| 83 | + flushbarPosition: |
| 84 | + isPhone ? FlushbarPosition.BOTTOM : FlushbarPosition.TOP, |
| 85 | + dismissDirection: config?.dismissDirection, |
| 86 | + flushbarStyle: FlushbarStyle.GROUNDED, |
| 87 | + forwardAnimationCurve: config?.forwardAnimationCurve, |
| 88 | + reverseAnimationCurve: config?.reverseAnimationCurve, |
| 89 | + animationDuration: config?.animationDuration, |
| 90 | + barBlur: config?.barBlur, |
| 91 | + blockBackgroundInteraction: config?.blockBackgroundInteraction, |
| 92 | + routeBlur: config?.routeBlur, |
| 93 | + routeColor: config?.routeColor, |
| 94 | + ); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +class NotificationConfig { |
| 99 | + /// The title displayed to the user |
| 100 | + final String title; |
| 101 | + |
| 102 | + /// Replaces [title]. Although this accepts a [Widget], it is meant to receive [Text] or [RichText] |
| 103 | + final Widget titleText; |
| 104 | + |
| 105 | + /// Will be ignored if [backgroundGradient] is not null |
| 106 | + final Color backgroundColor; |
| 107 | + |
| 108 | + /// If not null, shows a left vertical bar to better indicate the humor of the notification. |
| 109 | + /// It is not possible to use it with a [Form] and I do not recommend using it with [LinearProgressIndicator] |
| 110 | + final Color leftBarIndicatorColor; |
| 111 | + |
| 112 | + /// [boxShadows] The shadows generated by Flushbar. Leave it null if you don't want a shadow. |
| 113 | + /// You can use more than one if you feel the need. |
| 114 | + /// Check (this example)[https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/material/shadows.dart] |
| 115 | + final List<BoxShadow> boxShadows; |
| 116 | + |
| 117 | + /// Makes [backgroundColor] be ignored. |
| 118 | + final Gradient backgroundGradient; |
| 119 | + |
| 120 | + /// You can use any widget here, but I recommend [Icon] or [Image] as indication of what kind |
| 121 | + /// of message you are displaying. Other widgets may break the layout |
| 122 | + final Widget icon; |
| 123 | + |
| 124 | + /// An option to animate the icon (if present). Defaults to true. |
| 125 | + final bool shouldIconPulse; |
| 126 | + |
| 127 | + /// How long until Flushbar will hide itself (be dismissed). To make it indefinite, leave it null. |
| 128 | + final Duration duration; |
| 129 | + |
| 130 | + /// Determines if the user can swipe or click the overlay (if [routeBlur] > 0) to dismiss. |
| 131 | + /// It is recommended that you set [duration] != null if this is false. |
| 132 | + /// If the user swipes to dismiss or clicks the overlay, no value will be returned. |
| 133 | + final bool isDismissible; |
| 134 | + |
| 135 | + /// Used to limit Flushbar width (usually on large screens) |
| 136 | + final double maxWidth; |
| 137 | + |
| 138 | + /// Adds a custom margin to Flushbar |
| 139 | + final EdgeInsets margin; |
| 140 | + |
| 141 | + /// Adds a custom padding to Flushbar |
| 142 | + /// The default follows material design guide line |
| 143 | + final EdgeInsets padding; |
| 144 | + |
| 145 | + /// Adds a radius to all corners of Flushbar. Best combined with [margin]. |
| 146 | + /// I do not recommend using it with [showProgressIndicator] or [leftBarIndicatorColor]. |
| 147 | + final double borderRadius; |
| 148 | + |
| 149 | + /// Adds a border to every side of Flushbar |
| 150 | + /// I do not recommend using it with [showProgressIndicator] or [leftBarIndicatorColor]. |
| 151 | + final Color borderColor; |
| 152 | + |
| 153 | + /// Changes the width of the border if [borderColor] is specified |
| 154 | + final double borderWidth; |
| 155 | + |
| 156 | + /// Flushbar can be based on [FlushbarPosition.TOP] or on [FlushbarPosition.BOTTOM] of your screen. |
| 157 | + /// [FlushbarPosition.BOTTOM] is the default. |
| 158 | + final FlushbarPosition flushbarPosition; |
| 159 | + |
| 160 | + /// [FlushbarDismissDirection.VERTICAL] by default. |
| 161 | + /// Can also be [FlushbarDismissDirection.HORIZONTAL] in which case both left and right dismiss are allowed. |
| 162 | + final FlushbarDismissDirection dismissDirection; |
| 163 | + |
| 164 | + /// Flushbar can be floating or be grounded to the edge of the screen. |
| 165 | + /// If grounded, I do not recommend using [margin] or [borderRadius]. [FlushbarStyle.FLOATING] is the default |
| 166 | + /// If grounded, I do not recommend using a [backgroundColor] with transparency or [barBlur] |
| 167 | + final FlushbarStyle flushbarStyle; |
| 168 | + |
| 169 | + /// The [Curve] animation used when show() is called. [Curves.easeOut] is default |
| 170 | + final Curve forwardAnimationCurve; |
| 171 | + |
| 172 | + /// The [Curve] animation used when dismiss() is called. [Curves.fastOutSlowIn] is default |
| 173 | + final Curve reverseAnimationCurve; |
| 174 | + |
| 175 | + /// Use it to speed up or slow down the animation duration |
| 176 | + final Duration animationDuration; |
| 177 | + |
| 178 | + /// Default is 0.0. If different than 0.0, blurs only Flushbar's background. |
| 179 | + /// To take effect, make sure your [backgroundColor] has some opacity. |
| 180 | + /// The greater the value, the greater the blur. |
| 181 | + final double barBlur; |
| 182 | + |
| 183 | + /// Determines if user can interact with the screen behind it |
| 184 | + /// If this is false, [routeBlur] and [routeColor] will be ignored |
| 185 | + final bool blockBackgroundInteraction; |
| 186 | + |
| 187 | + /// Default is 0.0. If different than 0.0, creates a blurred |
| 188 | + /// overlay that prevents the user from interacting with the screen. |
| 189 | + /// The greater the value, the greater the blur. |
| 190 | + /// It does not take effect if [blockBackgroundInteraction] is false |
| 191 | + final double routeBlur; |
| 192 | + |
| 193 | + /// Default is [Colors.transparent]. Only takes effect if [routeBlur] > 0.0. |
| 194 | + /// Make sure you use a color with transparency here e.g. Colors.grey[600].withOpacity(0.2). |
| 195 | + /// It does not take effect if [blockBackgroundInteraction] is false |
| 196 | + final Color routeColor; |
| 197 | + |
| 198 | + const NotificationConfig({ |
| 199 | + this.title, |
| 200 | + this.titleText, |
| 201 | + this.backgroundColor = const Color(0xFF303030), |
| 202 | + this.leftBarIndicatorColor, |
| 203 | + this.boxShadows, |
| 204 | + this.backgroundGradient, |
| 205 | + this.icon, |
| 206 | + this.shouldIconPulse = true, |
| 207 | + this.duration, |
| 208 | + this.isDismissible = true, |
| 209 | + this.maxWidth, |
| 210 | + this.margin = const EdgeInsets.all(0.0), |
| 211 | + this.padding = const EdgeInsets.all(16), |
| 212 | + this.borderRadius = 0.0, |
| 213 | + this.borderColor, |
| 214 | + this.borderWidth = 1.0, |
| 215 | + this.flushbarPosition = FlushbarPosition.BOTTOM, |
| 216 | + this.dismissDirection = FlushbarDismissDirection.VERTICAL, |
| 217 | + this.flushbarStyle = FlushbarStyle.GROUNDED, |
| 218 | + this.forwardAnimationCurve = Curves.easeOutCirc, |
| 219 | + this.reverseAnimationCurve = Curves.easeOutCirc, |
| 220 | + this.animationDuration = const Duration(seconds: 1), |
| 221 | + this.barBlur = 0.0, |
| 222 | + this.blockBackgroundInteraction = false, |
| 223 | + this.routeBlur, |
| 224 | + this.routeColor, |
| 225 | + }); |
| 226 | +} |
0 commit comments