flutter_mailer 0.1.2 flutter_mailer: ^0.1.2 copied to clipboard
Share an email to device Email - Client supports multiple Attachments
import 'dart:io';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_mailer/flutter_mailer.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<String> attachment = [];
TextEditingController _subjectController =
new TextEditingController(text: 'the Subject');
TextEditingController _bodyController = new TextEditingController(text: """
<em>the body has <code>HTML</code></em> <br><br><br>
<strong>Some Apps like Gmail might ignore it</strong>
""");
final GlobalKey<ScaffoldState> _scafoldKey = new GlobalKey<ScaffoldState>();
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> send() async {
// Platform messages may fail, so we use a try/catch PlatformException.
final MailOptions mailOptions = MailOptions(
body: _bodyController.text,
subject: _subjectController.text,
recipients: ['example@example.com'],
isHTML: true,
// bccRecipients: ['other@example.com'],
ccRecipients: ['third@example.com'],
attachments: attachment,
);
String platformResponse;
try {
await FlutterMailer.send(mailOptions);
platformResponse = 'success';
} catch (error) {
platformResponse = error.toString();
}
// 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;
_scafoldKey.currentState.showSnackBar(SnackBar(
content: Text(platformResponse),
));
}
@override
Widget build(BuildContext context) {
final Widget imagePath =
Column(children: attachment.map((file) => Text('$file')).toList());
return new MaterialApp(
theme: ThemeData(primaryColor: Colors.red),
home: new Scaffold(
key: _scafoldKey,
appBar: new AppBar(
title: const Text('Plugin example app'),
actions: <Widget>[
IconButton(
onPressed: send,
icon: Icon(Icons.send),
)
],
),
body: SingleChildScrollView(
child: new Center(
child: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.max,
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: _subjectController,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Subject',
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: _bodyController,
maxLines: 10,
decoration: InputDecoration(
labelText: 'Body', border: OutlineInputBorder()),
),
),
imagePath,
],
),
),
),
),
floatingActionButton: FloatingActionButton.extended(
icon: Icon(Icons.camera),
label: Text('Add Image'),
onPressed: _picker,
),
floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
bottomNavigationBar: BottomAppBar(
notchMargin: 4.0,
// shape: CircularNotchedRectangle(),
// color: Theme.of(context).primaryColor,
child: Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Builder(
builder: (context) => FlatButton(
textColor: Theme.of(context).primaryColor,
child: Text('add text File'),
onPressed: () => _onCreateFile(context),
),
)
],
),
),
),
);
}
void _picker() async {
File pick = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
attachment.add(pick.path);
});
}
/// create a text file in Temporary Directory to share.
_onCreateFile(BuildContext context) async {
final TempFile tempFile = await _showDialog(context);
final File newFile = await writeFile(tempFile.content, tempFile.name);
setState(() {
attachment.add(newFile.path);
});
}
/// some A simple dialog and return fileName and content
Future<TempFile> _showDialog(BuildContext context) {
return showDialog<TempFile>(
context: context,
builder: (context) {
String content = '';
String fileName = '';
return SimpleDialog(
title: Text('write something to a file'),
contentPadding: EdgeInsets.all(8.0),
children: <Widget>[
TextField(
onChanged: (str) => fileName = str,
autofocus: true,
decoration: InputDecoration(
suffix: Text('.txt'),
labelText: 'file name'),
),
TextField(
decoration: InputDecoration(
hasFloatingPlaceholder: true,
labelText: 'Content',
),
onChanged: (str) => content = str,
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
RaisedButton(
color: Theme.of(context).accentColor,
child: Icon(Icons.save),
onPressed: () {
TempFile tempFile = TempFile(content: content, name: fileName);
// Map.from({'content': content, 'fileName': fileName});
Navigator.of(context).pop<TempFile>(tempFile);
},
),
],
)
],
);
},
);
}
Future<String> get _localPath async {
final directory = await getTemporaryDirectory();
return directory.path;
}
Future<File> _localFile(String fileName) async {
final path = await _localPath;
return File('$path/$fileName.txt');
}
Future<File> writeFile(String text, [String fileName = '']) async {
fileName = fileName.isNotEmpty ? fileName : 'fileName';
final file = await _localFile(fileName);
// Write the file
return file.writeAsString('$text');
}
}
class TempFile {
final String name, content;
TempFile({this.name, this.content});
}