Gapx
A smart, ScreenUtil-aware spacing widget for Flutter that replaces SizedBox and automatically adapts to Row, Column, Flex, and sliver layouts.
β¨ Why Gapx?
In Flutter, spacing is usually handled with SizedBox(height: x) or SizedBox(width: x). This becomes repetitive and errorβprone, especially in responsive apps using flutter_screenutil.
Gapx solves this by:
- Automatically detecting layout direction (
Row/Column/Flex) - Applying height for vertical layouts and width for horizontal layouts
- Seamlessly scaling values using
flutter_screenutil - Eliminating the need to use
.hor.w - Supporting slivers out of the box
π Features
- β
Dropβin replacement for
SizedBox - β
Auto adapts to
Row,Column, andFlex - β
ScreenUtil synced (no
.h/.wrequired) - β Optional background color (useful for debugging)
- β
Sliver support (
SliverGapx) - β Lightweight & performant (RenderObject based)
π¦ Installation
Add this to your pubspec.yaml:
dependencies:
gapx: ^0.0.1
Then run:
flutter pub get
β οΈ Required Setup
Gapx relies on flutter_screenutil for responsive scaling.
Make sure you initialize ScreenUtil at the root of your app:
ScreenUtilInit(
designSize: const Size(360, 690),
builder: (_, child) => MaterialApp(
home: child,
),
child: const MyHomePage(),
);
β οΈ If
ScreenUtilInitis not used, spacing will not scale as expected.
π§© Basic Usage
Column (Vertical spacing)
Column(
children: const [
Text('Above'),
Gapx(20), // height: 20.h
Text('Below'),
],
);
Row (Horizontal spacing)
Row(
children: const [
Icon(Icons.star),
Gapx(12), // width: 12.w
Text('Star'),
],
);
No need to think about height or width β Gapx handles it automatically.
π¨ With Color (Debugging / Layout Help)
Gapx(
16,
color: Colors.redAccent,
);
π Expanded Gap
Use Gapx.expand when you want the gap to take maximum space in the cross axis:
Gapx.expand(24);
π§± Flexible Gap
Use MaxGap inside flexible layouts:
Row(
children: const [
Text('Left'),
MaxGap(20),
Text('Right'),
],
);
π Sliver Support
Gapx also works inside slivers:
CustomScrollView(
slivers: const [
SliverToBoxAdapter(child: Text('Top')),
SliverGapx(24),
SliverToBoxAdapter(child: Text('Bottom')),
],
);
π§ How It Works
- Detects parent
RenderFlexdirection - Maps spacing to main axis automatically
- Applies
ScreenUtilscaling internally - Uses a custom
RenderBoxfor performance
π Comparison
| Approach | Responsive | Direction Aware | Boilerplate |
|---|---|---|---|
| SizedBox | β | β | High |
| Gap | β | β | Medium |
| Gapx | β | β | Low |
π οΈ Roadmap
Optional fallback when ScreenUtil is not initializedAnimation supportAxisβspecific constructorsWidget tests
π€ Contributing
Contributions are welcome!
- Fork the repo
- Create your feature branch
- Commit changes
- Open a pull request
π License
MIT License
β Support
If you find this package useful, please β star the repo and share feedback.
Happy Fluttering π