start method

Future<void> start()

Start recording the widget

Implementation

Future<void> start() async {
  if (_isRecording) return;
  _isRecording = true;

  try {
    // Get temporary directory and create output path
    final dir = await getTemporaryDirectory();
    _outputPath =
        '${dir.path}/widget_rec_${DateTime.now().millisecondsSinceEpoch}.mp4';

    final renderObject = _boundaryKey.currentContext?.findRenderObject();
    if (renderObject == null) {
      throw Exception('Widget not found. Ensure WidgetRecorder is built.');
    }

    _size = (renderObject as RenderRepaintBoundary).size;

    // Round dimensions down to the nearest multiple of 16 for perfect encoding
    final int validWidth = (_size!.width.toInt() ~/ 16) * 16;
    final int validHeight = (_size!.height.toInt() ~/ 16) * 16;

    await _channel.invokeMethod('startRecording', {
      'width': validWidth,
      'height': validHeight,
      'fps': _fps,
      'outputPath': _outputPath,
    });

    _timer = Timer.periodic(
      Duration(milliseconds: 1000 ~/ _fps),
      (_) => _captureFrame(),
    );
  } catch (e) {
    _handleError(e.toString());
  }
}