π§° MayR Flutter Extensions
Flutter-specific extension methods to supercharge your Flutter development
A comprehensive collection of Flutter-specific extension methods designed to make your Flutter code cleaner, more expressive, and easier to write. This package focuses exclusively on Flutter widgets and framework features.
π¦ Package Family
This package is part of the MayR Extensions family:
- mayr_dart_extensions - General Dart extensions for String, int, DateTime, Duration, List, Map, etc.
- mayr_flutter_extensions (this package) - Flutter-specific extensions for Widget, BuildContext, etc.
π‘ Usage Together
For Flutter applications, you'll typically want to use both packages together:
// In your pubspec.yaml
dependencies:
mayr_dart_extensions: ^1.0.0 # For general Dart utilities
mayr_flutter_extensions: ^1.0.0 # For Flutter-specific features
This separation ensures:
- β
Pure Dart projects can use
mayr_dart_extensionswithout Flutter dependencies - β Flutter projects get the best of both worlds
- β No code duplication between packages
- β Cleaner, more focused codebases
π Installation
Add the package to your pubspec.yaml:
flutter pub add mayr_flutter_extensions
Or manually add it:
dependencies:
mayr_flutter_extensions: ^1.0.0
Then import it:
import 'package:mayr_flutter_extensions/mayr_flutter_extensions.dart';
β¨ Features
π§± BuildContext Extensions
Quickly access common Flutter objects from BuildContext:
Navigation & State Access
// Easy access to common Flutter objects
context.navigator; // Navigator.of(context)
context.scaffold; // Scaffold.of(context)
context.form; // Form.of(context)
context.overlay; // Overlay.of(context)
context.scaffoldMessenger; // ScaffoldMessenger.of(context)
SnackBar Helper
// Show a snackbar with one line
context.showSnackBar(
'Hello World!',
duration: Duration(seconds: 2),
behavior: SnackBarBehavior.floating,
);
Media Query
// Access MediaQueryData
context.mediaQuery;
context.widgetSize; // Size of the widget
context.widgetWidth; // Width only
context.widgetHeight; // Height only
context.widgetShortestSide; // Shortest side (useful for responsive design)
Theme & Brightness
// Platform brightness
context.platformBrightness; // Brightness.dark or Brightness.light
context.platformInDarkMode; // true if dark mode
context.platformInLightMode; // true if light mode
Device Type Detection
// Responsive design helpers
context.orientation; // Portrait or landscape
context.isPortrait; // true if portrait
context.isLandscape; // true if landscape
// Device type checks
context.isPhone; // Screen width < 600
context.isSmallTablet; // 600 <= width < 720
context.isLargeTablet; // 720 <= width < 1024
context.isTablet; // isSmallTablet || isLargeTablet
context.isDesktop; // width >= 1024
π¨ Widget Extensions
Basic Transformations
// Center a widget
Text('Hello').center();
// Make widget expanded
Container().expanded(2); // flex = 2
// Make widget flexible
Container().flexible(flex: 1);
// Add opacity
Container().opacity(0.5);
// Wrap with SizedBox
Text('Hello').sizedBox(width: 100, height: 50);
// Add constraints
Container().constrained(
maxWidth: 200,
maxHeight: 300,
);
Padding
// Add padding
Widget().paddingAll(16);
Widget().paddingSymmetric(horizontal: 20, vertical: 10);
Widget().paddingOnly(left: 8, top: 16);
Widget().paddingZero();
Clipping
// Clip widget
Container().clipRect();
Container().clipRRect(BorderRadius.circular(12));
Container().clipRounded(16); // Quick rounded corners
Positioning
// Position with Align
Widget().positionAlign(Alignment.center);
// Fill parent with Positioned.fill
Widget().positionedFill();
Visibility Helpers
final isLoggedIn = true;
final isLoading = false;
// Conditional visibility
Text('Welcome!').showIf(isLoggedIn);
Text('Please login').hideIf(isLoggedIn);
Text('Content').showUnless(isLoading);
Text('Loading...').hideUnless(isLoading);
Tap Interactions with InkWellManager
Easily add tap, double-tap, and long-press interactions to any widget:
// Simple tap
Text('Click me')
.inkWellManager(() => print('Tapped!'))
.onTap();
// With custom splash color
Container(
padding: EdgeInsets.all(16),
child: Text('Tap me'),
).inkWellManager(
() => print('Tapped!'),
color: Colors.blue,
).onTap();
// Long press
Widget().inkWellManager(() => print('Long pressed'))
.onLongPress();
// Double tap
Widget().inkWellManager(() => print('Double tapped'))
.onDoubleTap();
Why InkWellManager?
Instead of manually wrapping widgets in InkWell every time, InkWellManager provides quick .onTap(), .onDoubleTap(), and .onLongPress() methods β making your code shorter, cleaner, and more maintainable.
πΌοΈ Image Extensions
Quickly create CircleAvatar from Image widgets:
Image.network('https://example.com/avatar.jpg')
.circleAvatar(
radius: 40,
backgroundColor: Colors.grey,
);
π Complete Example
import 'package:flutter/material.dart';
import 'package:mayr_flutter_extensions/mayr_flutter_extensions.dart';
class MyScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Demo')),
body: Column(
children: [
// Show info based on device type
Text('Device: ${context.isPhone ? "Phone" : "Tablet"}')
.paddingAll(16),
// Responsive container
Container(
color: Colors.blue,
child: Text('Hello'),
)
.paddingAll(8)
.opacity(0.8)
.clipRounded(12)
.center(),
// Conditional visibility
Text('Dark mode enabled!')
.showIf(context.platformInDarkMode),
// Tap interaction
Container(
padding: EdgeInsets.all(16),
color: Colors.amber,
child: Text('Tap me'),
)
.inkWellManager(() {
context.showSnackBar('Tapped!');
})
.onTap(),
],
),
);
}
}
π€ Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
Quick Start for Contributors
- Fork the repository
- Create a feature branch
- Make your changes (remember: Flutter-specific extensions only!)
- Add tests for your changes
- Run tests:
flutter test - Format code:
dart format . - Check for issues:
dart analyze . - Submit a pull request
π License
This package is licensed under the MIT License. See LICENSE for details.
Copyright (c) 2025 MayR Labs
π Support the Project
If you find this package helpful:
- β Star the repository on GitHub
- π Like it on pub.flutter-io.cn
- π Report issues or suggest features
- π Contribute with pull requests
- π’ Share it with other Flutter developers
π Links
- Package: pub.flutter-io.cn/packages/mayr_flutter_extensions
- Repository: github.com/MayR-Labs/mayr_flutter_extensions
- Related Package: mayr_dart_extensions
- Organization: MayR Labs
- Issues: Report a bug or request a feature
Made with β€οΈ by MayR Labs