immich_toast.dart 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import 'package:flutter/material.dart';
  2. import 'package:fluttertoast/fluttertoast.dart';
  3. enum ToastType { info, success, error }
  4. class ImmichToast {
  5. static show({
  6. required BuildContext context,
  7. required String msg,
  8. ToastType toastType = ToastType.info,
  9. ToastGravity gravity = ToastGravity.TOP,
  10. }) {
  11. final isDarkTheme = Theme.of(context).brightness == Brightness.dark;
  12. final fToast = FToast();
  13. fToast.init(context);
  14. Color _getColor(ToastType type, BuildContext context) {
  15. switch (type) {
  16. case ToastType.info:
  17. return Theme.of(context).primaryColor;
  18. case ToastType.success:
  19. return const Color.fromARGB(255, 78, 140, 124);
  20. case ToastType.error:
  21. return const Color.fromARGB(255, 220, 48, 85);
  22. }
  23. }
  24. Icon _getIcon(ToastType type) {
  25. switch (type) {
  26. case ToastType.info:
  27. return Icon(
  28. Icons.info_outline_rounded,
  29. color: Theme.of(context).primaryColor,
  30. );
  31. case ToastType.success:
  32. return const Icon(
  33. Icons.check_circle_rounded,
  34. color: Color.fromARGB(255, 78, 140, 124),
  35. );
  36. case ToastType.error:
  37. return const Icon(
  38. Icons.error_outline_rounded,
  39. color: Color.fromARGB(255, 240, 162, 156),
  40. );
  41. }
  42. }
  43. fToast.showToast(
  44. child: Container(
  45. padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0),
  46. decoration: BoxDecoration(
  47. borderRadius: BorderRadius.circular(5.0),
  48. color: isDarkTheme ? Colors.grey[900] : Colors.grey[50],
  49. border: Border.all(
  50. color: Colors.black12,
  51. width: 1,
  52. ),
  53. ),
  54. child: Row(
  55. mainAxisSize: MainAxisSize.min,
  56. children: [
  57. _getIcon(toastType),
  58. const SizedBox(
  59. width: 12.0,
  60. ),
  61. Flexible(
  62. child: Text(
  63. msg,
  64. style: TextStyle(
  65. color: _getColor(toastType, context),
  66. fontWeight: FontWeight.bold,
  67. fontSize: 15,
  68. ),
  69. ),
  70. ),
  71. ],
  72. ),
  73. ),
  74. gravity: gravity,
  75. toastDuration: const Duration(seconds: 2),
  76. );
  77. }
  78. }