Reputation: 31
App with two screens/stateful widgets, and the second screen to be able to show even when the phone is locked, not the whole app, for example Alarm app where the Alarm alert screen can showup even when phone is locked and user willbe able to perform actions on that screen while the phone being locked, or any messenger or calling app with callScreen, user will see that screen and be able to perform accept/reject without unlocking phone.
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MAIN SCREEN HOME',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'FIRST SCREEN'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: TextButton(child: const Text('GO TO SECOND SCREEN SCREEN'),onPressed: (){
Navigator.push(context, MaterialPageRoute(builder:(context) => const SecondScreen()));
}, ),
),
);
}
}
class SecondScreen extends StatefulWidget {
const SecondScreen({Key? key}) : super(key: key);
@override
State<SecondScreen> createState() => _SecondScreenState();
}
class _SecondScreenState extends State<SecondScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('SECOND SCREEN'),
),
body: const Center(
child: Text('THIS IS SECOND SCREEN THAT SHOULD BE ACCESSIBLE EVEN WHEN PHONE IS LOCKED'),
),
);
}
}
MyHomePage is the landing page, when user is on this screen, and phone is locked, then fi we try to unlock it, it requires unlocking phone, which is normal
but if we navigate to SecondScreen by manual click of the button on the first screen, or with any automated triggers in the background, the second screen should be visible even if the phone is locked, similar to how call screens work on normal calling app or whatsapp or any other app that has calling feature, only that call screen is visible,
I tried placing these attributes in androidManifest file of the single activity,
android:showWhenLocked="true" android:turnScreenOn="true"
but the entire app is becoming accessible, required behaviour is that the only SecondScreen to be accessible on locked screens, the rest should require unlocking phone
I also tried creating another element under in AndroidManifest.xml file but not sure if its right in Flutter as i read its single activity flow,
If any of you tried this, or similar, please share your thoughts.
Upvotes: 2
Views: 767
Reputation: 3406
i confirm that Flutter uses a single activity to manage your app so, using android:showWhenLocked="true"
, your entire app will open and be accessible.
You can try to use this package to identify if your screen is locked to deny access to other sections: https://pub.dev/packages/is_lock_screen
Upvotes: 0