Snackbar Handler

Let's create a Snackbar for temporarily displaying a message when a task has succeeded or failed.

In the note_app/modules/presentation/lib/common/ directory, create a file named snackbar_handler.dart containing some utility methods for displaying an error or an info snackbar:

import 'package:flutter/material.dart';

/// {@template SnackBarHandler}
/// Provides a suite of snackbars to display an error or info to the user.
/// {@endtemplate}
class SnackBarHandler {
  /// {@macro SnackBarHandler}
  SnackBarHandler._();

  /// Shows an info snackbar.
  static void info(BuildContext context, String message) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text(message)),
    );
  }

  /// Shows an error snackbar.
  static void error(BuildContext context, String message) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(
          message,
          style: Theme.of(context)
              .textTheme
              .bodyMedium
              ?.copyWith(color: Theme.of(context).colorScheme.onError),
        ),
        backgroundColor: Theme.of(context).colorScheme.error,
      ),
    );
  }
}

Last updated