import 'package:flutter/material.dart'; extension SnackBarHelper on BuildContext { static const _shape = RoundedRectangleBorder( borderRadius: BorderRadius.all(Radius.circular(12)), ); static const _behavior = SnackBarBehavior.floating; static const _margin = EdgeInsets.symmetric(horizontal: 16, vertical: 8); static const _dismissDirection = DismissDirection.horizontal; bool get _isDark => Theme.of(this).brightness == Brightness.dark; void showSuccessSnackBar(String message) { ScaffoldMessenger.of(this).showSnackBar( SnackBar( content: Text(message, style: TextStyle(color: Theme.of(this).colorScheme.onSurface)), backgroundColor: _isDark ? Colors.green[800] : Colors.green, behavior: _behavior, shape: _shape, margin: _margin, dismissDirection: _dismissDirection, ), ); } void showErrorSnackBar(String message) { ScaffoldMessenger.of(this).showSnackBar( SnackBar( content: Text(message, style: TextStyle(color: Theme.of(this).colorScheme.onSurface)), backgroundColor: _isDark ? Colors.red[900] : Colors.red, behavior: _behavior, shape: _shape, margin: _margin, dismissDirection: _dismissDirection, ), ); } void showWarningSnackBar(String message) { ScaffoldMessenger.of(this).showSnackBar( SnackBar( content: Text(message, style: TextStyle(color: Theme.of(this).colorScheme.onSurface)), backgroundColor: _isDark ? Colors.orange[900] : Colors.orange, behavior: _behavior, shape: _shape, margin: _margin, dismissDirection: _dismissDirection, ), ); } void showInfoSnackBar(String message, {Duration? duration}) { final textColor = Theme.of(this).colorScheme.onInverseSurface; final snackBar = duration != null ? SnackBar( content: Text(message, style: TextStyle(color: textColor)), behavior: _behavior, shape: _shape, margin: _margin, dismissDirection: _dismissDirection, duration: duration) : SnackBar( content: Text(message, style: TextStyle(color: textColor)), behavior: _behavior, shape: _shape, margin: _margin, dismissDirection: _dismissDirection); ScaffoldMessenger.of(this).showSnackBar(snackBar); } }