Snackbar Utility Extension

This extension adds snackbar functionality directly to the BuildContext, allowing for quick and easy display of messages.

Usage

context.showSuccessSnackbar("Operation successful!");

Benefits

  • Improves code readability.
  • Reduces boilerplate code for snackbar creation.

Full Code

import 'package:flutter/material.dart';

extension SnackbarUtils on BuildContext {
  void showSnackbar(String message, {Color backgroundColor = Colors.black}) {
    final snackBar = SnackBar(
      content: Text(message),
      backgroundColor: backgroundColor,
    );
    ScaffoldMessenger.of(this).showSnackBar(snackBar);
  }

  void showErrorSnackbar(String message) {
    showSnackbar(message, backgroundColor: Colors.red);
  }

  void showSuccessSnackbar(String message) {
    showSnackbar(message, backgroundColor: Colors.green);
  }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x