Skip to content

Commit

Permalink
Added FButton.icon for a single icon button
Browse files Browse the repository at this point in the history
  • Loading branch information
John Doe authored and Pante committed Aug 4, 2024
1 parent e70ca87 commit 81c8492
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
30 changes: 25 additions & 5 deletions forui/lib/src/widgets/button/button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class FButton extends StatelessWidget {
@useResult
static _Data _of(BuildContext context) {
final theme = context.dependOnInheritedWidgetOfExactType<_InheritedData>();
return theme?.data ?? (style: context.theme.buttonStyles.primary, enabled: true);
return theme?.data ??
(style: context.theme.buttonStyles.primary, enabled: true);
}

/// The style. Defaults to [FButtonStyle.primary].
Expand Down Expand Up @@ -99,6 +100,18 @@ class FButton extends StatelessWidget {
label: label,
);

/// Creates a [FButton] that contains only an [icon]
FButton.icon({
required this.onPress,
required Widget icon,
this.style = Variant.outline,
this.onLongPress,
this.autofocus = false,
this.focusNode,
this.onFocusChange,
super.key,
}) : child = _FButtonIconContent(icon: icon);

/// Creates a [FButton] with custom content.
const FButton.raw({
required this.onPress,
Expand Down Expand Up @@ -129,7 +142,8 @@ class FButton extends StatelessWidget {
onPress: onPress,
onLongPress: onLongPress,
child: DecoratedBox(
decoration: enabled ? style.enabledBoxDecoration : style.disabledBoxDecoration,
decoration:
enabled ? style.enabledBoxDecoration : style.disabledBoxDecoration,
child: _InheritedData(
data: (style: style, enabled: enabled),
child: child,
Expand All @@ -145,7 +159,8 @@ class FButton extends StatelessWidget {
..add(DiagnosticsProperty('style', style))
..add(DiagnosticsProperty('onPress', onPress))
..add(DiagnosticsProperty('onLongPress', onLongPress))
..add(FlagProperty('autofocus', value: autofocus, defaultValue: false, ifTrue: 'autofocus'))
..add(FlagProperty('autofocus',
value: autofocus, defaultValue: false, ifTrue: 'autofocus'))
..add(DiagnosticsProperty('focusNode', focusNode))
..add(DiagnosticsProperty('onFocusChange', onFocusChange))
..add(DiagnosticsProperty('builder', child));
Expand Down Expand Up @@ -233,7 +248,8 @@ class FButtonCustomStyle extends FButtonStyle with Diagnosticable {
}) =>
FButtonCustomStyle(
enabledBoxDecoration: enabledBoxDecoration ?? this.enabledBoxDecoration,
disabledBoxDecoration: disabledBoxDecoration ?? this.disabledBoxDecoration,
disabledBoxDecoration:
disabledBoxDecoration ?? this.disabledBoxDecoration,
content: content ?? this.content,
icon: icon ?? this.icon,
);
Expand All @@ -259,7 +275,11 @@ class FButtonCustomStyle extends FButtonStyle with Diagnosticable {
icon == other.icon;

@override
int get hashCode => enabledBoxDecoration.hashCode ^ disabledBoxDecoration.hashCode ^ content.hashCode ^ icon.hashCode;
int get hashCode =>
enabledBoxDecoration.hashCode ^
disabledBoxDecoration.hashCode ^
content.hashCode ^
icon.hashCode;
}

typedef _Data = ({FButtonCustomStyle style, bool enabled});
Expand Down
23 changes: 21 additions & 2 deletions forui/lib/src/widgets/button/button_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ final class _FButtonContent extends StatelessWidget {
return Padding(
padding: style.content.padding,
child: DefaultTextStyle.merge(
style: enabled ? style.content.enabledTextStyle : style.content.disabledTextStyle,
style: enabled
? style.content.enabledTextStyle
: style.content.disabledTextStyle,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: separate(
Expand All @@ -37,6 +39,22 @@ final class _FButtonContent extends StatelessWidget {
}
}

final class _FButtonIconContent extends StatelessWidget {
final Widget icon;

const _FButtonIconContent({required this.icon});

@override
Widget build(BuildContext context) {
final (:style, enabled: _) = FButton._of(context);

return Padding(
padding: style.content.padding,
child: icon,
);
}
}

/// [FButton] content's style.
class FButtonContentStyle with Diagnosticable {
/// The [TextStyle] when this button is enabled.
Expand Down Expand Up @@ -121,5 +139,6 @@ class FButtonContentStyle with Diagnosticable {
padding == other.padding;

@override
int get hashCode => enabledTextStyle.hashCode ^ disabledTextStyle.hashCode ^ padding.hashCode;
int get hashCode =>
enabledTextStyle.hashCode ^ disabledTextStyle.hashCode ^ padding.hashCode;
}

0 comments on commit 81c8492

Please sign in to comment.