example for overlay_support
popup a notification or toast:
showSimpleNotification(Text("this is a message from simple notification"),
background: Colors.green);
toast('this is a message from toast');
you can custom your notification widget to popup, for example:
showOverlayNotification((context) {
return MessageNotification(
message: 'i love you',
onReplay: () {
OverlaySupportEntry.of(context).dismiss(); //use OverlaySupportEntry to dismiss overlay
toast('you checked this message');
},
);
class MessageNotification extends StatelessWidget {
final VoidCallback onReplay;
const MessageNotification({Key key, this.onReplay}) : super(key: key);
@override
Widget build(BuildContext context) {
return Card(
margin: const EdgeInsets.symmetric(horizontal: 4),
child: SafeArea(
child: ListTile(
leading: SizedBox.fromSize(
size: const Size(40, 40),
child: ClipOval(child: Image.asset('assets/avatar.png'))),
title: Text('Lily MacDonald'),
subtitle: Text('Do you want to see a movie?'),
trailing: IconButton(
icon: Icon(Icons.reply),
onPressed: () {
///TODO i'm not sure it should be use this widget' BuildContext to create a Dialog
///maybe i will give the answer in the future
if (onReplay != null) onReplay();
}),
),
),
);
}
}
for example, we need create a iOS Style Toast.
showOverlay((context, t) {
return Opacity(
opacity: t,
child: IosStyleToast(),
);
});
class IosStyleToast extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SafeArea(
child: DefaultTextStyle(
style: Theme.of(context).textTheme.body1.copyWith(color: Colors.white),
child: Padding(
padding: const EdgeInsets.all(16),
child: Center(
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Container(
color: Colors.black87,
padding: const EdgeInsets.symmetric(
vertical: 8,
horizontal: 16,
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(
Icons.check,
color: Colors.white,
),
Text('Succeed')
],
),
),
),
),
),
),
);
}
}
overlay could be binded to BuildContext so it's not shown when we push another route above and is shown again when we pop it.
for it you should use OverlaySupport.local, wrap your screen with it and then use context which is under OverlaySupport.local to showOverlay
class OverlaySupportExample extends StatelessWidget {
const OverlaySupportExample({Key key}) : super(key: key);
@override
Widget build(BuildContext upperContext) {
return OverlaySupport.local(
child: Builder(builder: (context) {/// be sure you use this context and not upperContext
return Card(
margin: const EdgeInsets.symmetric(horizontal: 4),
child: SafeArea(
child: ListTile(
leading: SizedBox.fromSize(
size: const Size(40, 40),
child: ClipOval(child: Image.asset('assets/avatar.png'))),
title: Text('Lily MacDonald'),
subtitle: Text('Do you want to see a movie?'),
trailing: IconButton(
icon: Icon(Icons.reply),
onPressed: () {
showOverlayNotification(
context: context,
duration: Duration.zero,
(context) {
return Container();
},
);
},
),
),
),
);
}),
);
}
}
if you have some suggestion or advice, please open an issue to let me known. This will greatly help the improvement of the usability of this project. Thanks.