From c3c3481c787dc966f90cfd9ca4fe7da8003d3951 Mon Sep 17 00:00:00 2001 From: Brendan Ejike Date: Wed, 18 Oct 2023 12:27:50 +0100 Subject: [PATCH 1/3] feat: add copyWith method Example usage: create a blank `Flushbar()` and manually dismiss it somewhere else. ```dart var fb = Flushbar(...); fb = fb.copyWith( mainButton: TextButton( onPressed: () { fb.dismiss(); } ), ); fb.show(context); ``` --- lib/flushbar.dart | 159 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 113 insertions(+), 46 deletions(-) diff --git a/lib/flushbar.dart b/lib/flushbar.dart index 43c38ab..fa6dadc 100644 --- a/lib/flushbar.dart +++ b/lib/flushbar.dart @@ -235,8 +235,7 @@ class Flushbar extends StatefulWidget { flushbar: this, ) as route.FlushbarRoute; - return await Navigator.of(context, rootNavigator: false) - .push(flushbarRoute as Route); + return await Navigator.of(context, rootNavigator: false).push(flushbarRoute as Route); } /// Dismisses the flushbar causing is to return a future containing [result]. @@ -292,10 +291,105 @@ class Flushbar extends StatefulWidget { @override State createState() => _FlushbarState(); + + Flushbar copyWith({ + String? title, + Color? titleColor, + double? titleSize, + String? message, + Widget? titleText, + Widget? messageText, + Color? messageColor, + double? messageSize, + Widget? icon, + bool? shouldIconPulse, + Widget? mainButton, + OnTap? onTap, + Duration? duration, + bool? isDismissible, + double? maxWidth, + EdgeInsets? margin, + EdgeInsets? padding, + BorderRadius? borderRadius, + Color? borderColor, + double? borderWidth, + Color? backgroundColor, + Color? leftBarIndicatorColor, + List? boxShadows, + Gradient? backgroundGradient, + bool? showProgressIndicator, + AnimationController? progressIndicatorController, + Color? progressIndicatorBackgroundColor, + Animation? progressIndicatorValueColor, + FlushbarPosition? flushbarPosition, + double? positionOffset, + FlushbarDismissDirection? dismissDirection, + FlushbarStyle? flushbarStyle, + Curve? forwardAnimationCurve, + Curve? reverseAnimationCurve, + Duration? animationDuration, + double? barBlur, + bool? blockBackgroundInteraction, + double? routeBlur, + Color? routeColor, + Form? userInputForm, + Offset? endOffset, + bool? safeArea, + route.FlushbarRoute? flushbarRoute, + void Function(FlushbarStatus?)? onStatusChanged, + TextDirection? textDirection, + }) { + return Flushbar( + title: title ?? this.title, + titleColor: titleColor ?? this.titleColor, + titleSize: titleSize ?? this.titleSize, + message: message ?? this.message, + messageColor: messageColor ?? this.messageColor, + messageSize: messageSize ?? this.messageSize, + titleText: titleText ?? this.titleText, + messageText: messageText ?? this.messageText, + icon: icon ?? this.icon, + shouldIconPulse: shouldIconPulse ?? this.shouldIconPulse, + mainButton: mainButton ?? this.mainButton, + onTap: onTap ?? this.onTap, + duration: duration ?? this.duration, + isDismissible: isDismissible ?? this.isDismissible, + maxWidth: maxWidth ?? this.maxWidth, + margin: margin ?? this.margin, + padding: padding ?? this.padding, + borderRadius: borderRadius ?? this.borderRadius, + borderColor: borderColor ?? this.borderColor, + borderWidth: borderWidth ?? this.borderWidth, + backgroundColor: backgroundColor ?? this.backgroundColor, + leftBarIndicatorColor: leftBarIndicatorColor ?? this.leftBarIndicatorColor, + boxShadows: boxShadows ?? this.boxShadows, + backgroundGradient: backgroundGradient ?? this.backgroundGradient, + showProgressIndicator: showProgressIndicator ?? this.showProgressIndicator, + progressIndicatorController: progressIndicatorController ?? this.progressIndicatorController, + progressIndicatorBackgroundColor: progressIndicatorBackgroundColor ?? this.progressIndicatorBackgroundColor, + progressIndicatorValueColor: progressIndicatorValueColor ?? this.progressIndicatorValueColor, + flushbarPosition: flushbarPosition ?? this.flushbarPosition, + positionOffset: positionOffset ?? this.positionOffset, + dismissDirection: dismissDirection ?? this.dismissDirection, + flushbarStyle: flushbarStyle ?? this.flushbarStyle, + forwardAnimationCurve: forwardAnimationCurve ?? this.forwardAnimationCurve, + reverseAnimationCurve: reverseAnimationCurve ?? this.reverseAnimationCurve, + animationDuration: animationDuration ?? this.animationDuration, + barBlur: barBlur ?? this.barBlur, + blockBackgroundInteraction: blockBackgroundInteraction ?? this.blockBackgroundInteraction, + routeBlur: routeBlur ?? this.routeBlur, + routeColor: routeColor ?? this.routeColor, + userInputForm: userInputForm ?? this.userInputForm, + endOffset: endOffset ?? this.endOffset, + safeArea: safeArea ?? this.safeArea, + flushbarRoute: flushbarRoute ?? this.flushbarRoute, + onStatusChanged: onStatusChanged ?? this.onStatusChanged, + textDirection: textDirection ?? this.textDirection, + ); + } } -class _FlushbarState extends State> - with TickerProviderStateMixin { +class _FlushbarState extends State> with TickerProviderStateMixin { final Duration _pulseAnimationDuration = const Duration(seconds: 1); final Widget _emptyWidget = const SizedBox(); final double _initialOpacity = 1.0; @@ -320,10 +414,7 @@ class _FlushbarState extends State> _backgroundBoxKey = GlobalKey(); _boxHeightCompleter = Completer(); - assert( - widget.userInputForm != null || - ((widget.message != null && widget.message!.isNotEmpty) || - widget.messageText != null), + assert(widget.userInputForm != null || ((widget.message != null && widget.message!.isNotEmpty) || widget.messageText != null), 'A message is mandatory if you are not using userInputForm. Set either a message or messageText'); _isTitlePresent = (widget.title != null || widget.titleText != null); @@ -365,16 +456,13 @@ class _FlushbarState extends State> } void _configureProgressIndicatorAnimation() { - if (widget.showProgressIndicator && - widget.progressIndicatorController != null) { - _progressAnimation = CurvedAnimation( - curve: Curves.linear, parent: widget.progressIndicatorController!); + if (widget.showProgressIndicator && widget.progressIndicatorController != null) { + _progressAnimation = CurvedAnimation(curve: Curves.linear, parent: widget.progressIndicatorController!); } } void _configurePulseAnimation() { - _fadeController = - AnimationController(vsync: this, duration: _pulseAnimationDuration); + _fadeController = AnimationController(vsync: this, duration: _pulseAnimationDuration); _fadeAnimation = Tween(begin: _initialOpacity, end: _finalOpacity).animate( CurvedAnimation( parent: _fadeController!, @@ -400,9 +488,7 @@ class _FlushbarState extends State> return Align( heightFactor: 1.0, child: Material( - color: widget.flushbarStyle == FlushbarStyle.FLOATING - ? Colors.transparent - : widget.backgroundColor, + color: widget.flushbarStyle == FlushbarStyle.FLOATING ? Colors.transparent : widget.backgroundColor, child: GestureDetector( onTap: () => widget.onTap?.call(widget), child: _getFlushbar(), @@ -433,8 +519,7 @@ class _FlushbarState extends State> return ClipRRect( borderRadius: widget.borderRadius ?? BorderRadius.zero, child: BackdropFilter( - filter: ImageFilter.blur( - sigmaX: widget.barBlur, sigmaY: widget.barBlur), + filter: ImageFilter.blur(sigmaX: widget.barBlur, sigmaY: widget.barBlur), child: Container( height: snapshot.data!.height, width: snapshot.data!.width, @@ -457,21 +542,16 @@ class _FlushbarState extends State> Widget _generateInputFlushbar() { return Container( key: _backgroundBoxKey, - constraints: widget.maxWidth != null - ? BoxConstraints(maxWidth: widget.maxWidth!) - : null, + constraints: widget.maxWidth != null ? BoxConstraints(maxWidth: widget.maxWidth!) : null, decoration: BoxDecoration( color: widget.backgroundColor, gradient: widget.backgroundGradient, boxShadow: widget.boxShadows, borderRadius: widget.borderRadius, - border: widget.borderColor != null - ? Border.all(color: widget.borderColor!, width: widget.borderWidth) - : null, + border: widget.borderColor != null ? Border.all(color: widget.borderColor!, width: widget.borderWidth) : null, ), child: Padding( - padding: const EdgeInsets.only( - left: 8.0, right: 8.0, bottom: 8.0, top: 16.0), + padding: const EdgeInsets.only(left: 8.0, right: 8.0, bottom: 8.0, top: 16.0), child: FocusScope( node: _focusNode, autofocus: true, @@ -484,17 +564,13 @@ class _FlushbarState extends State> Widget _generateFlushbar() { return Container( key: _backgroundBoxKey, - constraints: widget.maxWidth != null - ? BoxConstraints(maxWidth: widget.maxWidth!) - : null, + constraints: widget.maxWidth != null ? BoxConstraints(maxWidth: widget.maxWidth!) : null, decoration: BoxDecoration( color: widget.backgroundColor, gradient: widget.backgroundGradient, boxShadow: widget.boxShadows, borderRadius: widget.borderRadius, - border: widget.borderColor != null - ? Border.all(color: widget.borderColor!, width: widget.borderWidth) - : null, + border: widget.borderColor != null ? Border.all(color: widget.borderColor!, width: widget.borderWidth) : null, ), child: Column( mainAxisSize: MainAxisSize.min, @@ -707,12 +783,8 @@ class _FlushbarState extends State> borderRadius: widget.borderRadius == null ? null : widget.textDirection == TextDirection.ltr - ? BorderRadius.only( - topLeft: widget.borderRadius!.topLeft, - bottomLeft: widget.borderRadius!.bottomLeft) - : BorderRadius.only( - topRight: widget.borderRadius!.topRight, - bottomRight: widget.borderRadius!.bottomRight), + ? BorderRadius.only(topLeft: widget.borderRadius!.topLeft, bottomLeft: widget.borderRadius!.bottomLeft) + : BorderRadius.only(topRight: widget.borderRadius!.topRight, bottomRight: widget.borderRadius!.bottomRight), color: widget.leftBarIndicatorColor, ), ); @@ -743,19 +815,14 @@ class _FlushbarState extends State> return widget.titleText ?? Text( widget.title ?? '', - style: TextStyle( - fontSize: widget.titleSize ?? 16.0, - color: widget.titleColor ?? Colors.white, - fontWeight: FontWeight.bold), + style: TextStyle(fontSize: widget.titleSize ?? 16.0, color: widget.titleColor ?? Colors.white, fontWeight: FontWeight.bold), ); } Text _getDefaultNotificationText() { return Text( widget.message ?? '', - style: TextStyle( - fontSize: widget.messageSize ?? 14.0, - color: widget.messageColor ?? Colors.white), + style: TextStyle(fontSize: widget.messageSize ?? 14.0, color: widget.messageColor ?? Colors.white), ); } From 108e367e71c582ecd45a40a8aff1baf9214ecbd2 Mon Sep 17 00:00:00 2001 From: Brendan Ejike Date: Wed, 18 Oct 2023 12:49:06 +0100 Subject: [PATCH 2/3] fix: remove 'flushRoute' initialization --- lib/flushbar.dart | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/flushbar.dart b/lib/flushbar.dart index fa6dadc..867e89b 100644 --- a/lib/flushbar.dart +++ b/lib/flushbar.dart @@ -335,7 +335,6 @@ class Flushbar extends StatefulWidget { Form? userInputForm, Offset? endOffset, bool? safeArea, - route.FlushbarRoute? flushbarRoute, void Function(FlushbarStatus?)? onStatusChanged, TextDirection? textDirection, }) { @@ -382,7 +381,6 @@ class Flushbar extends StatefulWidget { userInputForm: userInputForm ?? this.userInputForm, endOffset: endOffset ?? this.endOffset, safeArea: safeArea ?? this.safeArea, - flushbarRoute: flushbarRoute ?? this.flushbarRoute, onStatusChanged: onStatusChanged ?? this.onStatusChanged, textDirection: textDirection ?? this.textDirection, ); From 238620b190a53aa36d584a03f143f710aac2cc40 Mon Sep 17 00:00:00 2001 From: Brendan Ejike Date: Wed, 18 Oct 2023 12:56:44 +0100 Subject: [PATCH 3/3] feat: pass flushbar instance to mainButton - Removed copyWith method --- lib/flushbar.dart | 98 +--------------------------------------- lib/flushbar_helper.dart | 17 ++----- 2 files changed, 6 insertions(+), 109 deletions(-) diff --git a/lib/flushbar.dart b/lib/flushbar.dart index 867e89b..793adb1 100644 --- a/lib/flushbar.dart +++ b/lib/flushbar.dart @@ -119,7 +119,7 @@ class Flushbar extends StatefulWidget { final bool shouldIconPulse; /// Use if you need an action from the user. [TextButton] is recommended here - final Widget? mainButton; + final Widget Function(Flushbar)? mainButton; /// A callback that registers the user's click anywhere. An alternative to [mainButton] final OnTap? onTap; @@ -291,100 +291,6 @@ class Flushbar extends StatefulWidget { @override State createState() => _FlushbarState(); - - Flushbar copyWith({ - String? title, - Color? titleColor, - double? titleSize, - String? message, - Widget? titleText, - Widget? messageText, - Color? messageColor, - double? messageSize, - Widget? icon, - bool? shouldIconPulse, - Widget? mainButton, - OnTap? onTap, - Duration? duration, - bool? isDismissible, - double? maxWidth, - EdgeInsets? margin, - EdgeInsets? padding, - BorderRadius? borderRadius, - Color? borderColor, - double? borderWidth, - Color? backgroundColor, - Color? leftBarIndicatorColor, - List? boxShadows, - Gradient? backgroundGradient, - bool? showProgressIndicator, - AnimationController? progressIndicatorController, - Color? progressIndicatorBackgroundColor, - Animation? progressIndicatorValueColor, - FlushbarPosition? flushbarPosition, - double? positionOffset, - FlushbarDismissDirection? dismissDirection, - FlushbarStyle? flushbarStyle, - Curve? forwardAnimationCurve, - Curve? reverseAnimationCurve, - Duration? animationDuration, - double? barBlur, - bool? blockBackgroundInteraction, - double? routeBlur, - Color? routeColor, - Form? userInputForm, - Offset? endOffset, - bool? safeArea, - void Function(FlushbarStatus?)? onStatusChanged, - TextDirection? textDirection, - }) { - return Flushbar( - title: title ?? this.title, - titleColor: titleColor ?? this.titleColor, - titleSize: titleSize ?? this.titleSize, - message: message ?? this.message, - messageColor: messageColor ?? this.messageColor, - messageSize: messageSize ?? this.messageSize, - titleText: titleText ?? this.titleText, - messageText: messageText ?? this.messageText, - icon: icon ?? this.icon, - shouldIconPulse: shouldIconPulse ?? this.shouldIconPulse, - mainButton: mainButton ?? this.mainButton, - onTap: onTap ?? this.onTap, - duration: duration ?? this.duration, - isDismissible: isDismissible ?? this.isDismissible, - maxWidth: maxWidth ?? this.maxWidth, - margin: margin ?? this.margin, - padding: padding ?? this.padding, - borderRadius: borderRadius ?? this.borderRadius, - borderColor: borderColor ?? this.borderColor, - borderWidth: borderWidth ?? this.borderWidth, - backgroundColor: backgroundColor ?? this.backgroundColor, - leftBarIndicatorColor: leftBarIndicatorColor ?? this.leftBarIndicatorColor, - boxShadows: boxShadows ?? this.boxShadows, - backgroundGradient: backgroundGradient ?? this.backgroundGradient, - showProgressIndicator: showProgressIndicator ?? this.showProgressIndicator, - progressIndicatorController: progressIndicatorController ?? this.progressIndicatorController, - progressIndicatorBackgroundColor: progressIndicatorBackgroundColor ?? this.progressIndicatorBackgroundColor, - progressIndicatorValueColor: progressIndicatorValueColor ?? this.progressIndicatorValueColor, - flushbarPosition: flushbarPosition ?? this.flushbarPosition, - positionOffset: positionOffset ?? this.positionOffset, - dismissDirection: dismissDirection ?? this.dismissDirection, - flushbarStyle: flushbarStyle ?? this.flushbarStyle, - forwardAnimationCurve: forwardAnimationCurve ?? this.forwardAnimationCurve, - reverseAnimationCurve: reverseAnimationCurve ?? this.reverseAnimationCurve, - animationDuration: animationDuration ?? this.animationDuration, - barBlur: barBlur ?? this.barBlur, - blockBackgroundInteraction: blockBackgroundInteraction ?? this.blockBackgroundInteraction, - routeBlur: routeBlur ?? this.routeBlur, - routeColor: routeColor ?? this.routeColor, - userInputForm: userInputForm ?? this.userInputForm, - endOffset: endOffset ?? this.endOffset, - safeArea: safeArea ?? this.safeArea, - onStatusChanged: onStatusChanged ?? this.onStatusChanged, - textDirection: textDirection ?? this.textDirection, - ); - } } class _FlushbarState extends State> with TickerProviderStateMixin { @@ -826,7 +732,7 @@ class _FlushbarState extends State> with TickerPr Widget? _getMainActionButton() { if (widget.mainButton != null) { - return widget.mainButton; + return widget.mainButton!(widget); } else { return null; } diff --git a/lib/flushbar_helper.dart b/lib/flushbar_helper.dart index 3b478ad..0be9c7b 100644 --- a/lib/flushbar_helper.dart +++ b/lib/flushbar_helper.dart @@ -3,10 +3,7 @@ import 'package:flutter/material.dart'; class FlushbarHelper { /// Get a success notification flushbar. - static Flushbar createSuccess( - {required String message, - String? title, - Duration duration = const Duration(seconds: 3)}) { + static Flushbar createSuccess({required String message, String? title, Duration duration = const Duration(seconds: 3)}) { return Flushbar( title: title, message: message, @@ -20,10 +17,7 @@ class FlushbarHelper { } /// Get an information notification flushbar - static Flushbar createInformation( - {required String message, - String? title, - Duration duration = const Duration(seconds: 3)}) { + static Flushbar createInformation({required String message, String? title, Duration duration = const Duration(seconds: 3)}) { return Flushbar( title: title, message: message, @@ -38,10 +32,7 @@ class FlushbarHelper { } /// Get a error notification flushbar - static Flushbar createError( - {required String message, - String? title, - Duration duration = const Duration(seconds: 3)}) { + static Flushbar createError({required String message, String? title, Duration duration = const Duration(seconds: 3)}) { return Flushbar( title: title, message: message, @@ -58,7 +49,7 @@ class FlushbarHelper { /// Get a flushbar that can receive a user action through a button. static Flushbar createAction( {required String message, - required Widget button, + required Widget Function(Flushbar) button, String? title, Duration duration = const Duration(seconds: 3)}) { return Flushbar(