run method

  1. @override
Future<void> run()
override

the SimpleFrameApp subclass implements application-specific code

Implementation

@override
Future<void> run() async {
  currentState = ApplicationState.running;
  if (mounted) setState(() {});

  try {
    // Open the file picker
    FilePickerResult? result = await FilePicker.platform.pickFiles(
      type: FileType.custom,
      allowedExtensions: ['png', 'jpg'],
    );

    if (result != null) {
      File file = File(result.files.single.path!);

      // Read the file content
      Uint8List imageBytes = await file.readAsBytes();

      // Update the UI based on the original image
      setState(() {
        _image = Image.memory(imageBytes);
      });

      // yield here a moment in order to show the first image first
      await Future.delayed(const Duration(milliseconds: 10));

      // create the sprite, quantize and dither and scale if required
      var sprite = TxSprite.fromImageBytes(msgCode: 0x20, imageBytes: imageBytes);

      // Update the UI with the modified image
      setState(() {
        _image = Image.memory(img.encodePng(sprite.toImage()));
      });

      // create the image sprite block header and its sprite lines
      // based on the sprite
      TxImageSpriteBlock isb = TxImageSpriteBlock(
        msgCode: 0x20,
        image: sprite,
        spriteLineHeight: 20,
        progressiveRender: true);

      // and send the block header then the sprite lines to Frame
      await frame!.sendMessage(isb);

      for (var sprite in isb.spriteLines) {
        await frame!.sendMessage(sprite);
      }

    }
    else {
      currentState = ApplicationState.ready;
      if (mounted) setState(() {});
    }
  } catch (e) {
    _log.fine('Error executing application logic: $e');
    currentState = ApplicationState.ready;
    if (mounted) setState(() {});
  }
}