使用https://pub.dev/packages/local_auth,目前最新的是0.6.2+3。
根据官方文档:
Note that local_auth plugin requires the use of a FragmentActivity as opposed to Activity. This can be easily done by switching to use FlutterFragmentActivity as opposed to FlutterActivity in your manifest (or your own Activity class if you are extending the base class).
修改MainActiviti,由默认的:
1 2 3 4 5 6 7 8 9 |
package com.mlkui.flutter.example; import io.flutter.embedding.android.FlutterActivity; import android.os.Build; import android.view.ViewTreeObserver; import android.view.WindowManager; public class MainActivity extends FlutterActivity { } |
修改为:
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.mlkui.flutter.example; import io.flutter.embedding.android.FlutterFragmentActivity; import io.flutter.embedding.engine.FlutterEngine; import io.flutter.plugins.GeneratedPluginRegistrant; public class MainActivity extends FlutterFragmentActivity { @Override public void configureFlutterEngine(FlutterEngine flutterEngine) { GeneratedPluginRegistrant.registerWith(flutterEngine); } } |
注意,必须使用上述的代码对插件进行注册,否则会报错:
1 |
No implementation found for method authenticateWithBiometrics on channel plugins.flutter.io/local_auth |
还需要注意继承的FlutterFragmentActivity是io.flutter.embedding.android.FlutterFragmentActivity。
根据文档,在AndroidManifest.xml中增加USE_FINGERPRINT权限:
1 2 3 4 5 6 7 8 |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mlkui.flutter.example"> <!-- Flutter needs it to communicate with the running application to allow setting breakpoints, to provide hot reload, etc. --> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.USE_FINGERPRINT"/> </manifest> |
具体的使用代码参考官网example即可,例如笔者所实现的效果大致如下:
需要注意,可能遇到https://github.com/flutter/flutter/issues/47602中描述的问题:
1 |
You need to use a Theme.AppCompat theme (or descendant) with this activity |
可以通过在style.xml中增加:
1 |
<style name="LaunchTheme" parent="Theme.AppCompat.Light.NoActionBar"> |
解决。
转载时请保留出处,违法转载追究到底:进城务工人员小梅 » Flutter使用local_auth实现生物识别