master #81
@ -15,6 +15,7 @@
 | 
				
			|||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
 | 
					// along with this program. If not, see <https://www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import 'package:flutter/material.dart';
 | 
					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';
 | 
					import 'package:wyatt_ui_components/src/domain/entities/theme_style.dart';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// {@template theme_resolver}
 | 
					/// {@template theme_resolver}
 | 
				
			||||||
@ -38,52 +39,35 @@ abstract class ThemeResolver<S extends ThemeStyle<S>, T, E> {
 | 
				
			|||||||
  S? Function(BuildContext context, {E? extra}) get customStyleFn;
 | 
					  S? Function(BuildContext context, {E? extra}) get customStyleFn;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /// Compute default value from Flutter Theme or with hardcoded values.
 | 
					  /// Compute default value from Flutter Theme or with hardcoded values.
 | 
				
			||||||
  S computeDefaultValue(BuildContext context, {E? extra});
 | 
					  S computeDefaultValue(
 | 
				
			||||||
 | 
					    BuildContext context, {
 | 
				
			||||||
  S? computeExtensionValueFn(
 | 
					 | 
				
			||||||
    BuildContext context,
 | 
					 | 
				
			||||||
    T themeExtension, {
 | 
					 | 
				
			||||||
    E? extra,
 | 
					    E? extra,
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /// Compute values from the extension if found
 | 
					  /// Compute extension value from custom component extension.
 | 
				
			||||||
  S? _computeExtensionValue(
 | 
					  S? computeExtensionValueFn(
 | 
				
			||||||
    BuildContext context, {
 | 
					    BuildContext context,
 | 
				
			||||||
 | 
					    T? themeExtension, {
 | 
				
			||||||
    E? extra,
 | 
					    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.
 | 
					  /// Choose most suitable style for a given context.
 | 
				
			||||||
  S negotiate(BuildContext context, {E? extra}) {
 | 
					  S negotiate(BuildContext context, {E? extra}) {
 | 
				
			||||||
    S style = computeDefaultValue(context, extra: extra);
 | 
					    final style = computeDefaultValue(context, extra: extra);
 | 
				
			||||||
    style =
 | 
					    return ThemeHelper.getThemeElement<S, S>(
 | 
				
			||||||
        style.mergeWith(_computeExtensionValue(context, extra: extra)) ?? style;
 | 
					          [
 | 
				
			||||||
    style =
 | 
					            style,
 | 
				
			||||||
        style.mergeWith(_computeCustomValue(context, extra: extra)) ?? style;
 | 
					            computeExtensionValueFn(
 | 
				
			||||||
    return style;
 | 
					              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 'multi_color.dart';
 | 
				
			||||||
export 'text_wrapper.dart';
 | 
					export 'text_wrapper.dart';
 | 
				
			||||||
 | 
					export 'theme_helper.dart';
 | 
				
			||||||
export 'theme_resolver.dart';
 | 
					export 'theme_resolver.dart';
 | 
				
			||||||
 | 
				
			|||||||
@ -80,33 +80,33 @@ class FileSelectionButtonThemeResolver extends ThemeResolver<
 | 
				
			|||||||
  @override
 | 
					  @override
 | 
				
			||||||
  FileSelectionButtonStyle? computeExtensionValueFn(
 | 
					  FileSelectionButtonStyle? computeExtensionValueFn(
 | 
				
			||||||
    BuildContext context,
 | 
					    BuildContext context,
 | 
				
			||||||
    FileSelectionButtonThemeExtension themeExtension, {
 | 
					    FileSelectionButtonThemeExtension? themeExtension, {
 | 
				
			||||||
    ButtonState? extra,
 | 
					    ButtonState? extra,
 | 
				
			||||||
  }) {
 | 
					  }) {
 | 
				
			||||||
    FileSelectionButtonStyle? style;
 | 
					    FileSelectionButtonStyle? style;
 | 
				
			||||||
    switch (extra?.state) {
 | 
					    switch (extra?.state) {
 | 
				
			||||||
      case ControlState.disabled:
 | 
					      case ControlState.disabled:
 | 
				
			||||||
        style = themeExtension.disabledStyle;
 | 
					        style = themeExtension?.disabledStyle;
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
      case ControlState.focused:
 | 
					      case ControlState.focused:
 | 
				
			||||||
        style = themeExtension.focusedStyle;
 | 
					        style = themeExtension?.focusedStyle;
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
      case ControlState.hovered:
 | 
					      case ControlState.hovered:
 | 
				
			||||||
        style = themeExtension.hoveredStyle;
 | 
					        style = themeExtension?.hoveredStyle;
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
      case ControlState.tapped:
 | 
					      case ControlState.tapped:
 | 
				
			||||||
        style = themeExtension.tappedStyle;
 | 
					        style = themeExtension?.tappedStyle;
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
      case ControlState.normal:
 | 
					      case ControlState.normal:
 | 
				
			||||||
      case null:
 | 
					      case null:
 | 
				
			||||||
        style = themeExtension.normalStyle;
 | 
					        style = themeExtension?.normalStyle;
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (extra?.isSelected ?? false) {
 | 
					    if (extra?.isSelected ?? false) {
 | 
				
			||||||
      style = themeExtension.selectedStyle;
 | 
					      style = themeExtension?.selectedStyle;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (extra?.isInvalid ?? false) {
 | 
					    if (extra?.isInvalid ?? false) {
 | 
				
			||||||
      style = themeExtension.invalidStyle;
 | 
					      style = themeExtension?.invalidStyle;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return style;
 | 
					    return style;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
				
			|||||||
@ -76,21 +76,21 @@ class FlatButtonThemeResolver extends ThemeResolver<FlatButtonStyle,
 | 
				
			|||||||
  @override
 | 
					  @override
 | 
				
			||||||
  FlatButtonStyle? computeExtensionValueFn(
 | 
					  FlatButtonStyle? computeExtensionValueFn(
 | 
				
			||||||
    BuildContext context,
 | 
					    BuildContext context,
 | 
				
			||||||
    FlatButtonThemeExtension themeExtension, {
 | 
					    FlatButtonThemeExtension? themeExtension, {
 | 
				
			||||||
    ControlState? extra,
 | 
					    ControlState? extra,
 | 
				
			||||||
  }) {
 | 
					  }) {
 | 
				
			||||||
    switch (extra) {
 | 
					    switch (extra) {
 | 
				
			||||||
      case ControlState.disabled:
 | 
					      case ControlState.disabled:
 | 
				
			||||||
        return themeExtension.disabledStyle;
 | 
					        return themeExtension?.disabledStyle;
 | 
				
			||||||
      case ControlState.focused:
 | 
					      case ControlState.focused:
 | 
				
			||||||
        return themeExtension.focusedStyle;
 | 
					        return themeExtension?.focusedStyle;
 | 
				
			||||||
      case ControlState.hovered:
 | 
					      case ControlState.hovered:
 | 
				
			||||||
        return themeExtension.hoveredStyle;
 | 
					        return themeExtension?.hoveredStyle;
 | 
				
			||||||
      case ControlState.tapped:
 | 
					      case ControlState.tapped:
 | 
				
			||||||
        return themeExtension.tappedStyle;
 | 
					        return themeExtension?.tappedStyle;
 | 
				
			||||||
      case ControlState.normal:
 | 
					      case ControlState.normal:
 | 
				
			||||||
      case null:
 | 
					      case null:
 | 
				
			||||||
        return themeExtension.normalStyle;
 | 
					        return themeExtension?.normalStyle;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -74,21 +74,21 @@ class SimpleIconButtonThemeResolver extends ThemeResolver<SimpleIconButtonStyle,
 | 
				
			|||||||
  @override
 | 
					  @override
 | 
				
			||||||
  SimpleIconButtonStyle? computeExtensionValueFn(
 | 
					  SimpleIconButtonStyle? computeExtensionValueFn(
 | 
				
			||||||
    BuildContext context,
 | 
					    BuildContext context,
 | 
				
			||||||
    SimpleIconButtonThemeExtension themeExtension, {
 | 
					    SimpleIconButtonThemeExtension? themeExtension, {
 | 
				
			||||||
    ControlState? extra,
 | 
					    ControlState? extra,
 | 
				
			||||||
  }) {
 | 
					  }) {
 | 
				
			||||||
    switch (extra) {
 | 
					    switch (extra) {
 | 
				
			||||||
      case ControlState.disabled:
 | 
					      case ControlState.disabled:
 | 
				
			||||||
        return themeExtension.disabledStyle;
 | 
					        return themeExtension?.disabledStyle;
 | 
				
			||||||
      case ControlState.focused:
 | 
					      case ControlState.focused:
 | 
				
			||||||
        return themeExtension.focusedStyle;
 | 
					        return themeExtension?.focusedStyle;
 | 
				
			||||||
      case ControlState.hovered:
 | 
					      case ControlState.hovered:
 | 
				
			||||||
        return themeExtension.hoveredStyle;
 | 
					        return themeExtension?.hoveredStyle;
 | 
				
			||||||
      case ControlState.tapped:
 | 
					      case ControlState.tapped:
 | 
				
			||||||
        return themeExtension.tappedStyle;
 | 
					        return themeExtension?.tappedStyle;
 | 
				
			||||||
      case ControlState.normal:
 | 
					      case ControlState.normal:
 | 
				
			||||||
      case null:
 | 
					      case null:
 | 
				
			||||||
        return themeExtension.normalStyle;
 | 
					        return themeExtension?.normalStyle;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -76,30 +76,30 @@ class SymbolButtonThemeResolver extends ThemeResolver<SymbolButtonStyle,
 | 
				
			|||||||
  @override
 | 
					  @override
 | 
				
			||||||
  SymbolButtonStyle? computeExtensionValueFn(
 | 
					  SymbolButtonStyle? computeExtensionValueFn(
 | 
				
			||||||
    BuildContext context,
 | 
					    BuildContext context,
 | 
				
			||||||
    SymbolButtonThemeExtension themeExtension, {
 | 
					    SymbolButtonThemeExtension? themeExtension, {
 | 
				
			||||||
    ButtonState? extra,
 | 
					    ButtonState? extra,
 | 
				
			||||||
  }) {
 | 
					  }) {
 | 
				
			||||||
    SymbolButtonStyle? style;
 | 
					    SymbolButtonStyle? style;
 | 
				
			||||||
    switch (extra?.state) {
 | 
					    switch (extra?.state) {
 | 
				
			||||||
      case ControlState.disabled:
 | 
					      case ControlState.disabled:
 | 
				
			||||||
        style = themeExtension.disabledStyle;
 | 
					        style = themeExtension?.disabledStyle;
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
      case ControlState.focused:
 | 
					      case ControlState.focused:
 | 
				
			||||||
        style = themeExtension.focusedStyle;
 | 
					        style = themeExtension?.focusedStyle;
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
      case ControlState.hovered:
 | 
					      case ControlState.hovered:
 | 
				
			||||||
        style = themeExtension.hoveredStyle;
 | 
					        style = themeExtension?.hoveredStyle;
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
      case ControlState.tapped:
 | 
					      case ControlState.tapped:
 | 
				
			||||||
        style = themeExtension.tappedStyle;
 | 
					        style = themeExtension?.tappedStyle;
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
      case ControlState.normal:
 | 
					      case ControlState.normal:
 | 
				
			||||||
      case null:
 | 
					      case null:
 | 
				
			||||||
        style = themeExtension.normalStyle;
 | 
					        style = themeExtension?.normalStyle;
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (extra?.isSelected ?? false) {
 | 
					    if (extra?.isSelected ?? false) {
 | 
				
			||||||
      style = themeExtension.selectedStyle;
 | 
					      style = themeExtension?.selectedStyle;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return style;
 | 
					    return style;
 | 
				
			||||||
 | 
				
			|||||||
@ -48,11 +48,11 @@ class LoaderThemeResolver
 | 
				
			|||||||
  @override
 | 
					  @override
 | 
				
			||||||
  LoaderStyle? computeExtensionValueFn(
 | 
					  LoaderStyle? computeExtensionValueFn(
 | 
				
			||||||
    BuildContext context,
 | 
					    BuildContext context,
 | 
				
			||||||
    LoaderThemeExtension themeExtension, {
 | 
					    LoaderThemeExtension? themeExtension, {
 | 
				
			||||||
    void extra,
 | 
					    void extra,
 | 
				
			||||||
  }) =>
 | 
					  }) =>
 | 
				
			||||||
      LoaderStyle(
 | 
					      LoaderStyle(
 | 
				
			||||||
        colors: themeExtension.colors,
 | 
					        colors: themeExtension?.colors,
 | 
				
			||||||
        stroke: themeExtension.stroke,
 | 
					        stroke: themeExtension?.stroke,
 | 
				
			||||||
      );
 | 
					      );
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -43,11 +43,11 @@ class RichTextBuilderThemeResolver extends ThemeResolver<RichTextBuilderStyle,
 | 
				
			|||||||
  @override
 | 
					  @override
 | 
				
			||||||
  RichTextBuilderStyle? computeExtensionValueFn(
 | 
					  RichTextBuilderStyle? computeExtensionValueFn(
 | 
				
			||||||
    BuildContext context,
 | 
					    BuildContext context,
 | 
				
			||||||
    RichTextBuilderThemeExtension themeExtension, {
 | 
					    RichTextBuilderThemeExtension? themeExtension, {
 | 
				
			||||||
    void extra,
 | 
					    void extra,
 | 
				
			||||||
  }) =>
 | 
					  }) =>
 | 
				
			||||||
      RichTextBuilderStyle(
 | 
					      RichTextBuilderStyle(
 | 
				
			||||||
        defaultStyle: themeExtension.defaultStyle,
 | 
					        defaultStyle: themeExtension?.defaultStyle,
 | 
				
			||||||
        styles: themeExtension.styles,
 | 
					        styles: themeExtension?.styles,
 | 
				
			||||||
      );
 | 
					      );
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -118,19 +118,19 @@ class TextInputThemeResolver extends ThemeResolver<TextInputStyle,
 | 
				
			|||||||
  @override
 | 
					  @override
 | 
				
			||||||
  TextInputStyle? computeExtensionValueFn(
 | 
					  TextInputStyle? computeExtensionValueFn(
 | 
				
			||||||
    BuildContext context,
 | 
					    BuildContext context,
 | 
				
			||||||
    TextInputThemeExtension themeExtension, {
 | 
					    TextInputThemeExtension? themeExtension, {
 | 
				
			||||||
    TextInputState? extra,
 | 
					    TextInputState? extra,
 | 
				
			||||||
  }) {
 | 
					  }) {
 | 
				
			||||||
    TextInputStyle? textInputStyle;
 | 
					    TextInputStyle? textInputStyle;
 | 
				
			||||||
    switch (extra?.controlState) {
 | 
					    switch (extra?.controlState) {
 | 
				
			||||||
      case ControlState.focused:
 | 
					      case ControlState.focused:
 | 
				
			||||||
        textInputStyle = themeExtension.focusedStyle;
 | 
					        textInputStyle = themeExtension?.focusedStyle;
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
      case ControlState.disabled:
 | 
					      case ControlState.disabled:
 | 
				
			||||||
        textInputStyle = themeExtension.disableStyle;
 | 
					        textInputStyle = themeExtension?.disableStyle;
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
      case ControlState.normal:
 | 
					      case ControlState.normal:
 | 
				
			||||||
        textInputStyle = themeExtension.normalStyle;
 | 
					        textInputStyle = themeExtension?.normalStyle;
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
      case ControlState.hovered:
 | 
					      case ControlState.hovered:
 | 
				
			||||||
      case ControlState.tapped:
 | 
					      case ControlState.tapped:
 | 
				
			||||||
@ -141,7 +141,7 @@ class TextInputThemeResolver extends ThemeResolver<TextInputStyle,
 | 
				
			|||||||
    TextInputStyle? style;
 | 
					    TextInputStyle? style;
 | 
				
			||||||
    switch (extra?.statusState) {
 | 
					    switch (extra?.statusState) {
 | 
				
			||||||
      case StatusState.error:
 | 
					      case StatusState.error:
 | 
				
			||||||
        style = themeExtension.errorStyle;
 | 
					        style = themeExtension?.errorStyle;
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
      case StatusState.initial:
 | 
					      case StatusState.initial:
 | 
				
			||||||
      case StatusState.success:
 | 
					      case StatusState.success:
 | 
				
			||||||
 | 
				
			|||||||
@ -15,4 +15,3 @@
 | 
				
			|||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
 | 
					// along with this program. If not, see <https://www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export './linear_gradient_helper.dart';
 | 
					export './linear_gradient_helper.dart';
 | 
				
			||||||
export './theme_helper.dart';
 | 
					 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user