textstyle_generator

⚑️ A Flutter code generator to automatically create TextStyle helpers for your project from your custom fonts!

🐌 Before

Text(
  'Hello World',
    style: TextStyle(
    fontFamily: 'SF-Pro-Display-BoldItalic',
    fontSize: 20,
    fontWeight: FontWeight.w700,
    color: Colors.black,
    height: 1.2,
    letterSpacing: 0.5,
  ),
)

πŸš€ After

Text(
  'Hello World',
  style: TextStyles.sfProDisplay20w700bi(c: Colors.black, h: 1.2, l: 0.5),
)

πŸš€ And After with defaults

Text(
  'Hello World',
  style: TextStyles.sfProDisplay20w700bi(),
)

✨ Features

  • Generates short, clear methods like style16w400m({Color? c, double? h, double? l,})
  • Supports all font sizes (default from 8 to 64)
  • Detects and maps font weight (400, 500, 700) automatically
  • Detects suffixes based on font family names (e.g., m, b, i, bi)
  • Allows overriding the default text color using your custom palette
  • Highly configurable via build.yaml
  • No need for manually writing hundreds of TextStyle constructors
  • Works perfectly with Flutter's build_runner

πŸ“‹ Requirements

You must create a trigger file at lib/textstyle_generator_trigger.dart.

The trigger file must contain at least:

  • An import for package:flutter/material.dart.
  • An import for your palette file if you want to override the default text color.
  • A part 'generated/text_styles.g.dart'; statement.

Example trigger file:

import 'package:flutter/material.dart';
/// Imports the palette file that defines your default text color.
import 'palette.dart';

part 'generated/text_styles.g.dart';

If no custom palette is provided, the default text color will be const Color(0xFF000000) (pure black).

πŸš€ Quick Start

1. Add dependency

Add this to your pubspec.yaml:

dependencies:
  textstyle_generator: ^0.1.0

2. Place your fonts:

assets/fonts/
β”œβ”€β”€ Ubuntu-Regular.ttf
β”œβ”€β”€ Ubuntu-Medium.ttf
β”œβ”€β”€ Ubuntu-Bold.ttf
β”œβ”€β”€ Ubuntu-BoldItalic.ttf
└── Ubuntu-Light.ttf

3. (Optional) Configure build.yaml

targets:
  $default:
    builders:
      textstyle_generator|textstyle_builder:
        options:
          font_path: assets/fonts/
          min: 10
          max: 24
          default_palette: Palette().black()
          output_dir: lib/generated_assets/
Default settings if omitted:
β€’	font_path: assets/fonts/
β€’	min: 8
β€’	max: 64
β€’	default_palette: const Color(0xFF000000) (pure black)
β€’	output_dir: lib/generated/

4. Run the generator

Use this command to generate your TextStyles:

dart run build_runner build --delete-conflicting-outputs

This will create the file: lib/generated/text_styles.g.dart

5. Use it in your project

import 'package:your_project/textstyle_generator_trigger.dart';

/// You can use the generated text styles directly
Text('Hello world!', style: TextStyles.ubuntu18w400i()),

/// You can use text styles with parameters
Text(
  'Hello world!',
  style: TextStyles.style16w400m(c: Colors.blue, h: 1.5),
);
 
/// If there are not enough parameters, you can use copyWith()
Text(
  'Hello world!',
  style: TextStyles.ubuntu10w300l().copyWith(wordSpacing: 2),
),
β€’	c: β€” optional Color (overrides default palette color)
β€’	h: β€” optional double height (line height adjustment)

πŸ›  How font naming and suffixes work

The textstyle_generator parses font file names to automatically detect font weights and styles. It expects your font files to follow a clear naming convention, typically in the format:

FamilyName-StyleName.ttf

For example:

β€’	Ubuntu-Regular.ttf
β€’	Ubuntu-Bold.ttf
β€’	Ubuntu-Italic.ttf
β€’	Ubuntu-BoldItalic.ttf
β€’	Ubuntu-Light.ttf
β€’	Ubuntu-LightItalic.ttf
β€’	Ubuntu-Medium.ttf
β€’	Ubuntu-MediumItalic.ttf

The generator analyzes the StyleName (part after the hyphen -) and assigns the correct font weight and suffix.

✏️ Naming rules

| Style keyword        | Weight | Suffix |
|:---------------------|:-------|:-------|
| Thin                 | 100    | t      |
| ThinItalic           | 100    | ti     |
| ExtraLight           | 200    | l      |
| ExtraLightItalic     | 200    | li     |
| Light                | 300    | l      |
| LightItalic          | 300    | li     |
| Regular              | 400    | (empty)|
| RegularItalic        | 400    | i      |
| Medium               | 500    | m      |
| MediumItalic         | 500    | mi     |
| SemiBold             | 600    | sb     |
| SemiBoldItalic       | 600    | sbi    |
| Bold                 | 700    | b      |
| BoldItalic           | 700    | bi     |
| ExtraBold            | 800    | eb     |
| ExtraBoldItalic      | 800    | ebi    |
| Black                | 900    | b      |
| BlackItalic          | 900    | bi     |
| Heavy                | 900    | h      |
| HeavyItalic          | 900    | hi     |
| Italic               | 400    | i      |

πŸ›  How method names are generated:

The generated method name pattern is:

<baseName><fontSize>w<weight><suffix>()

Where:

β€’	baseName β€” the normalized family name (e.g., ubuntu)
β€’	fontSize β€” the font size
β€’	weight β€” the font weight
β€’	suffix β€” the style suffix (if any)

πŸ“‹ Example

If you have a font file Ubuntu-BoldItalic.ttf, and your generator is configured for font size 18, the generated method will be:

TextStyles.ubuntu18w700bi()

πŸ“‹ Font file to generated method mapping

| Font File                  | Example Size | Generated Method         | Meaning                               |
|:----------------------------|:-------------|:--------------------------|:-------------------------------------|
| Ubuntu-Regular.ttf          | 18           | ubuntu18w400()             | Regular font, weight 400             |
| Ubuntu-Italic.ttf           | 16           | ubuntu16w400i()            | Italic font, weight 400              |
| Ubuntu-Light.ttf            | 14           | ubuntu14w300l()            | Light font, weight 300               |
| Ubuntu-LightItalic.ttf      | 12           | ubuntu12w300li()           | Light + Italic font, weight 300      |
| Ubuntu-Medium.ttf           | 20           | ubuntu20w500m()            | Medium font, weight 500              |
| Ubuntu-MediumItalic.ttf     | 18           | ubuntu18w500mi()           | Medium + Italic font, weight 500     |
| Ubuntu-Bold.ttf             | 22           | ubuntu22w700b()            | Bold font, weight 700                |
| Ubuntu-BoldItalic.ttf       | 24           | ubuntu24w700bi()           | Bold + Italic font, weight 700       |
| Ubuntu-ExtraBold.ttf        | 30           | ubuntu30w800eb()           | ExtraBold font, weight 800           |
| Ubuntu-Black.ttf            | 32           | ubuntu32w900b()            | Black font, weight 900               |
| Ubuntu-BlackItalic.ttf      | 28           | ubuntu28w900bi()           | Black + Italic font, weight 900      |

β˜• Support

If you find this package useful, you can support the development by buying me a coffee!
Your support helps keep this project active and maintained.

Buy Me a Coffee