ionic3Flash Light

Add plugin in your node

ionic cordova plugin add cordova-plugin-flashlight

npm install --save @ionic-native/flashlight


First import in :  app.module.ts

import { Flashlight } from '@ionic-native/flashlight';

Then:

  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    Flashlight
  ]
})

Home.html

<ion-content>
    <ion-fab middle center>
        <button ion-fab color="{{isOn ? 'danger' : 'secondary'}}" (click)="toggleFlash()">
    <ion-icon  name="{{isOn ? 'power' : 'power'}}"></ion-icon>
    </button>
    </ion-fab>

</ion-content>


HomePage.ts

import {  Component} from '@angular/core';
import {  NavController } from 'ionic-angular';
import {  Flashlight } from '@ionic-native/flashlight';

@Component( {
  selector: 'page-home',
 templateUrl: 'home.html'
}
)
export class HomePage {
  isOn: boolean=false;
  constructor(public navCtrl: NavController, private flashlight: Flashlight) {
}
  async isAvailable(): Promise < boolean > {
    try {
      return await this.flashlight.available();
    }
    catch (e) {
      console.log(e);
    }
  }
  async toggleFlash(): Promise < void> {
    try {
      let available=await this.isAvailable();
      if (available) {
        await this.flashlight.toggle();
        this.isOn=!this.isOn;
      }
      else {
        console.log("is not available");
      }
    }
    catch (e) {
      console.log(e);
    }
  }
  async turnOnFlash(): Promise < void> {
    await this.flashlight.switchOn();
  }
  async turnOffFlash(): Promise < void> {
    await this.flashlight.switchOff();
  }
  async isOnFlash(): Promise < boolean> {
    return await this.flashlight.isSwitchedOn();
  }
}


and make fast your application to run command.

ionic codova run build android --prod



Comments

Popular posts from this blog

Hide Tabs when open sub pages