Ionic 5 QR Code Generator App

In this step-by-step tutorial, we'll demonstrate how to create an Ionic 5 app that can generate a QR Code on a button click.

πŸ‘

Want to know more?

You might also like to add our predefined QR-Code Generator plug-in to your Ionic 5 app.

Creating App

  1. From the Apps tab, click Create new app.
  2. Select Ionic 5 > Ionic 5 Blank, for the application name, enter QRCodeGenerator, and click Create:
924924

Creating App UI

  1. After the App Builder loads, open Pages > Screen1 in the Project view tree.
  2. Now, select the Page component from the breadcrumbs (you can also use the OUTLINE view to locate the needed component) and set the page Footer property to False as we will not use it in the app (you will also be asked to confirm changing the property):
19121912
  1. Select ToolbarTitle of the screen header and under the Common tab, change its Text property to QR Code Generator:
15971597
  1. From PALETTE, drag & drop the Input component to the page:
13461346
  1. Replace the default Input1 Common > Label > Text property with QR code info:
13521352
  1. Now, drag and drop a Button component from PALETTE to the page.
  2. Change the button Common > Text property to Generate:
13561356
  1. Finally, drag and drop an HTML component from PALETTE to the page and set its Wrapper Style > Height and Wrapper Style > Width properties to 256px under the Common tab. This will be the size of our QR code:
13501350

That’s all for the app UI and you can save your work by clicking the SAVE button in the App Builder Toolbar.

Now we can start creating the app logic.

Defining App Logic

To generate the QR code, we will use the Angular X QR code library available on GitHub.

  1. First of all, we need to install the Angular X QR code library using Npm modules. Go to the Project > App settings > Npm modules tab. Click on the Add button under the Dependency section and insert the following for the module name: angularx-qrcode of 10.0.11 version:
13161316
  1. To import the module, go back to the Screen1 page and open its MODULE panel, then add the following code to the Imports section: import { QRCodeModule } from 'angularx-qrcode'::
19111911
  1. Then scroll down to locate the NgModule Settings section. Add the following code in QRCodeModule for the Imports value:
14961496
  1. Now, let's create a variable that will be used for returning the entered QR code info.
    To do it, go to the Screen1 CODE panel and under the Variables section, create a new variable: value with the String type and empty string Value field:
11361136
  1. We will use a custom Angular component, so we need to add the following code to the HTML component that we created on Screen1. In the page DESIGN panel, select the component and click the Edit button for the HTML property located under the Common tab. In the HTML editor that opens, add the following code and save:
<qrcode [qrdata]="value" [width]="256" [errorCorrectionLevel]="'M'"></qrcode>
13381338

πŸ‘

Note that in this code, we specified the value variable created in the previous step as the data source.

  1. Also, we need to add the value variable for the Common > [(ngModel)] property of the Input component:
13521352

After that, we can add the functionality to show the QR code after the Button click and to hide the QR code after changing the Input value.

  1. First, for the HTML component, set the Common > Visible property = False so that the QR code will not be visible until the Generate button is clicked:
13571357
  1. Now, to show the QR code on the Generate button click, unfold the EVENTS tab from the bottom and for the Button component, create a new Click event.
    This is done by selecting Set property as the Action. Now, define Html1 as a Component Name, leave its Property name as Visible and its Value as True. Save the event:
13501350
  1. Finally, to hide the QR code after the input value changes, let's add one more event. Now, this will be a Change event created for the Input component.
    For the event, from the drop-down, select Set property as the Action and then select Html for Component Name. Also, leave Property name as Visible and set its Value to False. Save the event:
13541354

πŸ‘

You can click Show All to review that both events have been added to the page and the EVENTS tab looks like the following:

15901590
  1. Save the app.

App Testing

You are now ready to test your app: click the TEST button in the App Builder Toolbar to run the app in the preview to launch the app in the browser and check how it works.

First, enter some test text into the input field, then click the GENERATE button and the QR code will be automatically generated.
When you start typing some new text, the QR code will disappear until you click the GENERATE button again:

517517

Testing on Device

When your app version is ready, you might also like to test it on the device.

πŸ‘

Appery.io Tester App

A great option to quickly test the app on the device is to use our Appery.io Tester app.
The app is free and available for both iOS and Android.
You can check out this page to learn more about using the Appery.io Tester app.

464464