safe_area_fraction 0.0.6
safe_area_fraction: ^0.0.6 copied to clipboard
A Flutter widget that applies padding based on a fraction of the safe area dimensions.
π§ Safe Area Fraction #
A lightweight Flutter utility for safe-area-aware layouts
Apply fractional padding or spacing based on the usable (safe) screen area,
so your widgets never overlap notches, status bars, or system UI elements.
β¨ Features #
β
Compute fractional height/width of the usable screen area
β
Respect SafeArea paddings automatically
β
Works on all Flutter platforms (Android, iOS, Web, Windows, macOS, Linux)
β
Provides both Widget (SafeFraction) and BuildContext extension
β
Zero dependencies β pure Flutter
β
Ideal for responsive layouts, dynamic padding, and adaptive positioning
π¦ Installation #
Add this to your pubspec.yaml:
dependencies:
safe_area_fraction: ^0.0.6
Then run:
flutter pub get
Import in your Dart file:
import 'package:safe_area_fraction/safe_area_fraction.dart';
π Quick Start #
π§± Example 1 β Use as a Widget #
SafeFraction(
heightFraction: 0.1, // 10% of safe height
widthFraction: 0.05, // 5% of safe width
child: Container(
color: Colors.blueAccent,
child: const Center(
child: Text(
'Safe Fraction Demo',
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
),
)
β
This ensures your content stays within the visible screen area,
avoiding notches and system bars automatically.
π‘ Example 2 β Use as a Context Extension #
final paddingFromBottom = context.safeHeightFraction(0.1);
final buttonWidth = context.safeWidthFraction(0.8);
Positioned(
bottom: paddingFromBottom,
child: SizedBox(
width: buttonWidth,
child: ElevatedButton(
onPressed: () {},
child: const Text('Continue'),
),
),
);
π§ How It Works #
Internally uses Flutterβs MediaQuery to calculate safe dimensions:
final media = MediaQuery.of(context);
final safeHeight = media.size.height - media.padding.top - media.padding.bottom;
final safeWidth = media.size.width - media.padding.left - media.padding.right;
Your fractions are then multiplied by these safe area dimensions to determine consistent spacing.
π₯οΈ Platform Support #
| Platform | Supported | Notes |
|---|---|---|
| Android | β | Safe-area aware |
| iOS | β | Works with notches & home indicator |
| Web | β | Falls back to full window size |
| Windows | β | Works (safe padding usually 0) |
| macOS | β | Works (safe padding usually 0) |
| Linux | β | Works (safe padding usually 0) |
π± Example App #
A minimal demo is included in the /example folder.
Run it locally:
cd example
flutter run
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:safe_area_fraction/safe_area_fraction.dart';
void main() => runApp(const SafeFractionExampleApp());
class SafeFractionExampleApp extends StatelessWidget {
const SafeFractionExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Safe Area Fraction Demo',
home: Scaffold(
appBar: AppBar(title: const Text('Safe Area Fraction')),
body: SafeFraction(
heightFraction: 0.1,
widthFraction: 0.05,
child: Container(
color: Colors.blueAccent,
child: const Center(
child: Text(
'Safe Area Fraction Example',
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
),
),
),
);
}
}
βοΈ API Reference #
π§± SafeFraction Widget #
| Property | Type | Description |
|---|---|---|
heightFraction |
double? |
Fraction (0.0β1.0) of safe height to apply vertically |
widthFraction |
double? |
Fraction (0.0β1.0) of safe width to apply horizontally |
extraPadding |
EdgeInsets? |
Additional padding added on top of fractional padding |
child |
Widget |
The content widget inside the padding |
π§© SafeAreaFractionContext Extension #
| Method | Returns | Description |
|---|---|---|
safeHeightFraction(double fraction) |
double |
Returns a fraction of the usable screen height |
safeWidthFraction(double fraction) |
double |
Returns a fraction of the usable screen width |
π§© Use Cases #
- Dynamically position elements relative to screen size
- Consistent spacing on devices with notches or navigation bars
- Adaptive UIs without wrapping every widget in
SafeArea - Responsive padding for tablets, phones, and foldables
π§± Project Structure #
lib/
βββ safe_area_fraction.dart # main library file
βββ src/
βββ widgets/safe_fraction.dart # SafeFraction widget
βββ extensions/context_safe_fractions.dart # context extension
π§° Development #
To run tests:
flutter test
To check formatting & linting:
dart format .
dart analyze
πͺ Version History #
| Version | Changes |
|---|---|
| 0.0.1 | Initial release with SafeFraction widget & context extension |
π§Ύ License #
MIT License
Copyright (c) 2025 Rahul
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction...
π¬ Feedback #
If you find a bug or want to suggest a feature, open an issue:
π GitHub Issues
β€οΈ Made with Flutter #
Built and maintained by Rahul β follow for more Flutter utility packages.