124 lines
3.8 KiB
Dart
124 lines
3.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
class SnackBarUtils {
|
|
static final scaffoldKey = GlobalKey<ScaffoldMessengerState>();
|
|
|
|
static void showError(String error) {
|
|
_showBasicSnackBar(error,
|
|
textColor: Colors.black,
|
|
snackbarColor: Colors.amber,
|
|
snackbarIcon: Icons.error_outline);
|
|
}
|
|
|
|
static void showSuccess(String message) {
|
|
_showBasicSnackBar(message, snackbarColor: Colors.green);
|
|
}
|
|
|
|
static void showLoading(String message) {
|
|
_showBasicSnackBar(message,
|
|
textColor: Colors.black,
|
|
iconColor: Colors.black,
|
|
snackbarColor: Colors.white);
|
|
}
|
|
|
|
static void showLoadingWithProgress(String message,
|
|
{required ValueNotifier<double> notifier}) {
|
|
_showProgressBar(message, notifier: notifier);
|
|
}
|
|
|
|
static void hideSnackBar() {
|
|
scaffoldKey.currentState?.hideCurrentSnackBar();
|
|
}
|
|
|
|
static void _showBasicSnackBar(String message,
|
|
{Color textColor = Colors.black,
|
|
Color snackbarColor = Colors.teal,
|
|
IconData snackbarIcon = Icons.info_outline,
|
|
Color iconColor = Colors.black}) {
|
|
scaffoldKey.currentState?.showSnackBar(
|
|
SnackBar(
|
|
behavior: SnackBarBehavior.floating,
|
|
duration: Duration(seconds: 10),
|
|
width: 500,
|
|
content: Container(
|
|
margin: const EdgeInsets.only(bottom: 25),
|
|
padding: const EdgeInsets.all(8.0),
|
|
decoration: BoxDecoration(
|
|
color: snackbarColor,
|
|
borderRadius: BorderRadius.circular(30.0),
|
|
border: Border.all(color: Colors.black, width: 3),
|
|
),
|
|
child: Row(
|
|
children: [
|
|
Icon(snackbarIcon, color: iconColor),
|
|
const SizedBox(width: 8),
|
|
Expanded(
|
|
child: Text(
|
|
message,
|
|
style:
|
|
TextStyle(color: textColor, fontWeight: FontWeight.w500),
|
|
maxLines: 3,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
backgroundColor: Colors.transparent,
|
|
elevation: 0.0,
|
|
),
|
|
);
|
|
}
|
|
|
|
static void _showProgressBar(
|
|
String message, {
|
|
required ValueNotifier<double> notifier,
|
|
Color textColor = Colors.black,
|
|
Color snackbarColor = Colors.amber,
|
|
IconData snackbarIcon = Icons.info_outline,
|
|
Color iconColor = Colors.black,
|
|
}) {
|
|
double height = 15.0;
|
|
|
|
scaffoldKey.currentState?.showSnackBar(
|
|
SnackBar(
|
|
behavior: SnackBarBehavior.floating,
|
|
width: 500,
|
|
duration: const Duration(days: 1),
|
|
content: Container(
|
|
margin: const EdgeInsets.only(bottom: 25),
|
|
padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 15),
|
|
decoration: BoxDecoration(
|
|
color: snackbarColor,
|
|
borderRadius: BorderRadius.circular(30.0),
|
|
border: Border.all(color: Colors.black, width: 3),
|
|
),
|
|
child: ValueListenableBuilder(
|
|
valueListenable: notifier,
|
|
builder: (_, double value, Widget? child) {
|
|
return Row(
|
|
children: [
|
|
Icon(snackbarIcon, color: iconColor),
|
|
const SizedBox(width: 8),
|
|
Text(message,
|
|
style: TextStyle(color: textColor), maxLines: 3),
|
|
const SizedBox(width: 8),
|
|
Expanded(
|
|
child: LinearProgressIndicator(
|
|
backgroundColor: Colors.amber,
|
|
color: Colors.white38,
|
|
minHeight: height,
|
|
value: value,
|
|
borderRadius: BorderRadius.circular(30.0),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
),
|
|
backgroundColor: Colors.transparent,
|
|
elevation: 0.0,
|
|
),
|
|
);
|
|
}
|
|
} |