immich_toast.dart 2.3 KB

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