Qr Code Generator in Flutter | How To Generate Qr | Qr Scanner 2024

 So Hlo Guys This is Shivam And in this article we learn how To Generate Qr Code and Scan Qr code in flutter framework .If u liked this article please share this article with your friends .

So lets write code first :

void main(){
  runApp(Myapp());
}
class Myapp extends StatelessWidget {
  const Myapp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Qr code generator',
      debugShowCheckedModeBanner: false,
      home: first(),
    );
  }
}

Explanation:

First we a header function 

And this material run app

And add stetful widgets that are used in the generating first app 

and then retun in Material App becouse we make an app not a single page app

we place a name title 

and then checking our banner 

and a home function that redirect new page 

class first extends StatefulWidget {
  const first({super.key});

  @override
  State<first> createState() => _firstState();
}

class _firstState extends State<first> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Qr Generator',style: TextStyle(color:
Colors.white,fontSize: 30),),
      backgroundColor: Colors.green,),
      endDrawer: AppD(),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton( onPressed: (){
              setState(() {
                Navigator.of(context).push(MaterialPageRoute(builder:
(context)=>ScanQR()));
              });
            }, child: Text('Scan qr code',)),
            SizedBox(height: 20,),
            ElevatedButton(onPressed: (){
              setState(() {
                Navigator.of(context).push(MaterialPageRoute(builder:
(context)=>GenerateQr()));
              });
            }, child: Text('Generate Qr code')),
          ],
        ),
      ),
    );
  }
}



we make a stateful widgets and add first becouse that we make before we add here and the steteful widgets gets managed the widgets

Scaffold : then we add  that are the material who used in adding all the apps widgets in the scaffold.

App bar : then we adding  It is the function that build a simple layout in the upper side of page .in the app bar we add title and we managed the text like text style and their color and widgets and other things

we add some background color in the app bar 

Drawer : It is the menu button or like it place on the right side of the app bar darwer are mainly used for add new items or another page int the apps 

Body: then we add body it uses for different different types then in the body we add centre and 

centre means all we write in the body this comes in the centre .

Column : it uses for if any type function we made in the page then the function placed in the column wise that the use of column

Mainaxixalignment : means it decide where u want to placed the text or image 

Children : it used for adding some sdditional features and that we want 

ElevatedButton : its used for ading button option in the page and it may be different types so we only add set state function that is control the elevated button and other things 

Navigator : it is uesd for adding new page in the previous page this function navigate to the user in a new page ;

Scanqr : we make this page so we navigate first in this page

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_barcode_scanner/flutter_barcode_scanner.dart';  // Import the barcode scanner

class ScanQR extends StatefulWidget {
  const ScanQR({Key? key}) : super(key: key);

  @override
  State<ScanQR> createState() => _ScanQRState();
}

class _ScanQRState extends State<ScanQR> {
  String qrResult = 'Scanned Data will appear here';

  // Function to scan the QR code
  Future<void> scanQR() async {
    try {
      final qrCode = await FlutterBarcodeScanner.scanBarcode(
        '#ff6666', // Custom color for the scanning line
        'Cancel', // Cancel button text
        true,     // Show flash icon
        ScanMode.QR, // Scan mode as QR code
      );

      if (qrCode == '-1') {
        // If the user cancels the scan
        return;
      }

      setState(() {
        qrResult = qrCode;
      });
    } on PlatformException {
      setState(() {
        qrResult = 'Failed to read QR code';
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Scan QR Code'),
      ),
      body: Center(
        child: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const SizedBox(height: 10),
              Text(
                qrResult,
                style: const TextStyle(color: Colors.black, fontSize: 16),
                textAlign: TextAlign.center,
              ),
              const SizedBox(height: 38),
              ElevatedButton(
                onPressed: scanQR,
                child: const Text('Scan QR'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

In this code we see the code of scanqr 

Lets understand the code 

so first we add some dependency in our pubspec.yaml becouse without dependency our code will be not run.

Lets describe about this page

Barcode scanner : we add this dependency on this page

then add steteful widgets 

in the steful widgets we add some logic about the qr scanner 

sizedbox : it is the function who make the difference in the about the two columns

Generateqr 

import 'package:flutter/material.dart';
//import 'package:qr_flutter/qr_flutter.dart';  // Import the QR code generator package

class GenerateQr extends StatefulWidget {
  const GenerateQr({Key? key}) : super(key: key);

  @override
  State<GenerateQr> createState() => _GenerateQrState();
}

class _GenerateQrState extends State<GenerateQr> {
  TextEditingController urlController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Generate QR Code'),
      ),
      body: Center(
        child: SingleChildScrollView(
          padding: const EdgeInsets.all(16.0),  // Added padding for better UI
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // Display the QR code only if there is some text
              if (urlController.text.isNotEmpty)
               
                 
             
              const SizedBox(height: 20),
              TextField(
                controller: urlController,
                decoration: InputDecoration(
                  hintText: 'Enter your data',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(15),
                  ),
                  labelText: 'Enter your data',
                ),
              ),
              const SizedBox(height: 20),
              ElevatedButton(
                onPressed: () {
                  if (urlController.text.isEmpty) {
                    ScaffoldMessenger.of(context).showSnackBar(
                      const SnackBar(
                        content: Text('Please enter some data to generate QR code'),
                      ),
                    );
                    return;
                  }
                  setState(() {
                    // Trigger UI update to display the QR code
                  });
                },
                child: const Text('Generate QR Code'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

This is the code of generatedqr 






Post a Comment

0 Comments