π§© Context Helpers
A lightweight Flutter utility package providing expressive and powerful extensions on BuildContext.
Simplify access to MediaQuery, Theme, Navigation, Localization, and Responsive utilities β making your UI code cleaner, faster, and more readable.
β¨ Features
| Feature | Description |
|---|---|
| π Quick Size Access | Replace MediaQuery.of(context).size.width with context.width |
| π¨ Theme Shortcuts | Access theme and color schemes instantly via context.theme or context.colorScheme |
| π§ Simplified Navigation | Push, pop, and replace routes with one-liners |
| π Localization Access | Get current locale, language, and region easily |
| π± Responsive Utilities | Detect context.isMobile, context.isTablet, context.isDesktop |
| β‘ SnackBar Helper | Show messages instantly using context.showSnackBar() |
| π§ Intuitive Syntax | Readable, concise, and fully null-safe |
π Getting Started
Installation
Add context_helpers to your pubspec.yaml:
dependencies:
context_helpers: ^1.0.0
Then run:
flutter pub get
Import
import 'package:context_helpers/context_helpers.dart';
π‘ Usage Examples
Basic Example
import 'package:flutter/material.dart';
import 'package:context_helpers/context_helpers.dart';
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Width: ${context.width.toStringAsFixed(0)}'),
backgroundColor: context.colorScheme.primary,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Locale Access
Text(
'Locale: ${context.locale.languageCode.toUpperCase()}',
style: context.textTheme.headlineMedium,
),
const SizedBox(height: 20),
// Responsive Detection
Text(
'Device Type: ${context.isMobile ? "π± Mobile" : context.isTablet ? "π± Tablet" : "π» Desktop"}',
style: context.textTheme.titleLarge,
),
const SizedBox(height: 30),
// SnackBar Helper
ElevatedButton(
onPressed: () => context.showSnackBar('Hello from Context Helpers! π'),
child: const Text('Show Snackbar'),
),
const SizedBox(height: 10),
// Navigation Helper
ElevatedButton(
onPressed: () => context.push(const SecondPage()),
child: const Text('Navigate to Second Page'),
),
],
),
),
);
}
}
class SecondPage extends StatelessWidget {
const SecondPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Second Page')),
body: Center(
child: ElevatedButton(
onPressed: () => context.pop(),
child: const Text('β¬
οΈ Go Back'),
),
),
);
}
}
Responsive Layout Example
Widget build(BuildContext context) {
return Container(
width: context.width,
height: context.height * 0.5,
child: Center(
child: Text(
context.isMobile
? 'π± Mobile Layout'
: context.isTablet
? 'π± Tablet Layout'
: 'π» Desktop Layout',
style: context.textTheme.headlineSmall?.copyWith(
color: context.colorScheme.primary,
),
),
),
);
}
Theme Access Example
Container(
color: context.colorScheme.primaryContainer,
child: Text(
'Styled with Theme Colors',
style: context.textTheme.bodyLarge?.copyWith(
color: context.colorScheme.onPrimaryContainer,
),
),
)
Localization Example
Column(
children: [
Text('Language: ${context.languageCode}'),
Text('Country: ${context.countryCode}'),
Text('Full Locale: ${context.locale}'),
],
)
βοΈ Complete API Reference
π Size & Dimensions
| Extension | Type | Description | Example |
|---|---|---|---|
context.width |
double |
Screen width | context.width β 390.0 |
context.height |
double |
Screen height | context.height β 844.0 |
π¨ Theme Access
| Extension | Type | Description |
|---|---|---|
context.theme |
ThemeData |
Current theme data |
context.colorScheme |
ColorScheme |
Material color scheme |
context.textTheme |
TextTheme |
Typography theme |
π§ Navigation
| Extension | Return Type | Description | Example |
|---|---|---|---|
context.navigator |
NavigatorState |
Direct navigator access | context.navigator.pushNamed('/home') |
context.push(page) |
Future<T?> |
Push new route | context.push(DetailPage()) |
context.pop() |
void |
Pop current route | context.pop() |
π Localization
| Extension | Type | Description | Example |
|---|---|---|---|
context.locale |
Locale |
Current locale | Locale('en', 'US') |
context.languageCode |
String |
Language code | 'en' |
context.countryCode |
String? |
Country code | 'US' |
π± Responsive Utilities
| Extension | Type | Breakpoint | Description |
|---|---|---|---|
context.isMobile |
bool |
< 600px |
Returns true for mobile devices |
context.isTablet |
bool |
β₯ 600px & < 1024px |
Returns true for tablets |
context.isDesktop |
bool |
β₯ 1024px |
Returns true for desktop screens |
β‘ UI Helpers
| Extension | Parameters | Description |
|---|---|---|
context.showSnackBar(message) |
String message |
Display a snackbar with message |
π§ Setup for Localization
To use localization features, configure your MaterialApp:
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:context_helpers/context_helpers.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Context Helpers Demo',
// Add localization delegates
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
// Define supported locales
supportedLocales: const [
Locale('en', 'US'),
Locale('hi', 'IN'),
Locale('es', 'ES'),
],
home: const HomePage(),
);
}
}
π§ Platform Support
| Platform | Supported |
|---|---|
| Android | β |
| iOS | β |
| Web | β |
| macOS | β |
| Windows | β |
| Linux | β |
π Troubleshooting
Error: Undefined name 'GlobalWidgetsLocalizations'
Solution: Add the import at the top of your file:
import 'package:flutter_localizations/flutter_localizations.dart';
Error: No MaterialLocalizations found
Solution: Ensure you've added localizationsDelegates and supportedLocales in your MaterialApp:
MaterialApp(
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: const [Locale('en', 'US')],
// ...
)
π€ Contributing
Contributions are welcome! If you'd like to add new helpers or improve existing ones:
- Fork this repository
- Create a feature branch (
git checkout -b feature/amazing-helper) - Commit your changes (
git commit -m 'Add amazing helper') - Push to the branch (
git push origin feature/amazing-helper) - Open a Pull Request
Ideas for Contributions
context.deviceType- Enhanced device detectioncontext.safePadding- Safe area padding accesscontext.responsive()- Widget builder for responsive layoutscontext.textScale- Text scaling factor- More theme shortcuts and utilities
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π§βπ» Author
Rahul Sharma
- π GitHub: @rahulk3sharmadev
- π§ Email: rahulk3sharmadev@gmail.com
- πΌ LinkedIn: Rahul Sharma
π Support This Package
If you find Context Helpers useful:
- β Star this repo on GitHub
- π Like it on pub.flutter-io.cn
- π Report issues or request features here
- π’ Share with the Flutter community
πΊοΈ Roadmap
Addcontext.responsive()widget builderAddcontext.textScaleshortcutAddcontext.safeAreaandcontext.insethelpersAddcontext.appThemefor theme extensionsAddcontext.mediaQueryfor full MediaQueryData accessComprehensive unit tests for all helpersAdd more navigation helpers (pushReplacement, pushAndRemoveUntil)
π Changelog
See CHANGELOG.md for a detailed version history.
π Acknowledgments
Inspired by community-driven context extensions from:
Special thanks to the Flutter community for continuous inspiration and excellent open-source practices.
Made with β€οΈ by Rahul Sharma
Happy Coding! π