screen_aware 1.0.0
screen_aware: ^1.0.0 copied to clipboard
A plugin that uses the device front-facing camera (with user permission) to perform a simple, privacy-conscious check for user presence. It dont do facial recognition, just detect if a face is present [...]
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'dart:async';
import 'package:screen_aware/screen_aware.dart';
import 'package:screen_aware_example/Utils.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
class _MyAppState extends State<MyApp> {
final _screenAwarePlugin = ScreenAware();
bool _canRecord = false;
bool _isRunning = false;
bool _isPresent = false;
StreamSubscription? _presenceSubscription;
@override
void initState() {
super.initState();
_init();
}
@override
Widget build(BuildContext context) {
final size = MediaQuery.sizeOf(context);
return MaterialApp(
navigatorKey: navigatorKey,
home: Scaffold(
appBar: AppBar(title: const Text('ScreenAware')),
body: Center(
child: Column(
children: [
TextButton.icon(
icon: AnimatedContainer(
width: 12,
height: 12,
duration: Duration(milliseconds: 400),
decoration: BoxDecoration(
color: canRecord ? Colors.green : Colors.red,
shape: BoxShape.circle,
),
),
onPressed: () {
if (canRecord) {
return;
}
_requestPermission();
},
label: Text(
canRecord ? "Ready to Start" : "Request Permission",
style: Theme.of(context).textTheme.bodySmall,
),
),
AnimatedContainer(
width: size.width*0.8,
height: size.height*0.5,
duration: Duration(milliseconds: 400),
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(color: isPresent?Colors.green:Colors.red,width: 2),
),
child: isPresent?Center(
child: Icon(Icons.co_present,size: 100,),
):SizedBox.shrink(),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: isRunning ? Colors.red : null,
),
onPressed: () {
if(isRunning){
_dispose();
}else{
_initialise();
}
},
child: Text(
isRunning ? "Stop" : "Start",
style: Theme.of(context).textTheme.headlineMedium!.copyWith(
color: _isRunning ? Colors.white : null,
),
),
),
],
),
),
),
);
}
void _init() {
_checkPermission();
_checkRun();
}
Future<void> _checkRun() async {
try {
await Future.delayed(Duration(milliseconds: 400));
isRunning = await _screenAwarePlugin.isRunning();
} catch (error) {
Utils().handleError(error);
}
}
Future<void> _checkPermission() async {
try {
canRecord = await _screenAwarePlugin.checkPermission();
} catch (error) {
Utils().handleError(error);
}
}
Future<void> _requestPermission() async {
try {
if(Platform.isAndroid){
await Permission.camera.request();
}else{
await _screenAwarePlugin.requestPermission();
}
_checkPermission();
} catch (error) {
Utils().handleError(error);
}
}
Future<void> _initialise() async {
try {
await _screenAwarePlugin.initialize();
_checkRun();
_setupStreamListener();
} catch (error) {
Utils().handleError(error);
}
}
Future<void> _dispose() async {
try {
await _screenAwarePlugin.dispose();
_checkRun();
if(_presenceSubscription!=null){
_presenceSubscription!.cancel();
_presenceSubscription=null;
}
isPresent=false;
_init();
} catch (error) {
Utils().handleError(error);
}
}
bool get canRecord => _canRecord;
set canRecord(bool value) {
_canRecord = value;
setState(() {});
}
bool get isRunning => _isRunning;
set isRunning(bool value) {
_isRunning = value;
setState(() {});
}
bool get isPresent => _isPresent;
set isPresent(bool value) {
_isPresent = value;
setState(() {
});
}
void _setupStreamListener() {
_presenceSubscription?.cancel();
_presenceSubscription = _screenAwarePlugin.onUserAttentionChanged.listen(
(presenceEvent) {
isPresent = presenceEvent;
},
);
}
}