搜索插件、代码仓与 APP 分类

插件详情v4.3.0

flutter_app_lock

一个用于在应用打开和暂停时显示锁屏的 Flutter 包。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1940
喜欢数
125
Pub 分数
150

安装插件

flutter pub add flutter_app_lock

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Flutter App Lock 是一个 Flutter 包,可在应用启动或暂停时启用锁屏界面。它提供了一种简单的方法,通过生物识别认证(如指纹或面部识别)或自定义 PIN/密码来保护应用访问。该包支持 Android、iOS、Web、macOS、Windows 和 Linux 等多个平台,非常适合需要用户认证的跨平台应用程序。它与 Flutter 的 widget 系统无缝集成,并允许自定义锁屏 UI 和行为。

适合场景

  • 在启动时保护敏感的应用程序数据
  • 应用暂停后需要重新进行身份验证
  • 保护共享设备中的个人信息
  • 在企业应用中强制执行访问控制
  • 为金融或健康应用添加安全层

主要特点

  • 生物识别认证支持
  • 可自定义的锁屏UI
  • 跨平台兼容性
  • 与现有 Flutter 应用轻松集成
  • 可配置的超时和重新锁定行为

适合对象

  • 开发安全的移动或桌面应用程序的开发者
  • 处理用户隐私数据的应用
  • 企业或政府应用程序
  • 需要符合数据保护标准的应用程序

使用建议

  • 需要适当的平台权限以使用生物识别功能
  • 如果过度使用,可能会影响用户体验
  • 不适用于需要连续后台运行的应用程序
  • Web 和桌面平台需要额外设置
  • 依赖底层操作系统生物识别服务以确保可靠性

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

flutter_app_lock

A Flutter package for showing a lock screen on app open and app pause.

If the app is launching, the lock screen is shown first and then the rest of the app is instantiated once a successful unlock has occured.

If the user is returning to the app after it has already launched, the lock screen is shown on top of your app and can't be dismissed until another successful unlock.

Getting Started

In your flutter project add the dependency:

dependencies:
  ...
  flutter_app_lock: ^4.2.0+2

For help getting started with Flutter, view the online documentation.

Usage example

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  ...

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      ...,
      builder: (context, child) => AppLock(
        builder: (context, arg) => child!,
        lockScreenBuilder: (context) => LockScreen(),
      ),
    );
  }
}

Simply use your MaterialApp's builder property to return an AppLock widget, passing child to its own builder property.

Note: While I mention MaterialApp throughout this README, this is also compatible with CupertinoApp and WidgetsApp widgets.

LockScreen is your own widget implementing your own login logic which should call the following once a successful login has occured.

AppLock.of(context)!.didUnlock();

This will now make the MaterialApp's Navigator or Router the top most widget.

Theme

Because AppLock is expected to be returned within your MaterialApp's builder method, your existing theme and theme variants will style the widget returned from lockScreenBuilder automatically.

MaterialApp(
  ...,
  theme: ThemeData(
    ...,
  ),
  builder: (context, child) => AppLock(
    builder: (context, arg) => child!,
    lockScreenBuilder: (context) => LockScreen(),
  ),
);

Enabling and disabling

It is possible to enable and disable the showing of the widget returned from lockScreenBuilder on app launch and on-demand.

MaterialApp(
  ...,
  builder: (context, child) => AppLock(
    builder: (context, arg) => child!,
    lockScreenBuilder: (context) => LockScreen(),
    initiallyEnabled: false,
  ),
);

The above will cause child (your MaterialApp's Navigator or Router) to be built instantly and the widget returned from lockScreenBuilder will never be shown. The default for initiallyEnabled is true.

You can then enable the showing of the widget returned from lockScreenBuilder later on by doing:

AppLock.of(context)!.enable();

This will now cause the widget returned from lockScreenBuilder to be shown on app pauses.

If you wanted to disable the showing of the widget returned from lockScreenBuilder again you can simply do:

AppLock.of(context)!.disable();

There is also a convenience method:

AppLock.of(context)!.setEnabled(true);
AppLock.of(context)!.setEnabled(false);

Passing arguments

In some scenarios, it might be appropriate to unlock a database or create some other objects from the widget returned from lockScreenBuilder and then expose them to your app further down the tree, so you can better guarantee that services are instantiated or databases are opened/unlocked.

You can do this by passing in an argument to the didUnlock method on AppLock:

var database = await openDatabase(...);

AppLock.of(context)!.didUnlock(database);

This object is then available as part of the AppLock builder method, builder:

MaterialApp(
  ...,
  builder: (context, child) => AppLock(
    builder: (context, arg) => child!, // arg is the `database` object passed in to `didUnlock`
    lockScreenBuilder: (context) => LockScreen(),
  ),
);

It is also available by calling AppLock.of(context)!.launchArg.

Manually showing the lock screen

In some scenarios, you might want to manually trigger the lock screen to show.

You can do this by calling:

AppLock.of(context)!.showLockScreen();

If you want to wait until the user has successfully unlocked again, showLockScreen returns a Future so you can await this method call.

await AppLock.of(context)!.showLockScreen();

print('Did unlock!');

Background lock latency

It might be useful for apps to not require the lock screen to be shown immediately after entering the background state. You can now specify how long the app is allowed to be in the background before requiring the lock screen to be shown:

MaterialApp(
  ...,
  builder: (context, child) => AppLock(
    ...,
    initialBackgroundLockLatency: const Duration(seconds: 30),
  ),
);

The above example allows the app to be in the background for up to 30 seconds without requiring the lock screen to be shown.

It's also possible to change this at runtime using the method:

AppLock.of(context)!.setBackgroundLockLatency(const Duration(seconds: 5));

Inactive statuses (e.g. app switcher)

When the app becomes "inactive", for example by launching the device app switcher or device notification center, you can now configure a widget to be shown when this occurs:

MaterialApp(
  ...,
  builder: (context, child) => AppLock(
    ...,
    inactiveBuilder: (context) => InactiveScreen(),
  ),
);

InactiveScreen is your own widget implementing your own requirements for a screen shown while the app is inactive.

There are limitations to this as noted on issue #6 on GitHub.

The widget returned by inactiveBuilder is only shown if AppLock is enabled (default behavior). This can be changed so the widget returned by inactiveBuilder is shown whenever the app is inactive, irrespective of whether AppLock is enabled or not, by setting inactiveBehavior to InactiveBehavior.alwaysShow.

MaterialApp(
  ...,
  builder: (context, child) => AppLock(
    ...,
    inactiveBehavior: InactiveBehavior.showWhenEnabled, // only show when the app is in active and AppLock is enabled (default)
  ),
);
MaterialApp(
  ...,
  builder: (context, child) => AppLock(
    ...,
    inactiveBehavior: InactiveBehavior.alwaysShow, // always show when the app is inactive
  ),
);

Tests

Integration tests have been introduced in the example project and were used to confirm the behavior hasn't changed since the move to null-safety.

They can be run by running flutter test integration_test/integration_tests.dart in a terminal.

所属分类

源码仓库

使用此插件的项目