fix(ui): move theme_resolver in compnoent to fix code generation

This commit is contained in:
2023-02-15 20:35:25 +01:00
parent 96781880f4
commit ed06e18043
20 changed files with 104 additions and 30 deletions
@@ -18,5 +18,4 @@ export 'enums/control_state.dart';
export 'extensions/build_context_extensions.dart';
export 'extensions/string_extension.dart';
export 'mixins/copy_with_mixin.dart';
export 'utils/multi_color.dart';
export 'utils/text_wrapper.dart';
export 'utils/utils.dart';
@@ -0,0 +1,83 @@
// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This 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.
//
// This 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 this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter/material.dart';
/// {@template theme_resolver}
/// In charge of theme negotiation and merge.
///
/// When you build a component `Component({double? radius})`.
/// You have several possibilities:
/// 1) Pass the "radius" into the constructor, `Component(radius: 12)`.
/// 2) Set up a theme extension `ComponentThemeExtension(radius: 15)`.
/// 3) Let `wyatt_ui_kit` "negotiate" and try to find a suitable style in the
/// flutter theme.
///
/// If this negotiation phase fails, then:
/// - If the value is mandatory: a hardcoded value in "wyatt_ui_kit" is chosen.
/// - If not, the style is simply not applied.
/// {@endtemplate}
abstract class ThemeResolver<S, T, E> {
/// {@macro theme_resolver}
const ThemeResolver();
S? Function(
BuildContext context,
S defaultValue,
T themeExtension, {
E? extra,
}) get computeExtensionValueFn;
S? Function(BuildContext context, {E? extra}) get customStyleFn;
/// Compute default value from Flutter Theme or with hardcoded values.
S computeDefaultValue(BuildContext context, {E? extra});
/// Compute values from the extension if found
S? computeExtensionValue(
BuildContext context,
S defaultValue, {
E? extra,
}) {
final themeExtension = findExtension(context);
if (themeExtension != null) {
return computeExtensionValueFn(context, defaultValue, themeExtension);
}
return defaultValue;
}
/// Compute custom value
S? computeCustomValue(
BuildContext context,
S previousPhaseValue, {
E? extra,
}) {
final customStyle = customStyleFn(context, extra: extra);
if (customStyle != null) {
return customStyle;
}
return previousPhaseValue;
}
T? findExtension(BuildContext context) => Theme.of(context).extension<T>();
/// Choose most suitable style for a given context.
S negotiate(BuildContext context, {E? extra}) {
S style = computeDefaultValue(context, extra: extra);
style = computeExtensionValue(context, style, extra: extra) ?? style;
style = computeCustomValue(context, style, extra: extra) ?? style;
return style;
}
}
@@ -0,0 +1,19 @@
// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This 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.
//
// This 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 this program. If not, see <https://www.gnu.org/licenses/>.
export 'multi_color.dart';
export 'text_wrapper.dart';
export 'theme_resolver.dart';
@@ -26,6 +26,7 @@ abstract class ButtonComponent extends Component {
this.selectedStyle,
this.invalidStyle,
this.onPressed,
this.themeResolver,
super.key,
});
@@ -52,4 +53,7 @@ abstract class ButtonComponent extends Component {
/// Callback on button press
final void Function(ControlState state)? onPressed;
/// Theme Resolver for this component
final ThemeResolver<dynamic, dynamic, dynamic>? themeResolver;
}
@@ -36,6 +36,7 @@ abstract class FileSelectionButtonComponent extends ButtonComponent
super.selectedStyle,
super.invalidStyle,
super.onPressed,
super.themeResolver,
super.key,
});
@@ -34,6 +34,7 @@ abstract class FlatButtonComponent extends ButtonComponent
super.focusedStyle,
super.tappedStyle,
super.onPressed,
super.themeResolver,
super.key,
});
@@ -31,6 +31,7 @@ abstract class SimpleIconButtonComponent extends ButtonComponent
super.focusedStyle,
super.tappedStyle,
super.onPressed,
super.themeResolver,
super.key,
});
@@ -34,6 +34,7 @@ abstract class SymbolButtonComponent extends ButtonComponent
super.tappedStyle,
super.selectedStyle,
super.onPressed,
super.themeResolver,
super.key,
});