You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.3 KiB

  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:my_flutter_puzzle/utils/toast.dart';
  4. class Utils {
  5. Utils.privateConstructor();
  6. static final Utils instance = Utils.privateConstructor();
  7. factory Utils() {
  8. return instance;
  9. }
  10. void showToast(BuildContext? context, String? txt, {bool isError = true, ToastGravity gravity = ToastGravity.top}) {
  11. try {
  12. if (context == null) return;
  13. if (txt == null || txt.isEmpty) {
  14. if (isError == false) {
  15. return;
  16. }
  17. txt = 'Error';
  18. }
  19. FToast fToast = FToast();
  20. fToast.init(context);
  21. Widget toast = Container(
  22. padding: const EdgeInsetsDirectional.all(16),
  23. decoration: BoxDecoration(
  24. borderRadius: BorderRadius.circular(8),
  25. color: isError ? Colors.red : Colors.lightBlue,
  26. ),
  27. child: Text(
  28. txt,
  29. style: const TextStyle(
  30. color: Colors.white,
  31. fontWeight: FontWeight.bold,
  32. ),
  33. ),
  34. );
  35. fToast.showToast(child: toast, gravity: gravity, toastDuration: const Duration(milliseconds: 1500));
  36. } catch (e) {
  37. assert(() {
  38. if (kDebugMode) {
  39. print(e);
  40. }
  41. return true;
  42. }());
  43. }
  44. }
  45. }