CustomSizedBox

An enhanced version of Flutter's SizedBox with additional sizing constraints, aspect ratio control, and debugging capabilities.

Features

  • All standard SizedBox functionality
  • Minimum and maximum size constraints
  • Aspect ratio enforcement
  • Visual debugging options
  • Runtime validation for negative dimensions
  • Demo showcasing various configurations

Installation

Add the dependency to your pubspec.yaml:

dependencies:
  apptomate_custom_sized_box: ^0.0.1

Usage

Basic Usage

CustomSizedBox(
  width: 200,
  height: 100,
  child: Container(color: Colors.blue),
)

Available Parameters

Parameter Type Default Description
width double? null Exact width of the box
height double? null Exact height of the box
minWidth double? null Minimum width constraint
maxWidth double? null Maximum width constraint
minHeight double? null Minimum height constraint
maxHeight double? null Maximum height constraint
aspectRatio double? null Width/height ratio (overrides width/height)
debugPaint bool false Shows debug borders when true
debugColor Color Colors.red Color for debug borders
child Widget? null Child widget to contain

Advanced Examples

With aspect ratio:

CustomSizedBox(
  aspectRatio: 16/9,
  child: Image.network('https://example.com/image.jpg'),
)

With size constraints:

CustomSizedBox(
  minWidth: 100,
  maxWidth: 300,
  minHeight: 50,
  child: Container(color: Colors.green),
)

Debug mode:

CustomSizedBox(
  width: 150,
  height: 150,
  debugPaint: true,
  child: FlutterLogo(),
)

Demo

The package includes a CustomSizedBoxWidget demo that shows four different configurations:

  1. Basic SizedBox
  2. With aspect ratio (16:9)
  3. With size constraints (and debug paint)
  4. Empty SizedBox (spacer example)

To run the demo:

void main() {
  runApp(MaterialApp(
    home: CustomSizedBoxWidget(),
  ));
}

Implementation Details

The widget builds upon SizedBox with these additional layers:

  1. AspectRatio (when specified)
  2. ConstrainedBox (for min/max constraints)
  3. Debug Container (when debugPaint is true)

Validation Rules

The widget includes assertions to prevent invalid values:

  • Width and height cannot be negative
  • Aspect ratio must be positive

Comparison with Flutter's SizedBox

Feature Flutter SizedBox CustomSizedBox
Exact sizing Yes Yes
Aspect ratio No Yes
Size constraints No Yes
Debug visualization Manual Built-in
Validation No Yes

Best Practices

  1. Use aspectRatio for media containers (images, videos)
  2. Apply minWidth/minHeight for responsive layouts
  3. Enable debugPaint during development to visualize boxes
  4. Combine with other layout widgets for complex designs

Common Use Cases

  • Creating responsive containers
  • Enforcing aspect ratios for media
  • Building layout scaffolding
  • Debugging layout issues
  • Creating consistent spacing