setIcon method

Future<void> setIcon(
  1. String iconPath, {
  2. bool isTemplate = false,
  3. TrayIconPositon iconPosition = TrayIconPositon.left,
})

Sets the image associated with this tray icon.

iconPath is the path to the image file.

However, if the app is running in a sandbox like Flatpak or Snap, iconPath should be the name of the icon as specified in the app's manifest file, without the path or file extension. For example, if the icon is specified as org.example.app in the Flatpak manifest file, then the icon should be passed as org.example.app.

Implementation

Future<void> setIcon(
  String iconPath, {
  bool isTemplate = false, // macOS only
  TrayIconPositon iconPosition = TrayIconPositon.left, // macOS only
}) async {
  final Map<String, dynamic> arguments = {
    'id': shortid.generate(),
    'iconPath': path.joinAll([
      path.dirname(Platform.resolvedExecutable),
      'data/flutter_assets',
      iconPath,
    ]),
    'isTemplate': isTemplate,
    'iconPosition': iconPosition.name,
  };

  switch (defaultTargetPlatform) {
    case TargetPlatform.linux:
      if (runningInSandbox()) {
        // Pass the icon name as specified if running in a sandbox.
        //
        // This is required because when running in a sandbox, paths are not
        // the same as seen by the app and the host system.
        arguments['iconPath'] = iconPath;
      }
      break;
    case TargetPlatform.macOS:
      // Add the icon as base64 string
      ByteData imageData = await rootBundle.load(iconPath);
      String base64Icon = base64Encode(imageData.buffer.asUint8List());
      arguments['base64Icon'] = base64Icon;
      break;
    default:
      break;
  }

  await _channel.invokeMethod('setIcon', arguments);
}