responsive_media 1.0.0
responsive_media: ^1.0.0 copied to clipboard
A responsive utility for Flutter to simplify scaling text, padding, and layout.
ðą Responsive Media #
A clean and powerful Flutter utility for building responsive UIs based on screen size and orientation. It helps scale font sizes, paddings, margins, gaps, dimensions, and widgets using intuitive extensions and a central class â ResponsiveMedia.
âĻ Features #
- ðĨ Works seamlessly across all devices: phones, tablets, landscape mode, and web.
- ð§Ū Auto-scales typography and spacing based on the shortest screen side.
- ð§ Intuitive extensions like
0.02.rh,0.05.rw,0.04.rs. - ð Responsive widgets like
rm.h1Text(),rm.gapM(). - ðĶ Easy to integrate into any Flutter project.
- ð Optional
scalefactor for fine-tuning. - ð Static initialization via
.init(context)for easy usage anywhere.
ð Installation #
Add the package to your project's pubspec.yaml:
dependencies:
responsive_media: ^1.0.0
Then run:
flutter pub get
ð§ Basic Usage #
1. Import the package #
import 'package:responsive_media/responsive_media.dart';
2. Initialize ResponsiveMedia #
@override
Widget build(BuildContext context) {
ResponsiveMedia.init(context); // Initialize here
...
}
â ïļ Make sure to call ResponsiveMedia.init(context) inside the build method of the root widget or wrap your MaterialApp with a custom builder.
3. Access ResponsiveMedia anywhere #
final rm = ResponsiveMedia.instance; // Use after init(context)
Padding(
padding: rm.defaultPadding,
child: Text("Welcome!", style: rm.h1Style),
);
â
Example Implementation to Support .init(context) #
Below is an example of how you can implement the .init(context) pattern in the ResponsiveMedia class:
class ResponsiveMedia {
static ResponsiveMedia? _instance; // Nullable for safety
final BuildContext context;
final double scale;
late final double _shortestSide;
late final MediaQueryData _mq;
ResponsiveMedia._(this.context, {this.scale = 1.0}) {
_mq = MediaQuery.of(context);
_shortestSide = _mq.size.shortestSide;
}
/// Initialize and store instance
static void init(BuildContext context, {double scale = 1.0}) {
_instance = ResponsiveMedia._(context, scale: scale);
}
/// Access anywhere after init
static ResponsiveMedia get instance {
if (_instance == null) {
throw Exception('ResponsiveMedia.init(context) must be called first');
}
return _instance!;
}
double get rs => _shortestSide * scale;
// Example styles
TextStyle get h1Style => TextStyle(fontSize: 0.06 * _shortestSide);
TextStyle get h2Style => TextStyle(fontSize: 0.045 * _shortestSide);
TextStyle get pStyle => TextStyle(fontSize: 0.035 * _shortestSide);
// Example padding
EdgeInsets get defaultPadding => EdgeInsets.all(0.04 * _shortestSide);
// You can add rs extensions too
}
You can add more styles like h2Style, pStyle, and others in a similar manner.
Usage Example #
@override
Widget build(BuildContext context) {
ResponsiveMedia.init(context); // Initialize here
final rm = ResponsiveMedia.instance;
return Padding(
padding: rm.defaultPadding,
child: Text("Hello!", style: rm.h1Style),
);
}
ð§Đ Examples and Use Cases #
â 1. Responsive Text and Padding #
final rm = ResponsiveMedia.instance;
Text('Welcome!', style: rm.h1Style),
Padding(
padding: rm.defaultPadding,
child: Text('This is a responsive description.', style: rm.pStyle),
);
â 2. Responsive Layout with SizedBox and Spacing #
Column(
children: [
Text('Step 1', style: rm.h2Style),
rm.gapS(),
Text('Enter your info'),
rm.gapM(),
Text('Step 2', style: rm.h2Style),
],
);
â
3. Extensions: .rh, .rw, .rs #
Container(
height: 0.3.rh, // 30% of screen height
width: 0.9.rw, // 90% of screen width
padding: EdgeInsets.all(0.02.rs), // 2% of shortest side
child: Text('Scaled Container'),
);
â 4. Grid Layout Example #
GridView.count(
crossAxisCount: MediaQuery.of(context).orientation == Orientation.portrait ? 2 : 4,
crossAxisSpacing: 0.02.rw,
mainAxisSpacing: 0.02.rh,
children: List.generate(6, (i) => Container(
color: Colors.amber,
child: Center(child: Text('Item $i')),
)),
);
â 5. Reusable Card Layout #
Container(
padding: EdgeInsets.all(0.025.rs),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(0.02.rs),
boxShadow: [BoxShadow(color: Colors.black12, blurRadius: 4)],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
rm.h2Text('Title'),
rm.gapS(),
Text('Details...', style: rm.pStyle),
],
),
);
ð§ API Summary #
Typography #
| Getter | Description |
|---|---|
h1 |
Large heading |
h2 |
Medium heading |
h3, h4, h5, h6 |
Smaller headings |
title |
Section title |
subtitle |
Smaller subtitle |
p |
Paragraph text |
Sizing #
| Getter | Description |
|---|---|
small |
Small button size |
medium |
Medium padding size |
large |
Large widget size |
fromHeight(f) |
Scales by screen height |
fromWidth(f) |
Scales by screen width |
Gaps and Padding #
| Getter | Description |
|---|---|
gapS, gapM |
Spacing widgets |
defaultPadding |
Uniform padding |
marginXS, spacingXL, etc. |
Various sizes |
Extensions #
| Extension | Description |
|---|---|
0.2.rh |
20% of screen height |
0.1.rw |
10% of screen width |
0.03.rs |
3% of shortest side (consistent UI) |
ð Comparison with flutter_screenutil #
| Feature | ResponsiveMedia | flutter_screenutil |
|---|---|---|
| ð Requires context? | â
Yes (for .of) |
â Yes (init) |
| ð Uses shortestSide? | â Yes | â No |
| ð TextStyle & Widgets | â Built-in | â Manual |
| ðŠ Custom scale support | â Yes | â Yes |
| ð§Đ Easy to use | â Very simple | â ïļ Slightly verbose |
| ð Orientation support | â Yes | â ïļ Manual |
| ð§ą Spacing & Margin APIs | â Built-in | â Manual |
| ðŠķ Lightweight | â Zero dependencies | â ïļ Extra dependencies |
Happy coding with ResponsiveMedia! ð