Contextions is a Flutter package which makes use of extension methods on BuildContext to easily access Navigator functions, theme functions, and other BuildContext-based functions. This package enhances convenience and readability in Flutter app development.
Easily navigate to new pages and manage navigation stack:
context.to(YourPage());
context.navigateAndReplace(YourPage());
context.pop();
Access theme data and media query properties directly from BuildContext
:
ThemeData theme = context.theme;
TextTheme textTheme = context.textTheme;
Size screenSize = context.screenSize;
double screenHeight = context.screenHeight;
double screenWidth = context.screenWidth;
Show snackbar messages with a single line of code:
context.showSnackbar('Message');
Display dialogs with custom content and actions:
context.showDialogX(
AlertDialog(
title: Text('Dialog Title'),
content: Text('Dialog Content'),
actions: [
TextButton(
onPressed: () => context.pop(),
child: Text('OK'),
),
],
),
);
Open and close the drawer:
context.openDrawer();
context.closeDrawer();
Show a modal bottom sheet:
context.showModalBottomSheetX(
Container(
child: Text('Bottom Sheet Content'),
),
);
Show a customizable bottom sheet:
context.showBottomSheetX(
builder: (BuildContext context) => Container(
child: Text('Custom Bottom Sheet Content'),
),
);
Show a search interface:
context.showSearchX(
delegate: CustomSearchDelegate(),
);
Show a dialog with custom content and actions:
context.showDialogXY(
title: 'Custom Dialog Title',
content: 'Custom Dialog Content',
actions: [
TextButton(
onPressed: () => context.pop(),
child: Text('OK'),
),
],
);
Check if the keyboard is visible:
bool isKeyboardVisible = context.isKeyboardVisible;
Check if the screen is in landscape or portrait mode:
bool isLandscape = context.isLandscape;
bool isPortrait = context.isPortrait;
Here's a basic example demonstrating the usage of contextions
:
import 'package:flutter/material.dart';
import 'package:contextions/context_extensions.dart';
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Context Extensions Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
context.showSnackbar('Hello from Context Extensions!');
},
child: Text('Show Snackbar'),
),
),
);
}
}
void main() {
runApp(MaterialApp(
home: MyWidget(),
));
}