show static method

void show(
  1. BuildContext context,
  2. String message, {
  3. Color backgroundColor = Colors.white,
  4. Color textColor = Colors.black,
  5. IconData icon = Icons.info_outline,
  6. Color iconColor = Colors.black,
  7. double fontSize = 16.0,
  8. String fontFamily = 'Poppins',
  9. double borderRadius = 15.0,
  10. double verticalPadding = 12.0,
  11. double horizontalPadding = 24.0,
  12. Duration duration = const Duration(seconds: 3),
  13. Alignment alignment = Alignment.bottomCenter,
  14. double offset = 50.0,
  15. Color shadowColor = Colors.black,
  16. double shadowSpreadRadius = 2.0,
  17. double shadowBlurRadius = 5.0,
  18. 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.

  • backgroundColor sets the background color of the toast. Default is white.
  • textColor sets the color of the text. Default is black.
  • icon is the icon displayed in the toast. Default is Icons.info_outline.
  • iconColor sets the color of the icon. Default is black.
  • fontSize sets the font size of the message text. Default is 16.0.
  • fontFamily sets the font family of the message text. Default is 'Poppins'.
  • borderRadius sets the corner radius of the toast container. Default is 15.0.
  • verticalPadding sets the vertical padding inside the toast. Default is 12.0.
  • horizontalPadding sets the horizontal padding inside the toast. Default is 24.0.
  • duration sets how long the toast will be displayed. Default is 3 seconds.
  • alignment sets the position of the toast on the screen. Default is Alignment.bottomCenter.
  • offset sets the offset from the edge based on the alignment. Default is 50.0.
  • shadowColor sets the color of the shadow behind the toast. Default is black.
  • shadowSpreadRadius sets the spread radius of the shadow. Default is 2.0.
  • shadowBlurRadius sets the blur radius of the shadow. Default is 5.0.
  • shadowOffset sets 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();
  });
}