Access of flutter common context based widgets and properties using the context (BuildContext)
dependencies:
contextual : ^1.0.0
import 'package:contextual/contextual.dart';
import 'package:contextual/contextual.dart';
import 'package:flutter/material.dart';
class App extends StatefulWidget {
const App({super.key});
@override
State<App> createState() => _AppState();
}
class _AppState extends State<App> {
@override
Widget build(BuildContext context) {
final screenSize = context.screenSize;
final mediaQuery = context.mediaQuery;
return Container();
}
}
Property | Equivalent | Description |
---|---|---|
context.mediaQuery |
MediaQuery.of(context) | Returns the mediaquerydata for nearest Mediaquery |
context.size |
MediaQuery.of(context).size | Returns the current screen size |
context.theme |
Theme.of(context) | Get the nearest theme |
context.navigator |
Navigator.of(context) | Returns the nearest Navigator |
context.navigate.to(Widget page) |
Navigator.of(context).push | Navigates to the page argument |
context.showSnackBar(SnackBar snackBar) |
ScaffoldMessenger.of(this).showSnackBar() | Show snackbar |
context.scaffold |
Scaffold.of(context) | Get the nearest Scaffold |
context.overlay |
Overlay.of(context) || Navigator.of(context).overlay | Get the nearest overlay |
context.focus |
Focus.of(context) | Get the nearest Focus widget FocusNode |
context.overlay |
Overlay.of(context) || Navigator.of(context).overlay | Get the nearest overlay |
context.form |
Form.of(context) | Get the nearest form widget |
context.defaultTextStyle |
DefaultTextStyle.of(context) | Get the nearest DefaultTextStyle |
If you encounter any problems feel free to open an issue. If you feel the library is missing a feature, please raise a ticket on Github and I'll look into it. Pull request are also welcome.