s_extensions 1.0.0
s_extensions: ^1.0.0 copied to clipboard
A dart package aiming to provide useful extensions and helper functions to ease and speed up development.
✨ S-Extensions ✨
A dart package aiming to provide useful extensions and helper functions to ease and speed up development.
Works great for building responsive UIs — for example, News headers, Marketplace cards, or Prayer time widgets.
A dart package aiming to provide useful extensions and helper functions to ease and speed up development.
Works great for building responsive UIs — for example, News headers, Marketplace cards, or Prayer time widgets.
- 📋 Well Documented
- ⚔️ Fully Tested
- 👌 Follows Code Quality Guidelines
- 🦾 Production Ready
| On | Extensions | Functions | Operators |
|---|---|---|---|
| Bool | 1 | 0 | 0 |
| Context | 6 | 1 | 0 |
| DateTime | 21 (+5) | 0 | 2 |
| List | 4 | 3 | 1 |
| Map | 1 | 0 | 0 |
| Number | 18 | 2 | 0 |
| String | 15 | 0 | 0 |
| Widget | 28 | 1 | 0 |
| Total | 85 | 7 | 3 |
| All | 95 |
Installation #
-
Add to
pubspec.yaml:dependencies: s_extensions: <latest_version> -
Import library into your code.
import 'package:s_extensions/s_extensions.dart';
Quick Start #
Recommended initialization (supports hot reload and navigation changes):
MaterialApp(
builder: (context, child) {
FlutterExtensions.update(context); // keep stored view & media fresh
return child!;
},
home: const MyHomePage(),
);
Features #
Usage examples:
Bool extensions #
isLoaded.toggle; // toggles isLoaded to false if true, and true if false
Context extensions #
context.hideKeyboard; // hides the keyboard
context.orientation; // returns the device orientation
context.isDarkMode; // returns true if the device is in dark mode
context.isKeyboardVisible; // returns true if the keyboard is visible
context.keyboardHeight; // returns the height of the keyboard
context.isLandscape; // returns true if the device orientation is landscape
context.isPortrait; // returns true if the device orientation is portrait
DateTime extensions
DateTime.now().format('yyyy-MM-dd'); // 2023-08-24
DateTime.now().formatYearMonthDay; // 2023-08-24
DateTime.now().formatDayMonthYear; // 24-08-2023
DateTime.now().formatYearMonthDayHrMinSec; // 2023-08-24 12:34:56
DateTime.now().formatYearMonthDayHrMin; // 2023-08-24 12:34
DateTime.now().formatHrMin12; // 12:34 PM
DateTime.now().formatHrMinSec12; // 12:34:56 PM
DateTime.now().formatHrMin24; // 15:34
DateTime.now().formatHrMinSec24; // 15:34:56
DateTime.now().formatDay; // Sunday
DateTime.now().formatMonth; // August
DateTime.now().isPast; // false
DateTime.now().isFuture; // false
DateTime.now().isToday; // true
DateTime.now().isTomorrow; // false
DateTime.now().isYesterday; // false
DateTime.now().isInPreviousMonth; // false
DateTime.now().isInPreviousYear; // false
DateTime.now().isInNextMonth; // false
DateTime.now().isInNextYear; // false
operator +(Duration duration) // Returns a new DateTime with this duration added.
(DateTime.now() + 1.day); // 2023-08-25 12:34:56.000
operator -(Duration duration) // Returns a new DateTime with this duration subtracted.
(DateTime.now() - 1.day); // 2023-08-23 12:34:56.000
(DateTime.now() - 1.day).fromNow; // Duration(days: 1)
List extensions #
[1, 2, 3].sum; // 6
[1, 2, 3].average; // 2.0
[1, 2, 3].toJson; // '[1, 2, 3]'
operator +(element) // Adds new [element] or [elements] to this list.
[1, 2, 3].random; // 2
[Widgets].toStack(); // Stack(children: [Widgets])
[Widgets].toColumn(); // Column(children: [Widgets])
[Widgets].toRow(); // Row(children: [Widgets])
Map extensions #
{'name': 'John', 'age': 30}.toJson; // '{"name": "John", "age": 30}'
Number extensions #
8.half; // 4.0
8.quarter; // 2.0
8.square; // 64.0
8.sqRoot; // 2.8284271247461903
300.misec; // Duration(microseconds: 300)
300.msec; // Duration(milliseconds: 300)
300.sec; // Duration(seconds: 300)
300.min; // Duration(minutes: 300)
300.hr; // Duration(hours: 300)
300.day; // Duration(days: 300)
0.5.screenWidth; // half of the screen width
0.5.screenHeight; // half of the screen height
123.456.fixed00; // 123.46
123.456.fixed0; // 123.5
123.456.format('0.00'); // 123.46
16.verticalSpace; // SizedBox(height: 16.0)
16.horizontalSpace; // SizedBox(width: 16.0)
20.isDivisibleBy(5); // true
30.isBetween(20, 40); // true
50.isEven; // true
String extensions #
'hello'.capitalize; // Hello
'hello'.reverse; // olleh
'[email protected]'.isEmail; // true
'+1234567890'.isPhoneNumber; // true
'https://www.example.com'.isUrl; // true
"23.4".toDouble; // 23.4
"23.4".toInt; // 23
"2023-08-24".toDate; // 2023-08-24 00:00:00.000
"Hello World!".words; // ["Hello", "World!"]
'{"name": "John", "age": 30}'.parseJson` // {'name': 'John', 'age': 30} as Map
'racecar'.isPalindrome; // true
'congratolations'.truncate(4); // 'cong...'
'Hello World!'.removeWhiteSpace; // 'HelloWorld!'
'Hello123 World!'.removeNumbers; // 'Hello World!'
Widget extensions #
[
// margin extensions
Text('Hello World!').marginAll(16.0),
Text('Hello World!').marginSymmetric(vertical: 16.0, horizontal: 16.0),
Text('Hello World!').marginOnly(left: 16.0, top: 16.0, right: 16.0, bottom: 16.0),
// padding extensions
Text('Hello World!').paddingAll(16.0),
Text('Hello World!').paddingSymmetric(vertical: 16.0, horizontal: 16.0),
Text('Hello World!').paddingOnly(left: 16.0, top: 16.0, right: 16.0, bottom: 16.0),
// expanded and flexible extensions
[
Text('Hello World!').expanded(),
Text('Hello World!').expanded(),
].toColumn(spacer: 16),
Text('Hello World!').flexible(),
// customizations
16.verticalSpace.center(),
Text('Hello World!').directionality(direction: TextDirection.ltr),
Image.asset('path').align(alignment: Alignment.center),
[
Text('Hello World!')
.center()
.paddingAll(12)
.decoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16.0),
),
].toStack(),
// shape extensions & gestures
Text('Hello World!')
.constrained(width: 100.0, height: 70.0)
.rounded(radius: 16.0),
Text('Hello World!')
.height(70.0)
.roundedTop(radius: 16.0),
Text('Hello World!')
.width(100.0)
.roundedBottom(radius: 16.0)
.gestures(
onTap: () => print('tap'),
),
// animation extensions
Text('Hello World!').fade(),
Text('Hello World!').scale(beginScale: 0.9),
Text('Hello World!').slideUp(),
Text('Hello World!').slideDown(),
Text('Hello World!').slideLeft(),
Text('Hello World!').slideRight(),
Text('Hello World!').rotate(beginAngle: 0.05),
// scroll and refresh extensions
[
Widgets...
].toColumn()
.scrollable()
.refreshable(onRefresh: () async {
await Future.delayed(2.seconds),
}),
].toColumn(separator: 16);
Static Context Manager #
FlutterExtensions is a tiny singleton managing a FlutterView and a cached MediaQueryData. It powers property access like 0.9.screenWidth and 0.9.screenHeight without explicitly passing BuildContext.
Initialization patterns:
- Explicit (recommended): call
FlutterExtensions.update(context)inMaterialApp.builder. - Lazy: first use attempts
WidgetsBinding.instance.platformDispatcher.views.first. This works afterrunApp; it throws if no view is available (e.g., too early in tests).
Key API:
FlutterExtensions.init([context])— initialize explicitly; optionalBuildContext.FlutterExtensions.update(context)— refresh storedFlutterViewandMediaQueryDataon rebuilds/hot reload.FlutterExtensions.isInitialized— check readiness.FlutterExtensions.mediaQuery()— get the currentMediaQueryData(attempts lazy init; may throw).FlutterExtensions.reset()— clear cached references (useful for tests).
Hot reload and metrics:
- Implements
WidgetsBindingObserver.didChangeMetricsto refresh cachedMediaQueryDataon orientation or size changes.
Error Handling #
mediaQuery()may throwStateErrorif Flutter is not fully initialized or noFlutterViewis available.- Ensure your app has started (
runApp) and prefer usingFlutterExtensions.update(context)for determinism.
Testing #
- Call
WidgetsFlutterBinding.ensureInitialized()in tests. - Use
FlutterExtensions.reset()to clear cached state between tests.
Notes & Best Practices #
- Prefer
FlutterExtensions.update(context)at the app root to avoid edge cases. - Values for
screenWidthandscreenHeightare typically between0and1for proportional sizing. - Duration conversions truncate fractional values via
toInt().
License #
Licensed under the MIT License. See LICENSE for details.
Changelog #
See CHANGELOG.md for release notes.