track_it_macos 0.0.1
track_it_macos: ^0.0.1 copied to clipboard
Provides macOS support for the track_it plugin.
example/lib/main.dart
import 'dart:developer';
import 'dart:io';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:track_it_macos/track_it_macos.dart';
void main() async{
WidgetsFlutterBinding.ensureInitialized();
final trackItMacosPlugin = TrackItMacos();
bool? permissionGranted = await trackItMacosPlugin.checkAccessibilityPermission();
log(permissionGranted.toString());
if (permissionGranted == true) {
runApp(const MyApp());
} else {
trackItMacosPlugin.getAccessPermissionFromUser();
runApp(const PermissionDeniedApp());
}
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String platformVersion = 'Unknown';
final _trackItMacosPlugin = TrackItMacos();
bool isMouseHovering = false;
int imageListLength = 0;
List<Uint8List> imageList = [];
@override
void initState() {
super.initState();
// initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
// Future<void> initPlatformState() async {
// String platformVersion;
// // Platform messages may fail, so we use a try/catch PlatformException.
// // We also handle the message potentially returning null.
// try {
// platformVersion =
// await _trackItMacosPlugin.getPlatformVersion() ?? 'Unknown platform version';
// } on PlatformException {
// platformVersion = 'Failed to get platform version.';
// }
// // If the widget was removed from the tree while the asynchronous platform
// // message was in flight, we want to discard the reply rather than calling
// // setState to update our non-existent appearance.
// if (!mounted) return;
// setState(() {
// _platformVersion = platformVersion;
// });
// }
Future<void> getScreenShot() async {
Directory directory = await getApplicationDocumentsDirectory();
String directoryPath = directory.path;
Directory(directoryPath).createSync(recursive: true); // Create directory if it doesn't exist
String dateTime = DateTime.now().toString();
String filePath = '$directoryPath/screenshots/$dateTime.png';
log(filePath);
try {
File imgFile = File(filePath);
Uint8List? img = await _trackItMacosPlugin.getScreenCapture();
if (img != null) {
await imgFile.writeAsBytes(img);
log('Image saved successfully.');
} else {
log('Received null image bytes.');
}
} catch (e) {
log('Error saving image: $e');
}
}
startEventMonitoring(){
debugPrint("Start monitoring events");
_trackItMacosPlugin.startEventMonitor();
}
stopEventMonitor() async {
final data = await _trackItMacosPlugin.stopEventMonitor();
debugPrint("Data : $data");
}
// getMouseLeftClickCount() async {
// final val = await _trackItMacosPlugin.getMouseLeftClickCount();
// log(val.toString());
// }
// getMouseRightClickCount()async{
// final val = await _trackItMacosPlugin.getMouseRightClickCount();
// log(val.toString());
// }
getMouseTotalClickCount() async {
final val = await _trackItMacosPlugin.getMouseTotalClickCount();
log(val.toString());
}
// getLocalKeyCount()async{
// final val = await _trackItMacosPlugin.getLocalKeyCount();
// log(val.toString());
// }
updateIsMouseHovering(bool val){
setState(() {
isMouseHovering = val;
});
}
getMousePosition()async{
String prev = "";
while(isMouseHovering){
final val = await _trackItMacosPlugin.getMousePosition();
if(prev != val.toString()){
log(val.toString());
prev = val.toString();
}
await Future.delayed(const Duration(milliseconds: 10));
}
}
getTotalKeyCount()async {
final val = await _trackItMacosPlugin.getTotalKeyCount();
log(val.toString());
}
Future<void> getListOfImages() async {
debugPrint("ImageList tap called");
final data = await _trackItMacosPlugin.getAllPhysicalScreenshots();
debugPrint("Data length: ${data.length}");
if (mounted) {
setState(() {
imageList = data;
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Running on: $platformVersion\n',style:const TextStyle(color: Colors.green,fontFamily: 'Roboto'),),
),
// body: Center(
// child: Column(
// mainAxisAlignment: MainAxisAlignment.spaceAround,
// children: [
// Text("TotalImages: $imageListLength"),
// InkWell(
// onTap: (){
// getScreenShot();
// },
// child:const Text("Take Screen shot")
// ),
// // InkWell(
// // onTap: (){
// // getMouseLeftClickCount();
// // },
// // child:const Text("Left Mouse Click count")
// // ),
// // InkWell(
// // onTap: (){
// // getMouseRightClickCount();
// // },
// // child:const Text("Right Mouse Click count")
// // ),
// // InkWell(
// // onTap: (){
// // getListOfImages();
// // },
// // child:const Text("Get List of Images")
// // ),
// // InkWell(
// // onTap: (){
// // stopEventMonitor();
// // },
// // child:const Text("Stop")
// // ),
// // InkWell(
// // onTap: (){
// // updateIsMouseHovering(false);
// // },
// // child:const Text("Stop Mouse Hovering")
// // ),
// // InkWell(
// // onTap: (){
// // getTotalKeyCount();
// // },
// // child:const Text("Total Key count")
// // ),
// ],
// ),
// ),
body: ListView.builder(
itemCount: imageList.length,
itemBuilder: (context, index) {
return Card(
margin: const EdgeInsets.all(8.0),
child: Image.memory(
imageList[index],
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) => const Icon(Icons.broken_image),
),
);
},
),
floatingActionButton: IconButton(icon: const Icon(Icons.abc),onPressed: ()async{
await getListOfImages();
}),
),
);
}
}
class PermissionDeniedApp extends StatelessWidget {
const PermissionDeniedApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Scaffold(
body: Center(
child: Text(
'Accessibility permission is required to run this app. Please enable it in System Preferences.',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.red, fontSize: 18),
),
),
),
);
}
}