refactor(ui_kit): use ThemeHelper in cards

This commit is contained in:
Hugo Pointcheval 2023-02-21 15:37:42 +01:00
parent 12d04c91ed
commit 93cc5058d1
Signed by: hugo
GPG Key ID: 3AAC487E131E00BC
2 changed files with 65 additions and 80 deletions

View File

@ -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<CardThemeExtension>();
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<CardThemeExtension>();
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<CardThemeExtension>();
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<BoxShadow> _shadow(BuildContext context) {
final shadows = List<BoxShadow>.empty(growable: true);
if (shadow != null) {
shadows.add(shadow!);
} else {
final extensionCardColor =
Theme.of(context).extension<CardThemeExtension>();
if (extensionCardColor != null &&
extensionCardColor.shadowColor != null) {
shadows.add(extensionCardColor.shadowColor!);
}
}
return shadows;
final themeShadow = ThemeHelper.getThemeElement<BoxShadow, BoxShadow>(
[
shadow,
Theme.of(context).extension<CardThemeExtension>()?.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<MultiColor, Gradient>(
[
backgroundColors,
Theme.of(context)
.extension<CardThemeExtension>()
?.backgroundColors,
],
valueValidator: (multiColor) =>
multiColor != null && multiColor.isGradient,
transform: (multiColor) =>
LinearGradientHelper.fromMultiColor(multiColor!),
defaultValue: null,
),
color: ThemeHelper.getThemeElement<MultiColor, Color>(
[
backgroundColors,
Theme.of(context)
.extension<CardThemeExtension>()
?.backgroundColors,
],
valueValidator: (multiColor) =>
multiColor != null && multiColor.isColor,
transform: (multiColor) => multiColor?.color,
defaultValue: Theme.of(context).cardColor,
),
border: ThemeHelper.getThemeElement<MultiColor, BoxBorder>(
[
borderColors,
Theme.of(context).extension<CardThemeExtension>()?.borderColors,
Theme.of(context)
.extension<CardThemeExtension>()
?.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)

View File

@ -14,7 +14,15 @@
// You should have received a copy of the GNU General Public License
// along with super program. If not, see <https://www.gnu.org/licenses/>.
/// 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<P, T>(
List<P?>? styles, {
required T? Function(P?)? transform,