Skip to content

Instantly share code, notes, and snippets.

@akifarhan
Last active March 26, 2024 13:57
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save akifarhan/f70a2c777651f2ea61a15eb92a5939c1 to your computer and use it in GitHub Desktop.
Save akifarhan/f70a2c777651f2ea61a15eb92a5939c1 to your computer and use it in GitHub Desktop.
Flutter Local_Auth Setting for Android in Kotlin

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).

1. Add USE_FINGERPRINT permission

  • Open android>app>src>main>AndroidManifest.xml
  • Add the permission
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.[your.package]">
  <uses-permission android:name="android.permission.USE_FINGERPRINT"/>
  <application...
    ...
    ...
    ...
<manifest>

2. Change FlutterActivity to FlutterFragmentActivity

If you receive this error: Exception has occurred. PlatformException (PlatformException(no_fragment_activity, local_auth plugin requires activity to be a FragmentActivity., null))

Then you need to do this step. Change FlutterActivity to FlutterFragmentActivity in MainActivity.kt.

package com.[your.package]

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterFragmentActivity() {
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }
}

3. Theme.AppCompat issue

If you receive this error: Exception has occurred. PlatformException (PlatformException(error, You need to use a Theme.AppCompat theme (or descendant) with this activity., null)) Then you need to do this:

  1. Go to android>app>src>main>res>values>style.xml
  2. Change the <style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar"> to <style name="LaunchTheme" parent="Theme.AppCompat.Light.NoActionBar">

ps: The local auth would not working if you update the Android Gradle Plugin.

@oliverbytes
Copy link

Thank you very much! worked 100%

@oliverbytes
Copy link

Do you know how to make the bottom area/status bar change to Dark/Light dynamically when changing Themes?

@akifarhan
Copy link
Author

Thank you very much! worked 100%

Glad that it helps!

Do you know how to make the bottom area/status bar change to Dark/Light dynamically when changing Themes?

I've never customised bottom area before, only status bar. I'm not so sure about doing it dynamically but this is what i did to change the status bar brightness that works for both iOS and Android.

In main.dart, I added this in main():

void main() {
  ....
  ....
  ....
  SystemChrome.setSystemUIOverlayStyle(
    SystemUiOverlayStyle(
      statusBarBrightness: Brightness.dark,
      statusBarIconBrightness: Brightness.light,
    ),
  );
}

To change the status bar in specific screen that have AppBar, I use this brightness: to change the status bar brightness. To make it dynamic depends on backgroundColor, I use computeLuminance.

Color backgroundColor = Colors.white; // example
return Scaffold(
      backgroundColor: backgroundColor,
      appBar: AppBar(
        brightness: backgroundColor.computeLuminance() < 0.5
            ? Brightness.dark
            : Brightness.light,
        elevation: 0,
        backgroundColor: backgroundColor,
       ....
       ....
       ....

For screen without AppBar, I use AnnotatedRegion:

return AnnotatedRegion<SystemUiOverlayStyle>(
        value: SystemUiOverlayStyle(
          statusBarBrightness: Brightness.dark,
          statusBarColor: Colors.white,
        ),
        child: Scaffold(

This is my work around for the time being. I hope this helps. Let me know if you have cooler ways to do this.

@marvin46
Copy link

thankyou its work properly

@reinaldobrazpereira
Copy link

100%

@reinaldobrazpereira
Copy link

100% Muito obrigado.

@varshaForTech
Copy link

Better to remove this line of code too in AndroidManifest.xml

<meta-data android:name="io.flutter.app.android.SplashScreenUntilFirstFrame" android:value="true"/>

@BangPei
Copy link

BangPei commented Oct 1, 2020

thank you so much, work 100%

@jovane21
Copy link

I change the mainActivity.kt file and I am still getting the platform exception what should I do?

@akifarhan
Copy link
Author

I change the mainActivity.kt file and I am still getting the platform exception what should I do?

Have you done the third step?

@FiresBurger
Copy link

Hi I am using local_auth: ^0.6.3+2 and android is giving me an error PlatformException(auth_in_progress, Authentication in progress, null, null). I've implemented all the steps mentioned. No errors on ios

@okelloEnos
Copy link

@jovane21 and @FiresBurger you should try and run flutter clean on the terminal

@PRASADHAJARgw
Copy link

not worked for me

@PRASADHAJARgw
Copy link

(no_fragment_activity, local_auth plugin requires activity to be a FragmentActivity., null, null) I am using local_auth: ^0.6.3+4

@Numanshakir
Copy link

thanks very much

@philip6560
Copy link

Works 100%

@tfbsza
Copy link

tfbsza commented Aug 31, 2021

changing the theme to <style name="LaunchTheme" parent="Theme.AppCompat.Light.NoActionBar"> worked for me, thank you!

@umaqs
Copy link

umaqs commented Sep 24, 2021

After changing the theme to Theme.AppCompat.Light.NoActionBar the app fails to build with the error:

* What went wrong:
Execution failed for task ':app:processDebugResources'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
   > Android resource linking failed
     AAPT: error: resource android:style/Theme.AppCompat.Light.NoActionBar not found.
     error: resource android:style/Theme.AppCompat.Light.NoActionBar not found.
     error: failed linking references.

Does anyone know how to resolve this?

@4inka
Copy link

4inka commented Dec 1, 2021

I had <style name="LaunchTheme" parent="@android:style/Theme.Light.NoTitleBar"> instead of <style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">, but changing it to <style name="LaunchTheme" parent="Theme.AppCompat.Light.NoActionBar"> did the trick for me also.

Thank you very much!

@adminant
Copy link

adminant commented Jan 6, 2022

After changing in styles.xml to
<style name="LaunchTheme" parent="Theme.AppCompat.Light.NoTitleBar">
I get error compiling project:

> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
   > Android resource linking failed
     AAPT: error: resource style/Theme.AppCompat.Light.NoTitleBar not found.
     error: resource android:style/Theme.AppCompat.Black.NoTitleBar not found.
     error: failed linking references.

Please help to resolve this.

@akifarhan
Copy link
Author

@adminant Can you share your distributionUrl from the gradle-wrapper.properties file?

@adminant
Copy link

adminant commented Jan 10, 2022

@akifarhan After reading the manual 3 times I've eventually managed to resolve this issue. I used wrong style parent:
<style name="LaunchTheme" parent="Theme.AppCompat.Light.NoTitleBar">
while it should be
<style name="LaunchTheme" parent="Theme.AppCompat.Light.NoActionBar">
Then the project was compiled ok, but the app still keeps crashing on fingerpring auth (Android 7.0 Samsung S7 and Motorola Moto G5s Plus Android 8.1).
Any ideas?

@akifarhan
Copy link
Author

@adminant I've faced this kind of issue before and found that the build Gradle version might be the culprit.

flutter/flutter#64996 (comment)

@manudevcode
Copy link

In my case throws 'Cannot resolve symbol 'Theme.AppCompat.Light.NoActionBar''

@vasilich6107
Copy link

API 27 app crashes on trying to call biometrics with error

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

@karun7607
Copy link

thank you so much its working for me 100%

@MoeAlshidi
Copy link

WORKED 100%

@msisinni
Copy link

msisinni commented Aug 3, 2023

On the off chance that someone is still having issues after switching to a Theme.AppCompat, try also adding android:theme="@style/LaunchTheme" to your <application ... >

    <application
        android:name="${applicationName}"
        android:theme="@style/LaunchTheme"
        ...
       >

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment