showToast method

Future<void> showToast({
  1. required String message,
  2. Color? textColor,
  3. double? fontSize,
  4. String? fontFamily,
  5. String? imageResourceName,
  6. Color? backgroundColor,
  7. String? imagePath,
  8. bool showImage = true,
  9. int maxLines = 2,
  10. ToastGravity gravity = ToastGravity.bottom,
  11. double? duration,
})

Implementation

Future<void> showToast({
  required String message,
  Color? textColor,
  double? fontSize,
  String? fontFamily,
  String? imageResourceName,
  Color? backgroundColor,
  String? imagePath,
  bool showImage = true,
  int maxLines = 2,
  ToastGravity gravity = ToastGravity.bottom,
  double? duration,
}) async {
  /// If an image is provided, load it from assets and convert to base64.
  if (imagePath != null && imagePath.isNotEmpty) {
    try{
      final ByteData data = await rootBundle.load(imagePath);
      final Uint8List bytes = data.buffer.asUint8List();
      imagePath = base64Encode(bytes);
    }catch(e){
      log("Error to load image--->$imagePath");
    }
  }
  if (kDebugMode) {
    print(" backgroundColor?.value ${backgroundColor?.value}");
  }
  final data = {
    'message': message,
    'textColor': textColor?.value,
    'fontSize': fontSize,
    'fontFamily': fontFamily,
    'backgroundColor': backgroundColor?.value??Colors.white.value,
    'base64Image': imagePath,
    'showImage': showImage,
    'maxLines': maxLines,
    'gravity': gravity.index,
    /// On Android, if the duration is 0.0, the message will be displayed for a short duration otherwise it show message for long time.
    'duration': duration?.toInt() ?? (Platform.isAndroid ? 1 : 2),
    'imageResourceName': imageResourceName,
  };

  try {
    await methodChannel.invokeMethod<void>('showCustomToast', data);
  } on PlatformException catch (e) {
    if (kDebugMode) {
      print("Failed to show toast: '${e.message}'.");
    }
  }
}