Spacing Gen

A Flutter CLI tool to generate a standardized app_spacing.dart file containing padding, border-radius, and gap constants based on a configurable base unit.

Features

  • Generates a comprehensive set of spacing constants (xs, sm, md, lg, etc.) based on a single base value.
  • Includes EdgeInsets constants for all sides, horizontal, and vertical padding.
  • Includes SizedBox constants for horizontal and vertical gaps.
  • Includes BorderRadius constants for standard border radii.
  • Highly configurable via CLI arguments.

Installation

Since this is a CLI tool, you can clone the repository and run it locally, or if added to your project's pubspec.yaml, run it via Dart or Flutter.

Usage

Run the generator from your terminal.

dart run spacing_gen [base=<value>] [output=<directory>]

Arguments

  • base (optional): The base unit for all spacing calculations. Defaults to 4.0.
  • output (optional): The directory where the app_spacing.dart file will be generated. Defaults to lib.

Examples

Generate with default settings (base = 4.0, output = lib):

dart run spacing_gen

Generate with a base of 8.0 and output to lib/theme:

dart run spacing_gen base=8.0 output=lib/theme

Generated File Example

The tool generates an AppSpacing class that looks like this:

import 'package:flutter/widgets.dart';

class AppSpacing {
  AppSpacing._();

  static const double base = 8.0;

  static const double xxs = base * 1;
  static const double xs = base * 2;
  // ... more sizes

  // All padding
  static const EdgeInsets allXs = EdgeInsets.all(xs);
  // ...

  // Gaps
  static const SizedBox gapHXs = SizedBox(width: xs);
  static const SizedBox gapVXs = SizedBox(height: xs);
  // ...

  // Border radius
  static const BorderRadius roundedSm = BorderRadius.all(Radius.circular(8));
  // ...
}

You can then use these constants throughout your Flutter app for consistent spacing:

Padding(
  padding: AppSpacing.allMd,
  child: Column(
    children: [
      Text('Hello'),
      AppSpacing.gapVMd,
      Container(
        decoration: BoxDecoration(
          borderRadius: AppSpacing.roundedMd,
        ),
      ),
    ],
  ),
)

Libraries

spacing_gen