one_context 0.1.0 one_context: ^0.1.0 copied to clipboard
Dialogs, Overlays and Navigations without BuildContext
One Context to rule them all
Show Dialogs, Overlays and Navigate without BuildContext. #
BuildContext always is needed (in some cases we need to choose carefully the specific one to make things work as expected), but, to global things, like dialogs, we shoudn't have to worry about (it's makes sense in my mind haha) and because of that, it is being encapsulated by the package, to we don't have to worry about it. 🎯
🎮 Let's start #
There are 2 ways to get OneContext singleton instance, OneContext() or OnceContext.intance. e.g.
OneContext().pushNamed('/detail_page');
OneContext.instance.pushNamed('/detail_page');
There are controllers to use navigation, overlays and dialogs. e.g.
OneContext().navigator.pushNamed(...);
OneContext().dialog.showDialog(...);
OneContext().overlay.addOverlay(...);
Or, you can use the shortcuts ;)
OneContext().pushNamed(...);
OneContext().showDialog(...);
OneContext().addOverlay(...);
💬 How to show Dialogs without BuildContext? #
// example snackBar
OneContext().showSnackBar(
builder: (_) => SnackBar(content: Text('My awesome snackBar!'))
);
// example dialog
OneContext().showDialog(
// barrierDismissible: false,
builder: (_) => AlertDialog(
title: new Text("The Title"),
content: new Text("The Body"),
)
);
// example bottomSheet
OneContext().showBottomSheet(
builder: (context) => Container(
alignment: Alignment.topCenter,
height: 200,
child: IconButton(
icon: Icon(Icons.arrow_drop_down),
iconSize: 50,
color: Colors.white,
onPressed: () => Navigator.of(context).pop('sucess')), // or OneContext().popDialog('sucess');
),
);
// example modalBottomSheet
OneContext().showModalBottomSheet<String>(
builder: (context) => Container(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
leading: Icon(Icons.music_note),
title: Text('Music'),
onTap: () => OneContext().popDialog('Music'); //Navigator.of(context).pop('Music')),
ListTile(
leading: Icon(Icons.videocam),
title: Text('Video'),
onTap: () => Navigator.of(context).pop('Video'),
),
SizedBox(height: 45)
],
),
)
);
⛵ How to navigate? #
// go to second page using named route
OneContext().pushNamed('/second');
// go to second page using MaterialPageRoute
OneContext().push(MaterialPageRoute(builder: (_) => SecondPage()));
// go back from second page
OneContext().pop();
// Retrieve data from route when it's pops
String result = await OneContext().push<String>(MaterialPageRoute(builder: (_) => SecondPage()));
print(result);
🍰 How to show Overlays? #
// show the default progress indicator
OneContext().showProgressIndicator();
// hide the default progress indicator
OneContext().hideProgressIndicator();
// show the default progress indicator with some colors
OneContext().showProgressIndicator(
backgroundColor: Colors.blue.withOpacity(.3),
circularProgressIndicatorColor: Colors.white
);
// Later
OneContext().hideProgressIndicator();
// Show a custom progress indicator
OneContext().showProgressIndicator(
builder: (_) => MyAwesomeProgressIndicator();
);
// Later
OneContext().hideProgressIndicator();
// Show a custom widget in overlay stack
String myCustomAndAwesomeOverlayId = UniqueKey().toString();
OneContext().addOverlay(
overlayId: myCustomAndAwesomeOverlayId,
builder: (_) => MyCustomAndAwesomeOverlay()
);
// Later
OneContext().removeOverlay(myCustomAndAwesomeOverlayId);
⚠ Important: Configure MaterialApp. e.g. #
/// important: Use [OneContext().builder] in `MaterialApp` builder, in order to show dialogs and overlays.
/// important: Use [OneContext().key] in `MaterialApp` navigatorKey, in order to navigate.
return MaterialApp(
builder: OneContext().builder,
navigatorKey: OneContext().key,
...
);
Add dependency
dependencies:
one_context: ^0.1.0
👨💻👨💻 Contributing #
Contributions of any kind are welcome! I'll be glad to analyse and accept them! 👾
If you have any question about the project:
Email-me: fastencoding@gmail.com
Connect with me at LinkedIn.