worldhopper/lib/helpers/snackbar_helper.dart
Felipe M. 47fd749682
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
feat: improved snackbars
2026-02-12 17:49:28 +01:00

73 lines
2.4 KiB
Dart

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);
}
}