Files
SpotiFLAC-Mobile/lib/widgets/expressive_button.dart
T
zarzet 9b0ef32cf6 fix(theme): show the app font in titles, tabs, snackbars and custom buttons
A component that is handed its own text style takes nothing from the text
theme, so a style written without a family fell back to the system font.
On Android that showed Roboto beside the app's typeface: every Material
app bar title (Now Playing among them), tab labels, snackbars, and the
selection and concert action buttons, whose button style sets a size and
weight.

The theme's own styles now name the family, with the brand roundness on
the title roles, and ExpressiveButton gives a caller's text style the
theme's family when it names none. An imported font is followed the same
way, and the system font setting still leaves the family unset.

Reported in issue 941.
2026-11-21 01:26:32 +07:00

135 lines
4.2 KiB
Dart

import 'package:material_ui/material_ui.dart';
import 'package:material_3_expressive/material_3_expressive.dart';
import 'package:mornye/theme/material_expressive.dart';
import 'package:mornye/widgets/app_icon.dart';
/// Expressive actions retain caller colors, disabled states and flexible labels.
class ExpressiveButton extends StatelessWidget {
const ExpressiveButton({
super.key,
required this.onPressed,
required this.child,
this.icon,
this.style,
this.outlined = false,
this.tonal = false,
this.text = false,
});
final VoidCallback? onPressed;
final Widget child;
final Widget? icon;
final ButtonStyle? style;
final bool outlined;
final bool tonal;
/// Low-emphasis action without a container, like [TextButton].
final bool text;
/// [style] with its text style in the theme's font: a button's own text
/// style replaces the theme's label style whole, family included.
static ButtonStyle? _inThemeFont(BuildContext context, ButtonStyle? style) {
final text = style?.textStyle;
final family = Theme.of(context).textTheme.labelLarge?.fontFamily;
if (style == null || text == null || family == null) return style;
return style.copyWith(
textStyle: WidgetStateProperty.resolveWith((states) {
final resolved = text.resolve(states);
return resolved == null || resolved.fontFamily != null
? resolved
: resolved.copyWith(fontFamily: family);
}),
);
}
@override
Widget build(BuildContext context) {
final icon = this.icon == null ? null : symbolIconWidget(this.icon!);
final style = _inThemeFont(context, this.style);
if (!materialExpressiveEnabled(context)) {
if (icon != null) {
// Keep the standard icon spacing and asymmetric padding.
if (text) {
return TextButton.icon(
onPressed: onPressed,
style: style,
icon: icon,
label: child,
);
}
if (outlined) {
return OutlinedButton.icon(
onPressed: onPressed,
style: style,
icon: icon,
label: child,
);
}
if (tonal) {
return FilledButton.tonalIcon(
onPressed: onPressed,
style: style,
icon: icon,
label: child,
);
}
return FilledButton.icon(
onPressed: onPressed,
style: style,
icon: icon,
label: child,
);
}
if (text) {
return TextButton(onPressed: onPressed, style: style, child: child);
}
if (outlined) {
return OutlinedButton(onPressed: onPressed, style: style, child: child);
}
if (tonal) {
return FilledButton.tonal(
onPressed: onPressed,
style: style,
child: child,
);
}
return FilledButton(onPressed: onPressed, style: style, child: child);
}
final content = Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (icon != null) ...[icon, const SizedBox(width: 8)],
Flexible(child: child),
],
);
final states = <WidgetState>{if (onPressed == null) WidgetState.disabled};
return MaterialExpressiveScope(
child: M3EButton(
onPressed: onPressed,
style: text
? M3EButtonStyle.text
: outlined
? M3EButtonStyle.outlined
: tonal
? M3EButtonStyle.tonal
: M3EButtonStyle.filled,
decoration: M3EButtonDecoration(
backgroundColor: style?.backgroundColor,
foregroundColor: style?.foregroundColor,
overlayColor: style?.overlayColor,
side: style?.side,
minimumSize:
style?.minimumSize?.resolve(states) ?? const Size(48, 48),
maximumSize: style?.maximumSize?.resolve(states),
fixedSize: style?.fixedSize?.resolve(states),
padding: style?.padding?.resolve(states),
textStyle: style?.textStyle?.resolve(states),
iconSize: style?.iconSize?.resolve(states),
),
child: content,
),
);
}
}