WSpacer
The WSpacer is a lightweight widget designed specifically for adding consistent gaps between elements. Unlike WDiv, which supports a full range of decorations and layout properties, WSpacer renders as a simple SizedBox, making it highly efficient for layouts where you only need spacing.
On this page
widgets/w_spacer_basic
A live demo of this example follows.
w_spacer_basic.dart
View source
Open demo
WDiv(
className: 'flex flex-col',
children: [
WText('Label', className: 'font-bold'),
WSpacer(className: 'h-2'), // 8px vertical gap
WInput(placeholder: 'Enter text...'),
],
)
Basic Usage#
The WSpacer widget extracts width and height values from your utility classes and applies them to a SizedBox. It supports the standard Wind spacing scale (based on a 4px unit by default).
Vertical Spacing#
Use the h-{n} classes to add vertical gaps in columns:
WSpacer(className: 'h-4') // 16px height
Horizontal Spacing#
Use the w-{n} classes to add horizontal gaps in rows:
WSpacer(className: 'w-2') // 8px width
Constructor#
const WSpacer({
Key? key,
String? className,
})
Props#
| Prop | Type | Default | Description |
|---|---|---|---|
className |
String? |
null |
Wind utility classes for sizing (h-, w-). |
Responsive Spacing#
You can adjust spacing based on screen size using responsive prefixes. This is particularly useful for increasing margins on tablet or desktop layouts.
widgets/w_spacer_responsive
A live demo of this example follows.
w_spacer_responsive.dart
View source
Open demo
WSpacer(className: 'h-4 md:h-8')
Why WSpacer?#
While you could use WDiv with padding or margin, WSpacer is preferred for explicit gaps for several reasons:
- Performance: It bypasses complex composition and renders as a single
SizedBox. - Semantics: It clearly communicates "this widget exists only for spacing" to other developers.
- Convenience: It replaces the verbose
SizedBox(height: 16)with the more maintainableWSpacer(className: 'h-4').
All Supported Classes#
| Category | Classes | Description |
|---|---|---|
| Sizing | h-{n}, w-{n} |
Standard scale (h-1, h-2, etc.) |
| Arbitrary | h-[15px], w-[2px] |
Custom pixel values |
| Responsive | md:h-*, lg:w-* |
Breakpoint-specific spacing |
Customizing Theme#
The pixel values for h-{n} and w-{n} are derived from the baseSpacingUnit in your theme.
WindThemeData(
baseSpacingUnit: 4.0, // Default: h-1 = 4px
)