diff --git a/packages/wyatt_ui_kit/lib/src/components/cards/widgets/card_wrapper.dart b/packages/wyatt_ui_kit/lib/src/components/cards/widgets/card_wrapper.dart index c2b91ca4..aed05cae 100644 --- a/packages/wyatt_ui_kit/lib/src/components/cards/widgets/card_wrapper.dart +++ b/packages/wyatt_ui_kit/lib/src/components/cards/widgets/card_wrapper.dart @@ -66,84 +66,17 @@ class CardWrapper extends StatelessWidget { child: child, ); - Gradient? _cardGradient(BuildContext context) { - if (backgroundColors != null && backgroundColors!.isGradient) { - return LinearGradientHelper.fromMultiColor(backgroundColors!); - } else { - final extensionCardColor = - Theme.of(context).extension(); - - if (extensionCardColor != null && - extensionCardColor.backgroundColors != null && - extensionCardColor.backgroundColors!.isGradient) { - return LinearGradient( - colors: extensionCardColor.backgroundColors!.colors, - ); - } - } - return null; - } - - Color? _cardColor(BuildContext context) { - if (backgroundColors != null && backgroundColors!.isColor) { - return backgroundColors!.color; - } else { - final extensionCardColor = - Theme.of(context).extension(); - - if (extensionCardColor != null && - extensionCardColor.backgroundColors != null) { - return extensionCardColor.backgroundColors!.color; - } - } - return Theme.of(context).cardColor; - } - - BoxBorder? _boxBorder(BuildContext context) { - if (borderColors != null) { - if (borderColors!.isGradient) { - return GradientBoxBorder( - gradient: LinearGradientHelper.fromMultiColor(borderColors!), - ); - } else if (borderColors!.isColor) { - return Border.all( - color: borderColors!.color, - ); - } - } else { - final extensionCardColor = - Theme.of(context).extension(); - if (extensionCardColor != null && - extensionCardColor.borderColors != null) { - if (extensionCardColor.borderColors!.isGradient) { - return GradientBoxBorder( - gradient: LinearGradient( - colors: extensionCardColor.borderColors!.colors, - ), - ); - } else if (extensionCardColor.backgroundColors!.colors.isNotEmpty) { - return Border.all( - color: extensionCardColor.backgroundColors!.color, - ); - } - } - } - return null; - } - List _shadow(BuildContext context) { - final shadows = List.empty(growable: true); - if (shadow != null) { - shadows.add(shadow!); - } else { - final extensionCardColor = - Theme.of(context).extension(); - if (extensionCardColor != null && - extensionCardColor.shadowColor != null) { - shadows.add(extensionCardColor.shadowColor!); - } - } - return shadows; + final themeShadow = ThemeHelper.getThemeElement( + [ + shadow, + Theme.of(context).extension()?.shadowColor, + ], + valueValidator: (shadow) => shadow != null, + transform: (shadow) => shadow, + defaultValue: null, + ); + return (themeShadow != null) ? [themeShadow] : []; } @override @@ -152,9 +85,53 @@ class CardWrapper extends StatelessWidget { key: _key, decoration: BoxDecoration( borderRadius: const BorderRadius.all(Radius.circular(12)), - gradient: _cardGradient(context), - color: _cardColor(context), - border: _boxBorder(context), + gradient: ThemeHelper.getThemeElement( + [ + backgroundColors, + Theme.of(context) + .extension() + ?.backgroundColors, + ], + valueValidator: (multiColor) => + multiColor != null && multiColor.isGradient, + transform: (multiColor) => + LinearGradientHelper.fromMultiColor(multiColor!), + defaultValue: null, + ), + color: ThemeHelper.getThemeElement( + [ + backgroundColors, + Theme.of(context) + .extension() + ?.backgroundColors, + ], + valueValidator: (multiColor) => + multiColor != null && multiColor.isColor, + transform: (multiColor) => multiColor?.color, + defaultValue: Theme.of(context).cardColor, + ), + border: ThemeHelper.getThemeElement( + [ + borderColors, + Theme.of(context).extension()?.borderColors, + Theme.of(context) + .extension() + ?.backgroundColors, + ], + valueValidator: (multiColor) => + multiColor != null && multiColor.isColor, + transform: (multiColor) { + if (multiColor!.isGradient) { + return GradientBoxBorder( + gradient: LinearGradientHelper.fromMultiColor(multiColor), + ); + } + return Border.all( + color: multiColor.color, + ); + }, + defaultValue: null, + ), boxShadow: _shadow(context), ), child: (minSize != null && maxSize != null) diff --git a/packages/wyatt_ui_kit/lib/src/core/helpers/theme_helper.dart b/packages/wyatt_ui_kit/lib/src/core/helpers/theme_helper.dart index 5414bfce..79cb54c8 100644 --- a/packages/wyatt_ui_kit/lib/src/core/helpers/theme_helper.dart +++ b/packages/wyatt_ui_kit/lib/src/core/helpers/theme_helper.dart @@ -14,7 +14,15 @@ // You should have received a copy of the GNU General Public License // along with super program. If not, see . +/// A helper class for getting theme elements. abstract class ThemeHelper { + + /// Gets a theme element from a list of styles. + /// Styles are checked in order, and the first one that passes the + /// [valueValidator] is returned. + /// If no style passes the [valueValidator], the [defaultValue] is returned. + /// If [styles] is null or empty, the [defaultValue] is returned. + /// Style elements are transformed using the [transform] function. static T? getThemeElement( List? styles, { required T? Function(P?)? transform,