LCOV - code coverage report
Current view: top level - theme - foss_theme.dart Coverage Total Hit
Test: fossui coverage Lines: 100.0 % 94 94
Test Date: 2026-11-29 09:15:47 Functions: - 0 0

            Line data    Source code
       1              : // ThemeExtension has no home outside material.dart on the supported SDKs.
       2              : // ignore: deprecated_member_use
       3              : import 'package:flutter/material.dart' show Theme, ThemeData, ThemeExtension;
       4              : import 'package:flutter/widgets.dart';
       5              : import 'package:fossui/src/theme/colors/foss_colors.dart';
       6              : import 'package:fossui/src/theme/foss_theme_spec.dart';
       7              : import 'package:fossui/src/theme/motion/foss_motion.dart';
       8              : import 'package:fossui/src/theme/radii/foss_radii.dart';
       9              : import 'package:fossui/src/theme/shadows/foss_shadows.dart';
      10              : import 'package:fossui/src/theme/spacing/foss_spacing.dart';
      11              : import 'package:fossui/src/theme/typography/foss_typography.dart';
      12              : 
      13              : /// The root token bundle: the six leaf bundles in one object. Register it once,
      14              : /// then read everything through `context.fossTheme`. [FossThemeData.light] and
      15              : /// [FossThemeData.dark] are the defaults; build your own to retheme the app.
      16              : ///
      17              : /// {@template foss.customize}
      18              : /// Customize globally by passing your own [FossThemeData], or per instance
      19              : /// through the widget's `style` object. There are no per-instance token props:
      20              : /// to change color, radius, or spacing, change the theme.
      21              : /// {@endtemplate}
      22              : ///
      23              : /// ```dart
      24              : /// MaterialApp(
      25              : ///   theme: FossThemeData.light.toThemeData(),
      26              : ///   darkTheme: FossThemeData.dark.toThemeData(),
      27              : /// );
      28              : /// ```
      29              : @immutable
      30              : class FossThemeData extends ThemeExtension<FossThemeData> {
      31              :   /// Creates a theme from explicit bundles. Prefer the named defaults.
      32            4 :   const FossThemeData({
      33              :     required this.colors,
      34              :     required this.radii,
      35              :     required this.spacing,
      36              :     required this.typography,
      37              :     required this.shadows,
      38              :     required this.motion,
      39              :   });
      40              : 
      41              :   /// The default light theme.
      42              :   static const light = FossThemeData(
      43              :     colors: FossColors.light,
      44              :     radii: FossRadii.standard,
      45              :     spacing: FossSpacing.standard,
      46              :     typography: FossTypography.standard,
      47              :     shadows: FossShadows.standard,
      48              :     motion: FossMotion.standard,
      49              :   );
      50              : 
      51              :   /// The default dark theme (light, with the dark color set).
      52              :   static const dark = FossThemeData(
      53              :     colors: FossColors.dark,
      54              :     radii: FossRadii.standard,
      55              :     spacing: FossSpacing.standard,
      56              :     typography: FossTypography.standard,
      57              :     shadows: FossShadows.standard,
      58              :     motion: FossMotion.standard,
      59              :   );
      60              : 
      61              :   /// Semantic color roles.
      62              :   final FossColors colors;
      63              : 
      64              :   /// Corner radii.
      65              :   final FossRadii radii;
      66              : 
      67              :   /// Spacing scale.
      68              :   final FossSpacing spacing;
      69              : 
      70              :   /// Text styles.
      71              :   final FossTypography typography;
      72              : 
      73              :   /// Elevation shadows.
      74              :   final FossShadows shadows;
      75              : 
      76              :   /// Animation durations.
      77              :   final FossMotion motion;
      78              : 
      79              :   /// Layers [spec] over this theme, returning a rethemed copy. Each unset spec
      80              :   /// field keeps this theme's value; colors pass through enumerated, while
      81              :   /// radius, spacing, shadow color, and font family seed their scales. Start
      82              :   /// from [light] or [dark].
      83              :   ///
      84              :   /// ```dart
      85              :   /// final theme = FossThemeData.light.retheme(
      86              :   ///   const FossThemeSpec(primary: Color(0xFF51F0A8), radius: 22),
      87              :   /// );
      88              :   /// ```
      89            1 :   FossThemeData retheme(FossThemeSpec spec) {
      90            1 :     final radius = spec.radius;
      91            1 :     final unit = spec.spacing;
      92            1 :     final shadowColor = spec.shadowColor;
      93            1 :     final fontFamily = spec.fontFamily;
      94            1 :     return FossThemeData(
      95            2 :       colors: colors.copyWith(
      96            1 :         background: spec.background,
      97            1 :         foreground: spec.foreground,
      98            1 :         card: spec.card,
      99            1 :         cardForeground: spec.cardForeground,
     100            1 :         popover: spec.popover,
     101            1 :         popoverForeground: spec.popoverForeground,
     102            1 :         primary: spec.primary,
     103            1 :         primaryForeground: spec.primaryForeground,
     104            1 :         secondary: spec.secondary,
     105            1 :         secondaryForeground: spec.secondaryForeground,
     106            1 :         muted: spec.muted,
     107            1 :         mutedForeground: spec.mutedForeground,
     108            1 :         accent: spec.accent,
     109            1 :         accentForeground: spec.accentForeground,
     110            1 :         destructive: spec.destructive,
     111            1 :         destructiveForeground: spec.destructiveForeground,
     112            1 :         destructiveForegroundOn: spec.destructiveForegroundOn,
     113            1 :         info: spec.info,
     114            1 :         infoForeground: spec.infoForeground,
     115            1 :         success: spec.success,
     116            1 :         successForeground: spec.successForeground,
     117            1 :         warning: spec.warning,
     118            1 :         warningForeground: spec.warningForeground,
     119            1 :         border: spec.border,
     120            1 :         input: spec.input,
     121            1 :         ring: spec.ring,
     122              :       ),
     123            2 :       radii: radius == null ? radii : FossRadii.fromBase(radius),
     124            2 :       spacing: unit == null ? spacing : FossSpacing(unit: unit),
     125              :       typography: fontFamily == null
     126            1 :           ? typography
     127            2 :           : _reFamily(typography, fontFamily),
     128            3 :       shadows: shadowColor == null ? shadows : _reTint(shadows, shadowColor),
     129            1 :       motion: motion,
     130              :     );
     131              :   }
     132              : 
     133            1 :   @override
     134              :   FossThemeData copyWith({
     135              :     FossColors? colors,
     136              :     FossRadii? radii,
     137              :     FossSpacing? spacing,
     138              :     FossTypography? typography,
     139              :     FossShadows? shadows,
     140              :     FossMotion? motion,
     141            1 :   }) => FossThemeData(
     142            1 :     colors: colors ?? this.colors,
     143            1 :     radii: radii ?? this.radii,
     144            1 :     spacing: spacing ?? this.spacing,
     145            1 :     typography: typography ?? this.typography,
     146            1 :     shadows: shadows ?? this.shadows,
     147            1 :     motion: motion ?? this.motion,
     148              :   );
     149              : 
     150              :   // Hand-written, not generated: each bundle must lerp through its own encoder,
     151              :   // so a generated default (which would snap the bundle fields) is not usable.
     152            1 :   @override
     153              :   FossThemeData lerp(covariant FossThemeData? other, double t) {
     154              :     if (other == null) return this;
     155            1 :     return FossThemeData(
     156            3 :       colors: colors.lerp(other.colors, t),
     157            3 :       radii: radii.lerp(other.radii, t),
     158            3 :       spacing: spacing.lerp(other.spacing, t),
     159            3 :       typography: typography.lerp(other.typography, t),
     160            3 :       shadows: shadows.lerp(other.shadows, t),
     161            3 :       motion: motion.lerp(other.motion, t),
     162              :     );
     163              :   }
     164              : 
     165              :   /// This theme wrapped in a [ThemeData], to register on `MaterialApp.theme`.
     166              :   /// Registers the theme as an extension; it does not restyle Material widgets.
     167              :   ///
     168              :   /// ```dart
     169              :   /// MaterialApp(theme: FossThemeData.light.toThemeData());
     170              :   /// ```
     171            6 :   ThemeData toThemeData() => ThemeData(extensions: [this]);
     172              : 
     173           20 :   @override
     174              :   bool operator ==(Object other) =>
     175              :       identical(this, other) ||
     176            8 :       other is FossThemeData &&
     177           24 :           colors == other.colors &&
     178            6 :           radii == other.radii &&
     179            6 :           spacing == other.spacing &&
     180            6 :           typography == other.typography &&
     181            6 :           shadows == other.shadows &&
     182            6 :           motion == other.motion;
     183              : 
     184            1 :   @override
     185              :   int get hashCode =>
     186            7 :       Object.hash(colors, radii, spacing, typography, shadows, motion);
     187              : }
     188              : 
     189              : /// Rebuilds every type step on [family], preserving size, height, and spacing.
     190            2 : FossTypography _reFamily(FossTypography t, String family) => FossTypography(
     191            2 :   xs: t.xs.copyWith(fontFamily: family),
     192            2 :   sm: t.sm.copyWith(fontFamily: family),
     193            2 :   base: t.base.copyWith(fontFamily: family),
     194            2 :   lg: t.lg.copyWith(fontFamily: family),
     195            2 :   xl: t.xl.copyWith(fontFamily: family),
     196            2 :   xl2: t.xl2.copyWith(fontFamily: family),
     197              : );
     198              : 
     199              : /// Re-tints every shadow layer to [color], keeping each layer's alpha and
     200              : /// geometry.
     201            2 : FossShadows _reTint(FossShadows s, Color color) => FossShadows(
     202            2 :   xs: _tint(s.xs, color),
     203            2 :   sm: _tint(s.sm, color),
     204            2 :   md: _tint(s.md, color),
     205            2 :   lg: _tint(s.lg, color),
     206              : );
     207              : 
     208            2 : List<BoxShadow> _tint(List<BoxShadow> layers, Color color) => [
     209            1 :   for (final layer in layers)
     210            1 :     BoxShadow(
     211            3 :       color: color.withValues(alpha: layer.color.a),
     212            1 :       offset: layer.offset,
     213            1 :       blurRadius: layer.blurRadius,
     214            1 :       spreadRadius: layer.spreadRadius,
     215              :     ),
     216              : ];
     217              : 
     218              : /// Provides a [FossThemeData] to its subtree for non-Material apps. Material
     219              : /// apps can instead register the theme in `ThemeData.extensions`; either way
     220              : /// `context.fossTheme` resolves it.
     221              : ///
     222              : /// ```dart
     223              : /// FossTheme(
     224              : ///   data: FossThemeData.light,
     225              : ///   child: const MyApp(),
     226              : /// );
     227              : /// ```
     228              : class FossTheme extends InheritedWidget {
     229              :   /// Creates a theme scope.
     230           47 :   const FossTheme({required this.data, required super.child, super.key});
     231              : 
     232              :   /// The theme exposed to the subtree.
     233              :   final FossThemeData data;
     234              : 
     235              :   /// The nearest [FossThemeData] above [context], or null if none.
     236           58 :   static FossThemeData? maybeOf(BuildContext context) =>
     237          108 :       context.dependOnInheritedWidgetOfExactType<FossTheme>()?.data;
     238              : 
     239           19 :   @override
     240           57 :   bool updateShouldNotify(FossTheme oldWidget) => data != oldWidget.data;
     241              : }
     242              : 
     243              : /// The single way to read fossui tokens.
     244              : ///
     245              : /// Resolves the [FossTheme] InheritedWidget first, then a `FossThemeData`
     246              : /// registered in `ThemeData.extensions`, and finally the light default, so it
     247              : /// works under MaterialApp, CupertinoApp, or a bare WidgetsApp.
     248              : ///
     249              : /// ```dart
     250              : /// final t = context.fossTheme;
     251              : /// Container(color: t.colors.background, padding: t.spacing.all(4));
     252              : /// ```
     253              : extension FossThemeContext on BuildContext {
     254              :   /// The active fossui theme.
     255           58 :   FossThemeData get fossTheme =>
     256           58 :       FossTheme.maybeOf(this) ??
     257           40 :       Theme.of(this).extension<FossThemeData>() ??
     258              :       FossThemeData.light;
     259              : }
        

Generated by: LCOV version 2.0-1