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
@@ -80,33 +80,33 @@ class FileSelectionButtonThemeResolver extends ThemeResolver<
@override
FileSelectionButtonStyle? computeExtensionValueFn(
BuildContext context,
FileSelectionButtonThemeExtension themeExtension, {
FileSelectionButtonThemeExtension? themeExtension, {
ButtonState? extra,
}) {
FileSelectionButtonStyle? style;
switch (extra?.state) {
case ControlState.disabled:
style = themeExtension.disabledStyle;
style = themeExtension?.disabledStyle;
break;
case ControlState.focused:
style = themeExtension.focusedStyle;
style = themeExtension?.focusedStyle;
break;
case ControlState.hovered:
style = themeExtension.hoveredStyle;
style = themeExtension?.hoveredStyle;
break;
case ControlState.tapped:
style = themeExtension.tappedStyle;
style = themeExtension?.tappedStyle;
break;
case ControlState.normal:
case null:
style = themeExtension.normalStyle;
style = themeExtension?.normalStyle;
break;
}
if (extra?.isSelected ?? false) {
style = themeExtension.selectedStyle;
style = themeExtension?.selectedStyle;
}
if (extra?.isInvalid ?? false) {
style = themeExtension.invalidStyle;
style = themeExtension?.invalidStyle;
}
return style;
}
@@ -76,21 +76,21 @@ class FlatButtonThemeResolver extends ThemeResolver<FlatButtonStyle,
@override
FlatButtonStyle? computeExtensionValueFn(
BuildContext context,
FlatButtonThemeExtension themeExtension, {
FlatButtonThemeExtension? themeExtension, {
ControlState? extra,
}) {
switch (extra) {
case ControlState.disabled:
return themeExtension.disabledStyle;
return themeExtension?.disabledStyle;
case ControlState.focused:
return themeExtension.focusedStyle;
return themeExtension?.focusedStyle;
case ControlState.hovered:
return themeExtension.hoveredStyle;
return themeExtension?.hoveredStyle;
case ControlState.tapped:
return themeExtension.tappedStyle;
return themeExtension?.tappedStyle;
case ControlState.normal:
case null:
return themeExtension.normalStyle;
return themeExtension?.normalStyle;
}
}
}
@@ -74,21 +74,21 @@ class SimpleIconButtonThemeResolver extends ThemeResolver<SimpleIconButtonStyle,
@override
SimpleIconButtonStyle? computeExtensionValueFn(
BuildContext context,
SimpleIconButtonThemeExtension themeExtension, {
SimpleIconButtonThemeExtension? themeExtension, {
ControlState? extra,
}) {
switch (extra) {
case ControlState.disabled:
return themeExtension.disabledStyle;
return themeExtension?.disabledStyle;
case ControlState.focused:
return themeExtension.focusedStyle;
return themeExtension?.focusedStyle;
case ControlState.hovered:
return themeExtension.hoveredStyle;
return themeExtension?.hoveredStyle;
case ControlState.tapped:
return themeExtension.tappedStyle;
return themeExtension?.tappedStyle;
case ControlState.normal:
case null:
return themeExtension.normalStyle;
return themeExtension?.normalStyle;
}
}
}
@@ -76,30 +76,30 @@ class SymbolButtonThemeResolver extends ThemeResolver<SymbolButtonStyle,
@override
SymbolButtonStyle? computeExtensionValueFn(
BuildContext context,
SymbolButtonThemeExtension themeExtension, {
SymbolButtonThemeExtension? themeExtension, {
ButtonState? extra,
}) {
SymbolButtonStyle? style;
switch (extra?.state) {
case ControlState.disabled:
style = themeExtension.disabledStyle;
style = themeExtension?.disabledStyle;
break;
case ControlState.focused:
style = themeExtension.focusedStyle;
style = themeExtension?.focusedStyle;
break;
case ControlState.hovered:
style = themeExtension.hoveredStyle;
style = themeExtension?.hoveredStyle;
break;
case ControlState.tapped:
style = themeExtension.tappedStyle;
style = themeExtension?.tappedStyle;
break;
case ControlState.normal:
case null:
style = themeExtension.normalStyle;
style = themeExtension?.normalStyle;
break;
}
if (extra?.isSelected ?? false) {
style = themeExtension.selectedStyle;
style = themeExtension?.selectedStyle;
}
return style;
@@ -48,11 +48,11 @@ class LoaderThemeResolver
@override
LoaderStyle? computeExtensionValueFn(
BuildContext context,
LoaderThemeExtension themeExtension, {
LoaderThemeExtension? themeExtension, {
void extra,
}) =>
LoaderStyle(
colors: themeExtension.colors,
stroke: themeExtension.stroke,
colors: themeExtension?.colors,
stroke: themeExtension?.stroke,
);
}
@@ -43,11 +43,11 @@ class RichTextBuilderThemeResolver extends ThemeResolver<RichTextBuilderStyle,
@override
RichTextBuilderStyle? computeExtensionValueFn(
BuildContext context,
RichTextBuilderThemeExtension themeExtension, {
RichTextBuilderThemeExtension? themeExtension, {
void extra,
}) =>
RichTextBuilderStyle(
defaultStyle: themeExtension.defaultStyle,
styles: themeExtension.styles,
defaultStyle: themeExtension?.defaultStyle,
styles: themeExtension?.styles,
);
}
@@ -118,19 +118,19 @@ class TextInputThemeResolver extends ThemeResolver<TextInputStyle,
@override
TextInputStyle? computeExtensionValueFn(
BuildContext context,
TextInputThemeExtension themeExtension, {
TextInputThemeExtension? themeExtension, {
TextInputState? extra,
}) {
TextInputStyle? textInputStyle;
switch (extra?.controlState) {
case ControlState.focused:
textInputStyle = themeExtension.focusedStyle;
textInputStyle = themeExtension?.focusedStyle;
break;
case ControlState.disabled:
textInputStyle = themeExtension.disableStyle;
textInputStyle = themeExtension?.disableStyle;
break;
case ControlState.normal:
textInputStyle = themeExtension.normalStyle;
textInputStyle = themeExtension?.normalStyle;
break;
case ControlState.hovered:
case ControlState.tapped:
@@ -141,7 +141,7 @@ class TextInputThemeResolver extends ThemeResolver<TextInputStyle,
TextInputStyle? style;
switch (extra?.statusState) {
case StatusState.error:
style = themeExtension.errorStyle;
style = themeExtension?.errorStyle;
break;
case StatusState.initial:
case StatusState.success:
@@ -15,4 +15,3 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
export './linear_gradient_helper.dart';
export './theme_helper.dart';
@@ -1,49 +0,0 @@
// 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;
}
}