fix(ui): move theme_resolver in compnoent to fix code generation
This commit is contained in:
+5
-3
@@ -40,7 +40,7 @@ class FileSelectionButton extends FileSelectionButtonComponent
|
||||
super.invalidStyle,
|
||||
super.onPressed,
|
||||
super.mainAxisSize,
|
||||
this.themeResolver,
|
||||
super.themeResolver,
|
||||
super.key,
|
||||
});
|
||||
|
||||
@@ -76,8 +76,10 @@ class FileSelectionButton extends FileSelectionButtonComponent
|
||||
@override
|
||||
FileSelectionButtonStyle? get invalidStyle =>
|
||||
super.invalidStyle as FileSelectionButtonStyle?;
|
||||
|
||||
final FileSelectionButtonThemeResolver? themeResolver;
|
||||
|
||||
@override
|
||||
FileSelectionButtonThemeResolver? get themeResolver =>
|
||||
super.themeResolver as FileSelectionButtonThemeResolver?;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => exportBloc(
|
||||
|
||||
+5
@@ -45,6 +45,10 @@ class $FileSelectionButtonCWProxyImpl
|
||||
FileSelectionButton onPressed(void Function(ControlState)? onPressed) =>
|
||||
this(onPressed: onPressed);
|
||||
@override
|
||||
FileSelectionButton themeResolver(
|
||||
ThemeResolver<dynamic, dynamic, dynamic>? themeResolver) =>
|
||||
this(themeResolver: themeResolver);
|
||||
@override
|
||||
FileSelectionButton key(Key? key) => this(key: key);
|
||||
@override
|
||||
FileSelectionButton call({
|
||||
@@ -60,6 +64,7 @@ class $FileSelectionButtonCWProxyImpl
|
||||
ButtonStyle<dynamic>? selectedStyle,
|
||||
ButtonStyle<dynamic>? invalidStyle,
|
||||
void Function(ControlState)? onPressed,
|
||||
ThemeResolver<dynamic, dynamic, dynamic>? themeResolver,
|
||||
Key? key,
|
||||
}) =>
|
||||
FileSelectionButton(
|
||||
|
||||
-1
@@ -17,7 +17,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wyatt_ui_components/wyatt_wyatt_ui_components.dart';
|
||||
import 'package:wyatt_ui_kit/src/components/buttons/cubit/button_cubit.dart';
|
||||
import 'package:wyatt_ui_kit/src/core/helpers/theme_resolver.dart';
|
||||
import 'package:wyatt_ui_kit/wyatt_ui_kit.dart';
|
||||
|
||||
class FileSelectionButtonThemeResolver extends ThemeResolver<
|
||||
|
||||
@@ -38,7 +38,7 @@ class FlatButton extends FlatButtonComponent
|
||||
super.tappedStyle,
|
||||
super.onPressed,
|
||||
super.mainAxisSize,
|
||||
this.themeResolver,
|
||||
super.themeResolver,
|
||||
super.key,
|
||||
});
|
||||
|
||||
@@ -62,7 +62,9 @@ class FlatButton extends FlatButtonComponent
|
||||
@override
|
||||
FlatButtonStyle? get tappedStyle => super.tappedStyle as FlatButtonStyle?;
|
||||
|
||||
final FlatButtonThemeResolver? themeResolver;
|
||||
@override
|
||||
FlatButtonThemeResolver? get themeResolver =>
|
||||
super.themeResolver as FlatButtonThemeResolver?;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => exportBloc(
|
||||
|
||||
@@ -37,6 +37,10 @@ class $FlatButtonCWProxyImpl implements $FlatButtonComponentCWProxy {
|
||||
FlatButton onPressed(void Function(ControlState)? onPressed) =>
|
||||
this(onPressed: onPressed);
|
||||
@override
|
||||
FlatButton themeResolver(
|
||||
ThemeResolver<dynamic, dynamic, dynamic>? themeResolver) =>
|
||||
this(themeResolver: themeResolver);
|
||||
@override
|
||||
FlatButton key(Key? key) => this(key: key);
|
||||
@override
|
||||
FlatButton call({
|
||||
@@ -50,6 +54,7 @@ class $FlatButtonCWProxyImpl implements $FlatButtonComponentCWProxy {
|
||||
ButtonStyle<dynamic>? focusedStyle,
|
||||
ButtonStyle<dynamic>? tappedStyle,
|
||||
void Function(ControlState)? onPressed,
|
||||
ThemeResolver<dynamic, dynamic, dynamic>? themeResolver,
|
||||
Key? key,
|
||||
}) =>
|
||||
FlatButton(
|
||||
|
||||
+1
-3
@@ -16,7 +16,6 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wyatt_ui_components/wyatt_wyatt_ui_components.dart';
|
||||
import 'package:wyatt_ui_kit/src/core/helpers/theme_resolver.dart';
|
||||
import 'package:wyatt_ui_kit/wyatt_ui_kit.dart';
|
||||
|
||||
class FlatButtonThemeResolver extends ThemeResolver<FlatButtonStyle,
|
||||
@@ -26,9 +25,8 @@ class FlatButtonThemeResolver extends ThemeResolver<FlatButtonStyle,
|
||||
required this.customStyleFn,
|
||||
});
|
||||
|
||||
@override
|
||||
|
||||
/// Values taken from <https://api.flutter.dev/flutter/material/ElevatedButton/defaultStyleOf.html>
|
||||
@override
|
||||
FlatButtonStyle computeDefaultValue(
|
||||
BuildContext context, {
|
||||
ControlState? extra,
|
||||
|
||||
+4
-2
@@ -35,7 +35,7 @@ class SimpleIconButton extends SimpleIconButtonComponent
|
||||
super.focusedStyle,
|
||||
super.tappedStyle,
|
||||
super.onPressed,
|
||||
this.themeResolver,
|
||||
super.themeResolver,
|
||||
super.key,
|
||||
});
|
||||
|
||||
@@ -64,7 +64,9 @@ class SimpleIconButton extends SimpleIconButtonComponent
|
||||
SimpleIconButtonStyle? get tappedStyle =>
|
||||
super.tappedStyle as SimpleIconButtonStyle?;
|
||||
|
||||
final SimpleIconButtonThemeResolver? themeResolver;
|
||||
@override
|
||||
SimpleIconButtonThemeResolver? get themeResolver =>
|
||||
super.themeResolver as SimpleIconButtonThemeResolver?;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => exportBloc(
|
||||
|
||||
+5
@@ -31,6 +31,10 @@ class $SimpleIconButtonCWProxyImpl
|
||||
SimpleIconButton onPressed(void Function(ControlState)? onPressed) =>
|
||||
this(onPressed: onPressed);
|
||||
@override
|
||||
SimpleIconButton themeResolver(
|
||||
ThemeResolver<dynamic, dynamic, dynamic>? themeResolver) =>
|
||||
this(themeResolver: themeResolver);
|
||||
@override
|
||||
SimpleIconButton key(Key? key) => this(key: key);
|
||||
@override
|
||||
SimpleIconButton call({
|
||||
@@ -41,6 +45,7 @@ class $SimpleIconButtonCWProxyImpl
|
||||
ButtonStyle<dynamic>? focusedStyle,
|
||||
ButtonStyle<dynamic>? tappedStyle,
|
||||
void Function(ControlState)? onPressed,
|
||||
ThemeResolver<dynamic, dynamic, dynamic>? themeResolver,
|
||||
Key? key,
|
||||
}) =>
|
||||
SimpleIconButton(
|
||||
|
||||
-1
@@ -16,7 +16,6 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wyatt_ui_components/wyatt_wyatt_ui_components.dart';
|
||||
import 'package:wyatt_ui_kit/src/core/helpers/theme_resolver.dart';
|
||||
import 'package:wyatt_ui_kit/wyatt_ui_kit.dart';
|
||||
|
||||
class SimpleIconButtonThemeResolver extends ThemeResolver<SimpleIconButtonStyle,
|
||||
|
||||
@@ -37,7 +37,7 @@ class SymbolButton extends SymbolButtonComponent
|
||||
super.tappedStyle,
|
||||
super.selectedStyle,
|
||||
super.mainAxisSize,
|
||||
this.themeResolver,
|
||||
super.themeResolver,
|
||||
super.onPressed,
|
||||
super.key,
|
||||
});
|
||||
@@ -69,7 +69,9 @@ class SymbolButton extends SymbolButtonComponent
|
||||
SymbolButtonStyle? get selectedStyle =>
|
||||
super.selectedStyle as SymbolButtonStyle?;
|
||||
|
||||
final SymbolButtonThemeResolver? themeResolver;
|
||||
@override
|
||||
SymbolButtonThemeResolver? get themeResolver =>
|
||||
super.themeResolver as SymbolButtonThemeResolver?;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => exportBloc(
|
||||
|
||||
@@ -38,6 +38,10 @@ class $SymbolButtonCWProxyImpl implements $SymbolButtonComponentCWProxy {
|
||||
SymbolButton onPressed(void Function(ControlState)? onPressed) =>
|
||||
this(onPressed: onPressed);
|
||||
@override
|
||||
SymbolButton themeResolver(
|
||||
ThemeResolver<dynamic, dynamic, dynamic>? themeResolver) =>
|
||||
this(themeResolver: themeResolver);
|
||||
@override
|
||||
SymbolButton key(Key? key) => this(key: key);
|
||||
@override
|
||||
SymbolButton call({
|
||||
@@ -51,6 +55,7 @@ class $SymbolButtonCWProxyImpl implements $SymbolButtonComponentCWProxy {
|
||||
ButtonStyle<dynamic>? tappedStyle,
|
||||
ButtonStyle<dynamic>? selectedStyle,
|
||||
void Function(ControlState)? onPressed,
|
||||
ThemeResolver<dynamic, dynamic, dynamic>? themeResolver,
|
||||
Key? key,
|
||||
}) =>
|
||||
SymbolButton(
|
||||
|
||||
+37
-12
@@ -17,7 +17,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wyatt_ui_components/wyatt_wyatt_ui_components.dart';
|
||||
import 'package:wyatt_ui_kit/src/components/buttons/cubit/button_cubit.dart';
|
||||
import 'package:wyatt_ui_kit/src/core/helpers/theme_resolver.dart';
|
||||
import 'package:wyatt_ui_kit/wyatt_ui_kit.dart';
|
||||
|
||||
class SymbolButtonThemeResolver extends ThemeResolver<SymbolButtonStyle,
|
||||
@@ -31,17 +30,43 @@ class SymbolButtonThemeResolver extends ThemeResolver<SymbolButtonStyle,
|
||||
SymbolButtonStyle computeDefaultValue(
|
||||
BuildContext context, {
|
||||
ButtonState? extra,
|
||||
}) =>
|
||||
SymbolButtonStyle(
|
||||
label: context.textTheme.labelLarge,
|
||||
dimension: context.buttonTheme.height,
|
||||
radius: (context.buttonTheme.shape is RoundedRectangleBorder)
|
||||
? (context.buttonTheme.shape as RoundedRectangleBorder).borderRadius
|
||||
: null,
|
||||
padding: context.buttonTheme.padding,
|
||||
foregroundColors: MultiColor.single(context.colorScheme.onPrimary),
|
||||
backgroundColors: MultiColor.single(context.colorScheme.primary),
|
||||
);
|
||||
}) {
|
||||
MultiColor backgroundColor = MultiColor.single(context.colorScheme.primary);
|
||||
MultiColor foregroundColor =
|
||||
MultiColor.single(context.colorScheme.onPrimary);
|
||||
|
||||
switch (extra?.state) {
|
||||
case ControlState.disabled:
|
||||
backgroundColor =
|
||||
MultiColor.single(context.colorScheme.onSurface.withOpacity(0.12));
|
||||
foregroundColor =
|
||||
MultiColor.single(context.colorScheme.onSurface.withOpacity(0.38));
|
||||
break;
|
||||
case ControlState.hovered:
|
||||
backgroundColor =
|
||||
MultiColor.single(context.colorScheme.primary.withOpacity(0.92));
|
||||
break;
|
||||
case ControlState.tapped:
|
||||
backgroundColor =
|
||||
MultiColor.single(context.colorScheme.primary.withOpacity(0.92));
|
||||
break;
|
||||
case null:
|
||||
case ControlState.normal:
|
||||
case ControlState.focused:
|
||||
break;
|
||||
}
|
||||
|
||||
return SymbolButtonStyle(
|
||||
label: context.textTheme.labelLarge,
|
||||
dimension: context.buttonTheme.height,
|
||||
radius: (context.buttonTheme.shape is RoundedRectangleBorder)
|
||||
? (context.buttonTheme.shape as RoundedRectangleBorder).borderRadius
|
||||
: null,
|
||||
padding: context.buttonTheme.padding,
|
||||
foregroundColors: foregroundColor,
|
||||
backgroundColors: backgroundColor,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
final SymbolButtonStyle? Function(
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
// 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';
|
||||
import 'package:wyatt_ui_kit/src/core/extensions/theme_extensions.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) => context.themeExtension<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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user