readsms 0.2.0+4 readsms: ^0.2.0+4 copied to clipboard
A flutter plugin that reads incoming SMS on device as a broadcast stream.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:permission_handler/permission_handler.dart';
import 'package:readsms/readsms.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _plugin = Readsms();
String sms = 'no sms received';
String sender = 'no sms received';
String time = 'no sms received';
@override
void initState() {
super.initState();
getPermission().then((value) {
if (value) {
_plugin.read();
_plugin.smsStream.listen((event) {
setState(() {
sms = event.body;
sender = event.sender;
time = event.timeReceived.toString();
});
});
}
});
}
Future<bool> getPermission() async {
if (await Permission.sms.status == PermissionStatus.granted) {
return true;
} else {
if (await Permission.sms.request() == PermissionStatus.granted) {
return true;
} else {
return false;
}
}
}
@override
void dispose() {
super.dispose();
_plugin.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('new sms received: $sms'),
Text('new sms Sender: $sender'),
Text('new sms time: $time'),
],
),
),
),
);
}
}