import 'package:flutter/material.dart';

class GetQRCodeView extends StatelessWidget {
  final String qrCodeUrl = "https://your-qrcode-url.com/qrcode.png"; // Replace with your QR code URL

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('QR Code Dialog Example'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            showQRCodeDialog(context, qrCodeUrl);
          },
          child: Text('Show QR Code'),
        ),
      ),
    );
  }

  // Function to display the QR code in a dialog
  void showQRCodeDialog(BuildContext context, String qrCodeUrl) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('Scan QR Code'),
          content: Image.network(
            qrCodeUrl,
            loadingBuilder: (BuildContext context, Widget child, ImageChunkEvent? loadingProgress) {
              if (loadingProgress == null) {
                return child; // Display the QR code once loaded
              } else {
                // Show a CircularProgressIndicator while the image is loading
                return Center(
                  child: CircularProgressIndicator(
                    value: loadingProgress.expectedTotalBytes != null
                        ? loadingProgress.cumulativeBytesLoaded / (loadingProgress.expectedTotalBytes ?? 1)
                        : null,
                  ),
                );
              }
            },
            errorBuilder: (context, error, stackTrace) {
              // Display an error widget if the image fails to load
              return Icon(Icons.error, color: Colors.red);
            },
          ),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop(); // Close the dialog
              },
              child: Text('Close'),
            ),
          ],
        );
      },
    );
  }
}

void main() {
  runApp(MaterialApp(home: GetQRCodeView()));
}
