safe_area_fraction 0.0.6 copy "safe_area_fraction: ^0.0.6" to clipboard
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.


pub package platform License: MIT


✨ 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.

2
likes
160
points
13
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A Flutter widget that applies padding based on a fraction of the safe area dimensions.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter, flutter_lints

More

Packages that depend on safe_area_fraction