11// ignore_for_file: use_build_context_synchronously
22
3-
43import 'package:flutter/material.dart' ;
54import 'package:open_mail/open_mail.dart' ;
65
@@ -11,6 +10,18 @@ void main() {
1110class MyApp extends StatelessWidget {
1211 const MyApp ({super .key});
1312
13+ EmailContent get emailContent => EmailContent (
14+ 15+ subject: 'Hello!' ,
16+ body: 'How are you doing? [Debug: ${DateTime .now ()}]' ,
17+ 18+ 19+ attachments: [
20+ // Example file path (must be accessible and supported by the mail app)
21+ // 'file:///path/to/your/file.pdf',
22+ ],
23+ );
24+
1425 @override
1526 Widget build (BuildContext context) {
1627 return Scaffold (
@@ -25,7 +36,7 @@ class MyApp extends StatelessWidget {
2536 ElevatedButton (
2637 child: const Text ("Open Mail App" ),
2738 onPressed: () async {
28- // Try to open the mail app
39+ //TODO: open default mail inbox only
2940 var result = await OpenMail .openMailApp (
3041 nativePickerTitle: 'Select email app to open' ,
3142 );
@@ -50,45 +61,30 @@ class MyApp extends StatelessWidget {
5061
5162 // Button to test mail app detection
5263 ElevatedButton (
53- child: const Text ("Debug Mail App Detection" ),
64+ child: const Text ("Debug Mail App Detection (Custom Picker) " ),
5465 onPressed: () async {
5566 try {
56- // Get installed mail apps with detailed logging
57- // Debug print removed
5867 var options = await OpenMail .getMailApps ();
59- // Debug print removed
60- // Debug print removed
61- // Show the list of detected mail apps
6268 showDialog (
6369 context: context,
64- builder: (_) {
65- return AlertDialog (
66- title: Text ('Detected ${options .length } Mail Apps' ),
67- content: SingleChildScrollView (
68- child: Column (
69- mainAxisSize: MainAxisSize .min,
70- crossAxisAlignment: CrossAxisAlignment .start,
71- children: options
72- .map ((app) => ListTile (
73- title:
74- Text ('${app .name } (${app .nativeId })' ),
75- subtitle: Text (
76- 'ID: ${app .nativeId ?? "No ID" }, Scheme: ${app .iosLaunchScheme ?? "No Scheme" }' ),
77- ))
78- .toList (),
79- ),
80- ),
81- actions: [
82- TextButton (
83- child: const Text ('Close' ),
84- onPressed: () => Navigator .of (context).pop (),
85- ),
86- ],
87- );
88- },
70+ builder: (_) => MailAppPickerDialog (
71+ mailApps: options,
72+ itemBuilder: (context, app, fn) => ListTile (
73+ onTap: () async {
74+ // TODO: open specific mail app inbox only
75+ await OpenMail .openSpecificMailApp (
76+ app.name, emailContent);
77+ },
78+ leading:
79+ const Icon (Icons .email), // Placeholder for icon
80+ title: Text (app.name),
81+ subtitle: Text (app.iosLaunchScheme ?? "No ID" ),
82+ ),
83+ ),
8984 );
9085 } catch (e) {
9186 // Debug print removed
87+ debugPrint ('Error: $e ' );
9288 }
9389 },
9490 ),
@@ -97,48 +93,25 @@ class MyApp extends StatelessWidget {
9793 ElevatedButton (
9894 child: const Text ('Open mail app, with email already composed' ),
9995 onPressed: () async {
100- // Define the content of the email with debug info
101- EmailContent email = EmailContent (
102- to
: [
'[email protected] ' ],
// Recipient(s) 103- subject: 'Hello!' , // Email subject
104- body:
105- 'How are you doing? [Debug: ${DateTime .now ()}]' , // Email body with timestamp for debugging
106- 107- bcc
: [
'[email protected] ' ],
// BCC recipients 108- );
109-
110- // Debug log
111- // Debug print removed
112-
11396 OpenMailAppResult result;
11497
11598 try {
116- // Try to compose a new email in a mail app
11799 result = await OpenMail .composeNewEmailInMailApp (
118100 nativePickerTitle: 'Select email app to compose' ,
119- emailContent: email );
101+ emailContent: emailContent );
120102
121- // Debug log after attempt
122- // Debug print removed
123-
124- // If no mail apps are installed
125103 if (! result.didOpen && ! result.canOpen) {
126104 showNoMailAppsDialog (context);
127- }
128- // If multiple mail apps are available on iOS, show a picker
129- else if (! result.didOpen && result.canOpen) {
105+ } else if (! result.didOpen && result.canOpen) {
130106 showDialog (
131107 context: context,
132108 builder: (_) => MailAppPickerDialog (
133109 mailApps: result.options,
134- emailContent: email ,
110+ emailContent: emailContent ,
135111 ),
136112 );
137113 }
138114 } catch (e) {
139- // Catch and print any exceptions
140- // Debug print removed
141- // Show error dialog to user
142115 showDialog (
143116 context: context,
144117 builder: (_) => AlertDialog (
@@ -212,6 +185,14 @@ class MyApp extends StatelessWidget {
212185 title: const Text ("Open Mail App" ),
213186 content: const Text ("No mail apps installed" ),
214187 actions: < Widget > [
188+ TextButton (
189+ child: const Text ("Settings" ),
190+ onPressed: () async {
191+ // Open device settings (works for both iOS and Android)
192+ await OpenMail .openDeviceSettings ();
193+ Navigator .pop (context);
194+ },
195+ ),
215196 TextButton (
216197 child: const Text ("OK" ),
217198 onPressed: () {
0 commit comments