Created
April 12, 2020 06:56
-
-
Save prafullakumar/1babee37427508881cafacb8854012ab to your computer and use it in GitHub Desktop.
show any kind of toast with example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'toast.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
title: 'Toast', | |
theme: new ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: new MyHomePage(title: 'Toast Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyAppState createState() => new _MyAppState(); | |
} | |
class _MyAppState extends State<MyHomePage> { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar( | |
title: const Text('Toast Example'), | |
), | |
body: Center( | |
child: Column( | |
children: <Widget>[ | |
ListTile( | |
title: Text("Top Toast"), | |
onTap: () { | |
ToastUtil.show( | |
ToastDecorator( | |
widget: Text( | |
"warning message", | |
style: TextStyle(color: Colors.white) | |
), | |
backgroundColor: Colors.red, | |
), | |
context, | |
gravity: ToastGravity.top); | |
}, | |
), | |
ListTile( | |
title: Text("Imgae with message Toast"), | |
onTap: () { | |
ToastUtil.show( | |
ToastDecorator( | |
widget: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: <Widget>[ | |
const ListTile( | |
leading: Icon(Icons.album, size: 50), | |
title: Text('Heart Shaker', style: TextStyle(color: Colors.white)), | |
subtitle: Text('TWICE', style: TextStyle(color: Colors.white)), | |
), | |
], | |
), | |
backgroundColor: Colors.greenAccent, | |
), | |
context, | |
gravity: ToastGravity.top); | |
}, | |
), | |
ListTile( | |
title: Text("Bottom Toast"), | |
onTap: () { | |
ToastUtil.show( | |
ToastDecorator( | |
widget: Text( | |
"info message", | |
style: TextStyle(color: Colors.white) | |
), | |
backgroundColor: Colors.black, | |
), | |
context, | |
gravity: ToastGravity.bottom); | |
}, | |
), | |
ListTile( | |
title: Text("Very Long Length Toast"), | |
onTap: () { | |
ToastUtil.show( | |
ToastDecorator( | |
widget: Text( | |
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean dignissim sem odio, id tempor erat laoreet vel. In aliquam purus vitae tristique iaculis. Curabitur sem orci, tristique accumsan vehicula id, bibendum vel nibh. Morbi et ipsum tortor. Nam eget enim in sapien malesuada pharetra. Proin ultrices enim at facilisis hendrerit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras sagittis ligula sollicitudin fermentum egestas. Nam consequat, felis et cursus semper, sapien urna vehicula turpis, vel sollicitudin ligula sapien sed augue.", | |
style: TextStyle(color: Colors.white) | |
), | |
backgroundColor: Colors.black, | |
), | |
context, | |
gravity: ToastGravity.bottom); | |
}, | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
enum ToastGravity { | |
bottom, | |
center, | |
top | |
} | |
class ToastDecorator extends StatelessWidget { | |
final Widget widget; | |
final Color backgroundColor; | |
final Border border; | |
final EdgeInsets margin; | |
final EdgeInsets padding; | |
final BorderRadius borderRadius; | |
ToastDecorator({ | |
@required this.widget, | |
this.backgroundColor = Colors.black, | |
this.border = const Border(), | |
this.margin = const EdgeInsets.symmetric(horizontal: 20), | |
this.padding = const EdgeInsets.fromLTRB(16, 10, 16, 10), | |
this.borderRadius = const BorderRadius.all(Radius.circular(20.0)) | |
}) {} | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
width: MediaQuery.of(context).size.width, | |
child: Container( | |
alignment: Alignment.center, | |
width: MediaQuery.of(context).size.width, | |
child: Container( | |
decoration: BoxDecoration( | |
color: backgroundColor, | |
borderRadius: this.borderRadius, | |
border: this.border, | |
), | |
margin: margin, | |
padding: padding, | |
child: widget, | |
) | |
) | |
); | |
} | |
} | |
class ToastUtil { | |
static void show(Widget widget, BuildContext context, | |
{int duration = 2, | |
ToastGravity gravity = ToastGravity.bottom, | |
}) { | |
_ToastView.dismiss(); | |
_ToastView.createViewToast(context, duration, gravity, widget); | |
} | |
} | |
class _ToastView { | |
static final _ToastView _singleton = new _ToastView._internal(); //singleton | |
factory _ToastView() { | |
return _singleton; | |
} | |
_ToastView._internal(); | |
static OverlayState overlayState; | |
static OverlayEntry _overlayEntry; | |
static bool _isVisible = false; | |
static void createViewToast( | |
BuildContext context, | |
int duration, | |
ToastGravity gravity, | |
Widget widget, | |
) async { | |
overlayState = Overlay.of(context); | |
_overlayEntry = OverlayEntry( | |
builder: (BuildContext context) => _ToastWidget( | |
widget: widget, | |
gravity: gravity, | |
) | |
); | |
_isVisible = true; | |
overlayState.insert(_overlayEntry); | |
await new Future.delayed(Duration( | |
seconds: duration)); | |
dismiss(); | |
} | |
static dismiss() async { | |
if (!_isVisible) { | |
return; | |
} | |
_isVisible = false; | |
_overlayEntry?.remove(); | |
} | |
} | |
class _ToastWidget extends StatelessWidget { | |
_ToastWidget({ | |
Key key, | |
@required this.widget, | |
@required this.gravity, | |
}) : super(key: key); | |
final Widget widget; | |
final ToastGravity gravity; | |
@override | |
Widget build(BuildContext context) { | |
return new Positioned( | |
top: gravity == ToastGravity.top ? MediaQuery.of(context).viewInsets.top + 50 : null, | |
bottom: | |
gravity == ToastGravity.bottom ? MediaQuery.of(context).viewInsets.bottom + 50 : null, | |
child: Material( | |
color: Colors.transparent, | |
child: widget, | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment