Advertisement
  1. Code
  2. Mobile Development
  3. Ionic Development

Creating a Task Manager App Using Ionic: Part 1

Scroll to top

In this tutorial series, you'll learn how to get started with creating a mobile app using the Ionic framework. Ionic uses Angular for writing the components and creating the mobile app. 

Throughout the course of this series, you'll be creating a simple task manager mobile app using the Ionic framework.

From the official documentation:

Ionic is the beautiful, free and open source mobile SDK for developing native and progressive web apps with ease. 

Getting Started

Let's get started by installing Ionic using Node Package Manager (npm). Before installing Ionic, make sure you have Node.js installed on your system.

1
npm install -g cordova ionic

Once Ionic has been installed, create a new Ionic blank project using the following command:

1
ionic start angular-ionic blank

You'll be testing the Ionic app in the browser until it's complete. Once complete, you'll test the application on an Android device.

Type in the following command to run the app from the browser.

1
ionic serve

Point your browser to http://localhost:8100/ and you should have the Ionic project running.

IONIC Blank Template ProjectIONIC Blank Template ProjectIONIC Blank Template Project

Here is how the project structure looks:

IONIC Project StructureIONIC Project StructureIONIC Project Structure

Inside the Ionic project, you have a folder called src/app. Inside the app folder, you will find a file called app.module.ts where the root module AppModule is defined.

In the app.module.ts file inside the imports section, you can see that the root component has been defined using the following code:

1
IonicModule.forRoot(MyApp)

MyApp has been imported from the app.component where it has set the rootPage as HomePage, which shows up when the app loads.

Creating a List Task Component

Let's create the view for listing the added tasks. Inside the src/pages folder, create another folder for the list component called list.

Inside the list folder, create a file called list.component.html which would be the component template. Add the following code to the list.component.html file:

1
<ion-header>
2
    <ion-navbar>
3
        <ion-title>Ionic Task List</ion-title>
4
    </ion-navbar>
5
</ion-header>
6
7
<ion-content>
8
    <ion-list>
9
        <ion-item>
10
            <ion-label>Task 1 </ion-label>
11
            <ion-checkbox></ion-checkbox>
12
        </ion-item>
13
        <ion-item>
14
            <ion-label>Task 2</ion-label>
15
            <ion-checkbox></ion-checkbox>
16
        </ion-item>
17
        <ion-item>
18
            <ion-label>Task 3 </ion-label>
19
            <ion-checkbox></ion-checkbox>
20
        </ion-item>
21
    </ion-list>
22
</ion-content>

As seen in the code above, you have used Ionic-specific tags for creating the elements. You can refer to the Ionic official documentation to get a list of Ionic component APIs which you can use while creating your templates.

To control the above created template, you'll need a controller class. Create a file called list.component.ts and define the ListPage class. Here is how it looks:

1
import { Component } from '@angular/core';
2
3
@Component({
4
  selector: 'page-list',
5
  templateUrl: 'list.component.html'
6
})
7
export class ListPage {
8
9
  constructor() {
10
11
  }
12
13
}

You have defined the task listing component using the @Component decorator. Inside the @Component decorator, you have defined the selector using the selector key and the template using the templateUrl key for the component.

The application needs to know that the above created component exists, and hence you need to import it inside the root module.

Import the ListPage class inside the src/app/app.module.ts file and include it in the declarations and entryComponents list. Here is how the modified app.module.ts file looks:

1
import { BrowserModule } from '@angular/platform-browser';
2
import { ErrorHandler, NgModule } from '@angular/core';
3
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
4
import { SplashScreen } from '@ionic-native/splash-screen';
5
import { StatusBar } from '@ionic-native/status-bar';
6
import { ListPage } from '../pages/list/list.component';
7
8
import { MyApp } from './app.component';
9
import { HomePage } from '../pages/home/home';
10
11
@NgModule({
12
  declarations: [
13
    MyApp,
14
    HomePage,
15
    ListPage
16
  ],
17
  imports: [
18
    BrowserModule,
19
    IonicModule.forRoot(MyApp)
20
  ],
21
  bootstrap: [IonicApp],
22
  entryComponents: [
23
    MyApp,
24
    HomePage,
25
    ListPage
26
  ],
27
  providers: [
28
    StatusBar,
29
    SplashScreen,
30
    {provide: ErrorHandler, useClass: IonicErrorHandler}
31
  ]
32
})
33
export class AppModule {}

When the application loads up, you want the newly created List component to show up instead of the HomePage. So modify the rootPage defined inside the app.component.ts file. 

1
import { Component } from '@angular/core';
2
import { Platform } from 'ionic-angular';
3
import { StatusBar } from '@ionic-native/status-bar';
4
import { SplashScreen } from '@ionic-native/splash-screen';
5
6
import { HomePage } from '../pages/home/home';
7
import { ListPage } from '../pages/list/list.component';
8
@Component({
9
  templateUrl: 'app.html'
10
})
11
export class MyApp {
12
  rootPage:any = ListPage;
13
14
  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
15
    platform.ready().then(() => {
16
      statusBar.styleDefault();
17
      splashScreen.hide();
18
    });
19
  }
20
}
21

As seen in the above code, you imported the ListPage inside the app.component.ts file and changed the rootPage to ListPage instead of HomePage.

Save the above changes and start the Ionic server using the following command:

1
ionic serve

You should have the mobile app running at http://localhost:8100/

Ionic App With List ComponentIonic App With List ComponentIonic App With List Component

Creating the Add Task Component

From the listing screen, you'll add a link to add items to the list. Let's place an add icon on the right side of the screen. Inside the list.component.html file, add the following to add the plus icon.

1
<ion-fab top right>
2
    <button ion-fab mini><ion-icon name="add"></ion-icon></button>
3
</ion-fab>

Here is how the modified list.component.html file looks:

1
<ion-header>
2
    <ion-navbar>
3
        <ion-title text-center>Ionic Task Manager</ion-title>
4
    </ion-navbar>
5
6
</ion-header>
7
8
<ion-content padding>
9
    <ion-fab top right>
10
        <button ion-fab mini><ion-icon name="add"></ion-icon></button>
11
    </ion-fab>
12
13
    <ion-list>
14
15
        <ion-item>
16
            <ion-label>Task 1 </ion-label>
17
            <ion-checkbox></ion-checkbox>
18
        </ion-item>
19
20
        <ion-item>
21
            <ion-label>Task 2</ion-label>
22
            <ion-checkbox></ion-checkbox>
23
        </ion-item>
24
25
        <ion-item>
26
            <ion-label>Task 3 </ion-label>
27
            <ion-checkbox></ion-checkbox>
28
        </ion-item>
29
30
    </ion-list>
31
32
</ion-content>

Save the above changes and restart the server, and you will have the plus icon displayed on the screen.

Task Manager List With Add IconTask Manager List With Add IconTask Manager List With Add Icon

When the user clicks on the plus icon, you need to show the screen to add new tasks. So let's create another component called the Add component.

Create a folder called add inside the pages folder. Inside the add folder, create a file called add.component.html and add the following code:

1
<ion-header>
2
    <ion-navbar>
3
        <ion-title text-center>Ionic Task Manager</ion-title>
4
    </ion-navbar>
5
6
</ion-header>
7
8
<ion-content padding>
9
    <ion-fab top right>
10
        <button ion-fab mini> <ion-icon name="arrow-back"></ion-icon></button>
11
    </ion-fab>
12
13
    <ion-list>
14
        <ion-item>
15
            <ion-label color="primary" floating>Enter task </ion-label>
16
            <ion-input></ion-input>
17
        </ion-item>
18
    </ion-list>
19
20
    <button ion-button round>Add</button>
21
22
</ion-content>

The above code displays the screen to add a new task.

You need to display the above screen when the user clicks the plus icon on the list screen. So you'll be showing it as a modal popup.

Create a file called add.component.ts and add the following code:

1
import { Component } from '@angular/core';
2
3
@Component({
4
  selector: 'page-add',
5
  templateUrl: 'add.component.html'
6
})
7
export class AddPage {
8
9
  constructor() {
10
11
  }
12
13
}

As seen in the above code, you just created the Add component by specifying the selector and templateUrl.

Register the component by importing it in the app.module.ts file inside the src/app folder. Here is how the modified app.module.ts file looks:

1
import { BrowserModule } from '@angular/platform-browser';
2
import { ErrorHandler, NgModule } from '@angular/core';
3
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
4
import { SplashScreen } from '@ionic-native/splash-screen';
5
import { StatusBar } from '@ionic-native/status-bar';
6
import { ListPage } from '../pages/list/list.component';
7
import { AddPage } from '../pages/add/add.component';
8
9
import { MyApp } from './app.component';
10
import { HomePage } from '../pages/home/home';
11
12
@NgModule({
13
  declarations: [
14
    MyApp,
15
    HomePage,
16
    ListPage,
17
    AddPage
18
  ],
19
  imports: [
20
    BrowserModule,
21
    IonicModule.forRoot(MyApp)
22
  ],
23
  bootstrap: [IonicApp],
24
  entryComponents: [
25
    MyApp,
26
    HomePage,
27
    ListPage,
28
    AddPage
29
  ],
30
  providers: [
31
    StatusBar,
32
    SplashScreen,
33
    {provide: ErrorHandler, useClass: IonicErrorHandler}
34
  ]
35
})
36
export class AppModule {}

To show the AddPage component in a modal popup, you need to import ModalController provided by ionic-angular. Import ModalController in list.component.ts.

1
import { ModalController } from 'ionic-angular';

Create a method called presentAddModal and create a modal using the imported ModalController. Here is how the list.component.ts file looks:

1
import { Component } from '@angular/core';
2
import { ModalController } from 'ionic-angular';
3
import { AddPage } from '../add/add.component';
4
5
@Component({
6
  selector: 'page-list',
7
  templateUrl: 'list.component.html'
8
})
9
export class ListPage {
10
11
  constructor(public modalCtrl: ModalController) {}
12
13
  presentAddModal() {
14
    let addModal = this.modalCtrl.create(AddPage);
15
    addModal.present();
16
  }
17
18
}

Register the click event on the add button in list.component.html

1
<button ion-fab mini (click)="presentAddModal()"><ion-icon name="add"></ion-icon></button>

Save the above changes and restart the Ionic server. Click on the add icon in the list page and you will have the add task page.

Task Manager App - Add TaskTask Manager App - Add TaskTask Manager App - Add Task

For the back button on the add task screen, you need to add a dismiss method on the button click in the add.component.ts file.

1
 <button ion-fab mini (click)="dismiss()"> <ion-icon name="arrow-back"></ion-icon></button>

Import the ViewController module and call the dismiss method to go back to the listing screen. Here is how it looks:

1
import { Component } from '@angular/core';
2
import { ViewController } from 'ionic-angular';
3
4
@Component({
5
  selector: 'page-add',
6
  templateUrl: 'add.component.html'
7
})
8
export class AddPage {
9
10
  constructor(public viewCtrl: ViewController) {
11
12
  }
13
14
  dismiss(){
15
      this.viewCtrl.dismiss();
16
  }
17
18
}

Wrapping It Up

In this tutorial, you saw how to get started with creating a mobile app using the Ionic framework, which uses Angular. You created the view for a task manager app. You created the Add component and List component. 

In the next part of this tutorial series, you'll implement the functionality for the task manager app to add and list tasks.

Source code from this tutorial is available on GitHub.

Do let us know your suggestions in the comments below.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.