Density class

Defines density settings for spacing throughout the UI.

Density provides base values that are multiplied by padding constants (e.g., padXs, padSm, padLg) to produce final pixel values. This allows consistent scaling of all spacing when switching density modes.

There are two types of padding:

  • Container padding: Used for widgets that contain multiple children (e.g., Card, AlertDialog, ListView panels)
  • Content padding: Used for widgets that contain content (e.g., Button, TextField, Chip)

Example:

// Apply compact density to reduce spacing
Theme(
  data: ThemeData(density: Density.compactDensity),
  child: MyApp(),
)

Constructors

Density({required double baseContainerPadding, required double baseGap, required double baseContentPadding})
Creates a Density with custom base values.
const

Properties

baseContainerPadding double
Base padding for container widgets (Card, AlertDialog, etc.).
final
baseContentPadding double
Base padding for content widgets (Button, TextField, etc.).
final
baseGap double
Base gap between items in rows, columns, and flex layouts.
final
hashCode int
The hash code for this object.
no setteroverride
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

copyWith({ValueGetter<double>? baseContainerPadding, ValueGetter<double>? baseGap, ValueGetter<double>? baseContentPadding}) Density
Creates a copy of this density with the specified values replaced.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
override

Operators

operator ==(Object other) bool
The equality operator.
override

Static Methods

lerp(Density a, Density b, double t) Density
Linearly interpolates between two density settings.

Constants

compactDensity → const Density
Compact density for maximizing content density (8px base).
defaultDensity → const Density
Default density with standard spacing (16px base).
reducedDensity → const Density
Reduced density for slightly more compact layouts (12px base).
spaciousDensity → const Density
Spacious density for more generous spacing (20px base).