flutter_sms_inbox 1.0.3 flutter_sms_inbox: ^1.0.3 copied to clipboard
Flutter SMS Inbox Plugin (Android only). This library allows users to easily query inbox messages.
import 'package:flutter/material.dart';
import 'package:flutter_sms_inbox/flutter_sms_inbox.dart';
import 'package:permission_handler/permission_handler.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 SmsQuery _query = SmsQuery();
List<SmsMessage> _messages = [];
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter SMS Inbox App',
theme: ThemeData(
primarySwatch: Colors.teal,
),
home: Scaffold(
appBar: AppBar(
title: const Text('SMS Inbox Example'),
),
body: Container(
padding: const EdgeInsets.all(10.0),
child: _messages.isNotEmpty
? _MessagesListView(
messages: _messages,
)
: Center(
child: Text(
'No messages to show.\n Tap refresh button...',
style: Theme.of(context).textTheme.headlineSmall,
textAlign: TextAlign.center,
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
var permission = await Permission.sms.status;
if (permission.isGranted) {
final messages = await _query.querySms(
kinds: [
SmsQueryKind.inbox,
SmsQueryKind.sent,
],
// address: '+254712345789',
count: 10,
);
debugPrint('sms inbox messages: ${messages.length}');
setState(() => _messages = messages);
} else {
await Permission.sms.request();
}
},
child: const Icon(Icons.refresh),
),
),
);
}
}
class _MessagesListView extends StatelessWidget {
const _MessagesListView({
Key? key,
required this.messages,
}) : super(key: key);
final List<SmsMessage> messages;
@override
Widget build(BuildContext context) {
return ListView.builder(
shrinkWrap: true,
itemCount: messages.length,
itemBuilder: (BuildContext context, int i) {
var message = messages[i];
return ListTile(
title: Text('${message.sender} [${message.date}]'),
subtitle: Text('${message.body}'),
);
},
);
}
}