import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_email_sender/flutter_email_sender.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Home(),
);
}
}
class Home extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _HomeState();
}
}
class _HomeState extends State<Home> {
var _emailFormKey = GlobalKey<FormState>();
TextEditingController emailController = new TextEditingController();
TextEditingController messageController = new TextEditingController();
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
resizeToAvoidBottomPadding: false,
appBar: AppBar(
title: Text("Email sending App"),
),
body: Form(
key: _emailFormKey,
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 50.0, left: 15.0, right: 15.0),
child: TextFormField(
controller: emailController,
validator: (value){
if(value.isEmpty) {
return "please Enter email";
}
},
decoration: InputDecoration(
labelText: "Enter email",
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.red,
width: 2.0,
))),
),
),
Container(
margin: EdgeInsets.only(top: 15.0, left: 15.0, right: 15.0),
child: TextFormField(
controller: messageController,
validator: (value){
if(value.isEmpty) {
return "please Enter message";
}
},
decoration: InputDecoration(
labelText: "Enter Message",
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.red,
width: 2.0,
))),
),
),
Container(
margin: EdgeInsets.only(top: 15.0),
child: RaisedButton(child: Text("Send"), onPressed: (){
if(_emailFormKey.currentState.validate()){
sendMessage();
}
}),
)
],
),
),
);
}
void sendMessage(){
String inputEmail ;
String inputMessage;
Email email ;
setState(() {
inputEmail= emailController.text;
inputMessage = messageController.text;
if(inputMessage.isNotEmpty&&inputEmail.isNotEmpty) {
email = Email(
body: inputMessage,
subject: 'Email subject',
recipients: [inputEmail],
);
send(email);
}
});
debugPrint('email - > $inputEmail message -> $inputMessage');
}
void send(Email email) async {
await FlutterEmailSender.send(email);
}
}
// пожалуйста, добавьте плагин "flutter_email_sender:" в файл pubspec.yaml