i have one assets image and on this image one container i have to crop image when click save button and show crop image to next screen. i try this code but when click save button the show error. how can solve this without any package in flutter?

import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  GlobalKey globalKey = GlobalKey();

  Offset framePosition = Offset(100, 100); // Initial position of the frame container
  double frameSize = 150; // Initial size of the frame container
  String backgroundImage = 'images/rose1.png'; // Background image path

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Home Screen"),
      ),
      body: Stack(
        children: [
          // Background image
          Image.asset(
            backgroundImage,
            fit: BoxFit.cover,
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,
          ),
          // Frame container
          Positioned(
            left: framePosition.dx,
            top: framePosition.dy,
            child: GestureDetector(
              onPanUpdate: (details) {
                setState(() {
                  framePosition += details.delta;
                });
              },
              child: RepaintBoundary(
                key: globalKey,
                child: Container(
                  width: frameSize,
                  height: frameSize,
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.red, width: 2.0),
                  ),
                ),
              ),
            ),
          ),
          // Save button
          Positioned(
            bottom: 20,
            right: 20,
            child: ElevatedButton(
              onPressed: () => _cropAndNavigate(context),
              child: Text("Save and Next"),
            ),
          ),
        ],
      ),
    );
  }

  Future<void> _cropAndNavigate(BuildContext context) async {
    // Capture the image within the frame container
    ui.Image image = await _captureImage(globalKey.currentContext!);

    // Get frame container position and size relative to the background image
    RenderBox containerBox = globalKey.currentContext!.findRenderObject() as RenderBox;
    Offset containerPosition = containerBox.localToGlobal(Offset.zero);
    double containerWidth = containerBox.size.width;
    double containerHeight = containerBox.size.height;

    // Calculate the crop coordinates relative to the background image
    double left = (framePosition.dx - containerPosition.dx) * 2;
    double top = (framePosition.dy - containerPosition.dy) * 2;
    double width = frameSize * 2;
    double height = frameSize * 2;

    // Crop the image
    Uint8List croppedImage = await _cropImage(image, left.toInt(), top.toInt(), width.toInt(), height.toInt());

    // Navigate to the next screen and pass the cropped image
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => NextScreen(croppedImage: croppedImage),
      ),
    );
  }

  Future<ui.Image> _captureImage(BuildContext context) async {
    RenderRepaintBoundary boundary = context.findRenderObject() as RenderRepaintBoundary;
    ui.Image image = await boundary.toImage(pixelRatio: 2.0);
    return image;
  }

  Future<Uint8List> _cropImage(ui.Image image, int left, int top, int width, int height) async {
    final ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    final Uint8List pngBytes = byteData!.buffer.asUint8List();

    // Crop the image
    // Here, you should implement your cropping logic based on the given coordinates
    // For simplicity, I'll just create a copy of the original image
    Uint8List croppedImage = Uint8List.fromList(pngBytes.sublist(left, left + width));

    return croppedImage;
  }
}

class NextScreen extends StatelessWidget {
  final Uint8List croppedImage;

  NextScreen({required this.croppedImage});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Next Screen"),
      ),
      body: Center(
        child: Image.memory(
          croppedImage,
          fit: BoxFit.cover,
        ),
      ),
    );
  }
}

i have one assets image and on this image one container i have to crop image when click save button and show crop image to next screen. i try this code but when click save button the show error. how can solve this without any package in flutter?