show static method
void
show(
- BuildContext context,
- String message, {
- Color backgroundColor = Colors.white,
- Color textColor = Colors.black,
- IconData icon = Icons.info_outline,
- Color iconColor = Colors.black,
- double fontSize = 16.0,
- String fontFamily = 'Poppins',
- double borderRadius = 15.0,
- double verticalPadding = 12.0,
- double horizontalPadding = 24.0,
- Duration duration = const Duration(seconds: 3),
- Alignment alignment = Alignment.bottomCenter,
- double offset = 50.0,
- Color shadowColor = Colors.black,
- double shadowSpreadRadius = 2.0,
- double shadowBlurRadius = 5.0,
- Offset shadowOffset = const Offset(0, 3),
Displays a toast message.
context is required to find the Overlay and show the toast.
message is the text message that will be displayed in the toast.
Optional parameters allow customization of the toast's appearance and behavior.
backgroundColorsets the background color of the toast. Default is white.textColorsets the color of the text. Default is black.iconis the icon displayed in the toast. Default is Icons.info_outline.iconColorsets the color of the icon. Default is black.fontSizesets the font size of the message text. Default is 16.0.fontFamilysets the font family of the message text. Default is 'Poppins'.borderRadiussets the corner radius of the toast container. Default is 15.0.verticalPaddingsets the vertical padding inside the toast. Default is 12.0.horizontalPaddingsets the horizontal padding inside the toast. Default is 24.0.durationsets how long the toast will be displayed. Default is 3 seconds.alignmentsets the position of the toast on the screen. Default is Alignment.bottomCenter.offsetsets the offset from the edge based on the alignment. Default is 50.0.shadowColorsets the color of the shadow behind the toast. Default is black.shadowSpreadRadiussets the spread radius of the shadow. Default is 2.0.shadowBlurRadiussets the blur radius of the shadow. Default is 5.0.shadowOffsetsets the offset of the shadow. Default is Offset(0, 3).
Implementation
static void show(
BuildContext context,
String message, {
Color backgroundColor = Colors.white, // Default background color
Color textColor = Colors.black, // Default text color
IconData icon = Icons.info_outline, // Default icon
Color iconColor = Colors.black, // Default icon color
double fontSize = 16.0, // Default font size
String fontFamily = 'Poppins', // Default font family
double borderRadius = 15.0, // Default border radius
double verticalPadding = 12.0, // Default vertical padding
double horizontalPadding = 24.0, // Default horizontal padding
Duration duration = const Duration(seconds: 3), // Default duration
Alignment alignment = Alignment.bottomCenter, // Default position
double offset = 50.0, // Default offset from the bottom
Color shadowColor = Colors.black, // Default shadow color
double shadowSpreadRadius = 2.0, // Default shadow spread radius
double shadowBlurRadius = 5.0, // Default shadow blur radius
Offset shadowOffset = const Offset(0, 3), // Default shadow offset
}) {
// Find the overlay for the current context
final overlay = Overlay.of(context);
// Create an overlay entry to show the toast
final overlayEntry = OverlayEntry(
builder: (context) => Align(
alignment: alignment, // Align the toast based on the provided alignment
child: Container(
margin: EdgeInsets.only(
bottom: alignment == Alignment.bottomCenter
? offset
: 0, // Bottom offset
top: alignment == Alignment.topCenter ? offset : 0, // Top offset
),
width: MediaQuery.of(context).size.width *
0.8, // Set width relative to screen width
child: Material(
color: Colors.transparent, // Use transparent material for overlay
child: Container(
padding: EdgeInsets.symmetric(
horizontal:
horizontalPadding, // Horizontal padding inside the toast
vertical: verticalPadding, // Vertical padding inside the toast
),
decoration: BoxDecoration(
color: backgroundColor, // Background color of the toast
borderRadius: BorderRadius.circular(
borderRadius), // Corner radius of the toast
boxShadow: [
BoxShadow(
color: shadowColor
.withOpacity(0.2), // Shadow color with opacity
spreadRadius:
shadowSpreadRadius, // Spread radius of the shadow
blurRadius: shadowBlurRadius, // Blur radius of the shadow
offset: shadowOffset, // Offset of the shadow
),
],
),
child: Row(
mainAxisAlignment:
MainAxisAlignment.center, // Center the row content
children: [
Icon(
icon, // Icon displayed in the toast
color: iconColor, // Color of the icon
),
const SizedBox(width: 8.0), // Space between icon and text
Expanded(
child: Text(
message, // Message displayed in the toast
style: TextStyle(
color: textColor, // Text color
fontSize: fontSize, // Font size
fontFamily: fontFamily, // Font family
),
textAlign: TextAlign.center, // Center align the text
),
),
],
),
),
),
),
),
);
// Insert the overlay entry into the overlay
overlay.insert(overlayEntry);
// Remove the overlay entry after the specified duration
Future.delayed(duration, () {
overlayEntry.remove();
});
}