flutter_email_sender 3.0.1 flutter_email_sender: ^3.0.1 copied to clipboard
Allows send emails from flutter using native platform functionality.
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_email_sender/flutter_email_sender.dart';
import 'package:image_picker/image_picker.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<String> attachments = [];
bool isHTML = false;
final _recipientController = TextEditingController(
text: 'example@example.com',
);
final _subjectController = TextEditingController(text: 'The subject');
final _bodyController = TextEditingController(
text: 'Mail body.',
);
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
Future<void> send() async {
final Email email = Email(
body: _bodyController.text,
subject: _subjectController.text,
recipients: [_recipientController.text],
attachmentPaths: attachments,
isHTML: isHTML,
);
String platformResponse;
try {
await FlutterEmailSender.send(email);
platformResponse = 'success';
} catch (error) {
platformResponse = error.toString();
}
if (!mounted) return;
_scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text(platformResponse),
));
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primaryColor: Colors.red),
home: Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text('Plugin example app'),
actions: <Widget>[
IconButton(
onPressed: send,
icon: Icon(Icons.send),
)
],
),
body: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.max,
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.all(8.0),
child: TextField(
controller: _recipientController,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Recipient',
),
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: TextField(
controller: _subjectController,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Subject',
),
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: TextField(
controller: _bodyController,
maxLines: 10,
decoration: InputDecoration(
labelText: 'Body', border: OutlineInputBorder()),
),
),
CheckboxListTile(
title: Text('HTML'),
onChanged: (bool value) {
setState(() {
isHTML = value;
});
},
value: isHTML,
),
...attachments.map(
(item) => Text(
item,
overflow: TextOverflow.fade,
),
),
],
),
),
),
floatingActionButton: FloatingActionButton.extended(
icon: Icon(Icons.camera),
label: Text('Add Image'),
onPressed: _openImagePicker,
),
),
);
}
void _openImagePicker() async {
File pick = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
attachments.add(pick.path);
});
}
}