flutter app accessible when phone is locked

How to Make Flutter App Accessible When Phone is Locked?

If you want your Flutter app to be accessible even when the phone is locked, there are a few things you can do.

1. Use the Flutter Native Plugins

Flutter has a set of native plugins that allow you to access the hardware and software features of the device. One of these plugins is the flutter_lock_screen plugin, which allows you to display a custom lock screen when the user presses the power button or the home button.

To use this plugin, you need to add it to your pubspec.yaml file:


dependencies:
  flutter_lock_screen: ^1.0.0

After that, you can use the flutter_lock_screen plugin in your app:


import 'package:flutter_lock_screen/flutter_lock_screen.dart';

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State {
  @override
  Widget build(BuildContext context) {
    return LockScreen(
      onUnlocked: () {
        // Do something when the user unlocks the screen
      },
      correctString: '1234',
      backgroundColor: Colors.blue,
      title: 'Enter Pin',
      showFingerprint: true,
      fingerprintCallback: () {
        // Do something when the user taps the fingerprint icon
      },
    );
  }
}

This will display a lock screen when the user presses the power button or the home button. The onUnlocked callback will be called when the user unlocks the screen. You can also customize the lock screen by changing the background color, the title, and whether to show the fingerprint icon.

2. Use Background Fetch

Another way to keep your Flutter app accessible even when the phone is locked is to use the background_fetch plugin. This plugin allows you to schedule a background task that will run periodically, even when the app is not running.

To use this plugin, you need to add it to your pubspec.yaml file:


dependencies:
  background_fetch: ^0.6.0

After that, you can use the background_fetch plugin in your app:


import 'dart:async';

import 'package:background_fetch/background_fetch.dart';

void backgroundFetchHeadlessTask() async {
  // Do something in the background
  print('[BackgroundFetch] Headless event received.');
  BackgroundFetch.finish();
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State {
  @override
  void initState() {
    super.initState();

    // Configure the background fetch
    BackgroundFetch.configure(
      BackgroundFetchConfig(
        minimumFetchInterval: 15,
        stopOnTerminate: false,
        enableHeadless: true,
        startOnBoot: true,
      ),
      backgroundFetchHeadlessTask,
    ).then((int status) {
      print('[BackgroundFetch] SUCCESS: $status');
    }).catchError((e) {
      print('[BackgroundFetch] ERROR: $e');
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text('Hello, World!'),
        ),
      ),
    );
  }
}

This will schedule a background task that will run every 15 minutes, even when the app is not running. The backgroundFetchHeadlessTask function will be called when the task runs. You can do whatever you want in this function, such as fetching data from a server or updating the user's location. When the function is done, you need to call BackgroundFetch.finish() to let the system know that the task is finished.

Note that you need to enable the enableHeadless option to run the task in the background. You also need to add the startOnBoot option if you want the task to start automatically when the device boots up.

Conclusion

These are two ways to keep your Flutter app accessible even when the phone is locked. Depending on your app's requirements, you can choose one of these methods or use both of them.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe