feat(ui_kit-ui_components): Modify the theme resolver to utilize the theme helper and streamline the logic

This commit is contained in:
2023-02-23 10:24:54 +01:00
parent 970dde2ef1
commit 0c176ba0fa
11 changed files with 63 additions and 79 deletions
@@ -0,0 +1,49 @@
// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// super program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// super program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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.
/// Style elements are transformed using the [transform] function.
///
/// [styles]: A list of styles that need to be checked.
/// [transform]: A function that transforms each style element
/// to a [T] type.
/// [valueValidator]: An optional validation function that
/// determines if a style element is valid.
/// [combine]: A function that combines two [P] type objects to create
/// a new object.
static T? getThemeElement<P, T>(
List<P?>? styles, {
required T? Function(P?)? transform,
bool? Function(P?)? valueValidator,
P? Function(P?, P?)? combine,
}) {
final Iterable<P?>? validStyles = styles?.where(
(element) => valueValidator?.call(element) ?? (element != null),
);
return (validStyles?.isNotEmpty ?? false)
? transform?.call(
validStyles?.reduce(
(value, element) => combine?.call(value, element) ?? value,
),
)
: null;
}
}
@@ -15,6 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter/material.dart';
import 'package:wyatt_ui_components/src/core/utils/theme_helper.dart';
import 'package:wyatt_ui_components/src/domain/entities/theme_style.dart';
/// {@template theme_resolver}
@@ -38,52 +39,35 @@ abstract class ThemeResolver<S extends ThemeStyle<S>, T, E> {
S? Function(BuildContext context, {E? extra}) get customStyleFn;
/// Compute default value from Flutter Theme or with hardcoded values.
S computeDefaultValue(BuildContext context, {E? extra});
S? computeExtensionValueFn(
BuildContext context,
T themeExtension, {
S computeDefaultValue(
BuildContext context, {
E? extra,
});
/// Compute values from the extension if found
S? _computeExtensionValue(
BuildContext context, {
/// Compute extension value from custom component extension.
S? computeExtensionValueFn(
BuildContext context,
T? themeExtension, {
E? extra,
}) {
final themeExtension = Theme.of(context).extension<T>();
if (themeExtension != null) {
return computeExtensionValueFn(
context,
themeExtension,
extra: extra,
);
}
return null;
}
/// Compute custom value
S? _computeCustomValue(
BuildContext context, {
E? extra,
}) {
final customStyle = customStyleFn(
context,
extra: extra,
);
if (customStyle != null) {
return customStyle;
}
return null;
}
});
/// Choose most suitable style for a given context.
S negotiate(BuildContext context, {E? extra}) {
S style = computeDefaultValue(context, extra: extra);
style =
style.mergeWith(_computeExtensionValue(context, extra: extra)) ?? style;
style =
style.mergeWith(_computeCustomValue(context, extra: extra)) ?? style;
return style;
final style = computeDefaultValue(context, extra: extra);
return ThemeHelper.getThemeElement<S, S>(
[
style,
computeExtensionValueFn(
context,
Theme.of(context).extension<T>(),
extra: extra,
),
// _computeExtensionValue(context, extra: extra),
customStyleFn(context, extra: extra)
],
transform: (value) => value,
combine: (value, element) => value?.mergeWith(element),
) ??
style;
}
}
@@ -16,4 +16,5 @@
export 'multi_color.dart';
export 'text_wrapper.dart';
export 'theme_helper.dart';
export 'theme_resolver.dart';