Last updated Mar 26, 2021 In last article Read Inbox Messages we learned how to read all Messages from inbox in Flutter. This part II we are going to learn how you can retrieve Messages from inbox for a particular number in Flutter. Search Inbox messages based on Number,Message...
For this we are going to use SMS plugin.
Let's get started
Read Phone Contacts in Flutter Application
Step 1: Open the last flutter application or Create flutter application
Step 2: Add dependencies or update to the latest version add SMS dependencies in pubspec.yaml file and run flutter pub get on terminal
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.0
flutter_sms_inbox: ^0.1.1+1
|
Step 3: Create dart file and add below code
Referring to the previous source code of the article replace the messages = await query.getAllSms; by
fetchSMS()
async {
messages = await query.querySms(address:"+256256899001");
}
}
|
Add import 'package: sms/sms.dart';
To handle the SMS we need to create instance of SmsQuery
Step 4: Add below code for the search option in the list of messages
class SearchBarDelegate extends SearchDelegate {
List<SmsMessage> recentSuggest;
List<SmsMessage> countryList;
SmsMessage selection = null;
SearchBarDelegate(List<SmsMessage> _countryList) {
countryList = _countryList;
recentSuggest = List();
}
@override
Widget buildLeading(BuildContext context) {
// TODO: implement buildLeading
return IconButton(icon: AnimatedIcon(
icon: AnimatedIcons.menu_arrow, progress: transitionAnimation),
onPressed: () {
close(context, null);
});
}
@override
Widget buildResults(BuildContext context) {
// TODO: implement buildResults
return Padding(
padding: const EdgeInsets.all(8.0),
child: (selection == null) ? Center() : ListTile(
leading: Icon(Icons.markunread, color: Colors.pink,),
title: Text(selection.address),
subtitle: Text(selection.body, maxLines: 2, style: TextStyle(),),
),
);
}
@override
Widget buildSuggestions(BuildContext context) {
List<SmsMessage> suggestionList = query.isEmpty
? recentSuggest
: countryList.where((input) =>
input.sender.toUpperCase().startsWith(query.toUpperCase())).toList();
return ListView.builder(
itemCount: suggestionList.length,
itemBuilder: (context, index) =>
ListTile(
onTap: () {
selection = suggestionList[index];
query = suggestionList[index].sender;
showResults(context);
recentSuggest.insert(0, suggestionList[index]);
},
title: Padding(
padding: const EdgeInsets.all(8.0),
child: ListTile(
leading: Icon(Icons.markunread, color: Colors.pink,),
title: RichText(
text: TextSpan(
text: suggestionList[index].sender.substring(
0, query.length),
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold),
children: [
TextSpan(
text: suggestionList[index].sender.substring(
query.length),
style: TextStyle(color: Colors.grey))
])),
subtitle: RichText(
text: TextSpan(
text: suggestionList[index].body,
style: TextStyle(
color: Colors.black, fontWeight: FontWeight.bold),
)),
),
)
));
}
@override
List<Widget> buildActions(BuildContext context) {
// TODO: implement buildActions
return [IconButton(icon: Icon(Icons.clear), onPressed: () => query = "")];
}
|
Step 5: Run application
