I am getting contact information from user. And sending it to another person via email. But I want to send the information in structural format, for example table etc.
Now the email is looks like: {"Name":"Johnson","Date":"12-12-2020","Phone":"(387)890-0987","Email":"usename@domain.com"}
I want to send data in the form of table. Shown below.
OR
Not like {"Name":"Johnson","Date":"12-12-2020","Phone":"(387)890-0987","Email":"usename@domain.com"}
here is my code:
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:async';
import 'dart:convert';
import 'package:mailer2/mailer.dart';
void main() => runApp(new MaterialApp(home: new MyApp(), debugShowCheckedModeBanner: false,));
class StepSixSessionSix {
Future<Contact> createContact(Contact contact) async {
try {
String json = _toJson(contact);
var options = new GmailSmtpOptions()
..username = 'myusername'
..password = 'mypassword';
var emailTransport = new SmtpTransport(options);
var envelope = new Envelope()
..from = 'email@gmail.com'
..recipients.add('anotheremail@outlook.com')
..subject = 'Testing2'
..html = json;
emailTransport.send(envelope)
.then((envelope) => print('Email sent!'))
.catchError((e) => print('Error occurred: $e'));
} catch (e) {
print('Server Exception!!!');
print(e);
return null;
}
}
String _toJson(Contact contact) {
var mapData = new Map();
mapData["Name"] = contact.name ;
mapData["Phone"] = contact.phones;
mapData["Email"] = contact.emails;
mapData["Message Subject "] = contact.messagesubject; // date for session 1's first
mapData["Message Text "] = contact.messagetext; // date for session 1's first
String json = jsonEncode(mapData);
return json;
}
}
class Contact {
String name;
String phones = '';
String emails= '';
String messagesubject;
String messagetext;
}
bool isValidPhoneNumber(String input) {
final RegExp regex = new RegExp(r'^\(\d\d\d\)\d\d\d\-\d\d\d\d$');
return regex.hasMatch(input);
}
void showMessage(String message, [MaterialColor color = Colors.red]) {
_scaffoldKey.currentState
.showSnackBar(new SnackBar(backgroundColor: color, content: new Text(message)));
}
void _submitForm() {
final FormState form = _formKey.currentState;
if (!form.validate()) {
showMessage('Form is not valid! Please review and correct.');
} else {
form.save(); //This invokes each onSaved event
var stepsixsessionsix = new StepSixSessionSix();
stepsixsessionsix.createContact(newContact);
showMessage('Form Submitted', Colors.blue);
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Form Demo',
theme: new ThemeData(
hintColor: Colors.white,
primarySwatch: Colors.yellow,
),
home: new MyHomePage(title: 'Flutter Form Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
Contact newContact = new Contact();
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
class _MyHomePageState extends State<MyHomePage> {
bool isValidEmail(String input) {
final RegExp regex = new RegExp(r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]
{0,253}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?)*$");
return regex.hasMatch(input);
}
double itemsHeight = 30;
Widget getTextField({String hint = 'Ingredients', Widget suffix}) {
return Container(
height: itemsHeight,
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.zero,
borderSide: BorderSide(color: Colors.white, width: 1)),
hintText: hint,
contentPadding: EdgeInsets.all(
0), // change each value, and set 0 remainding ones.
suffixIcon: suffix,
),
expands: false,
maxLines: 1,
controller: TextEditingController(),
),
);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
key: _scaffoldKey,
appBar: new AppBar(
title: new Text('Contact'),
backgroundColor: Color(0xFFF6D580),
),
backgroundColor: Colors.black,
body: new SafeArea(
top: false,
bottom: false,
child: new Form(
key: _formKey,
autovalidate: true,
child: new Container(
padding: const EdgeInsets.only( top: 20.0),
child: SingleChildScrollView(
child: new Column(
children: <Widget>[
new Text('Contact Us',style: TextStyle(fontSize: 25.0, color: const
Color(0xFFffffff)),textAlign: TextAlign.center),
new TextFormField(
style: TextStyle(
color: Colors.white,
),
decoration: const InputDecoration(
icon: const Icon(Icons.person, color: Colors.grey,),
hintText: 'Enter your first and last name',
labelText: 'Your Name',
),
inputFormatters: [new LengthLimitingTextInputFormatter(30)],
onSaved: (val) => newContact.name = val,
),
new TextFormField(
style: TextStyle(
color: Colors.white,
),
decoration: const InputDecoration(
icon: const Icon(Icons.phone,color: Colors.grey,),
hintText: 'Enter a phone number',
labelText: 'Your phone',
),
keyboardType: TextInputType.phone,
inputFormatters: [
new WhitelistingTextInputFormatter(
new RegExp(r'^[()\d -]{1,15}$')),
],
validator: (val) => isValidPhoneNumber(val)
? null
: 'Phone number must be entered as (###)###-####',
onSaved: (val) => newContact.phones = val,
),
new TextFormField(
style: TextStyle(
color: Colors.white,
),
decoration: const InputDecoration(
icon: const Icon(Icons.email,color: Colors.grey,),
hintText: 'Enter a email address',
labelText: 'Your email',
),
keyboardType: TextInputType.emailAddress,
validator: (val) => isValidEmail(val)
? null
: 'Please enter a valid email address',
onSaved: (val) => newContact.emails = val,
),
new TextFormField(
style: TextStyle(
color: Colors.white,
),
decoration: const InputDecoration(
labelText: 'Message subject',
),
inputFormatters: [new LengthLimitingTextInputFormatter(30)],
onSaved: (val) => newContact.messagesubject = val,
),
new TextFormField(
style: TextStyle(
color: Colors.white,
),
decoration: const InputDecoration(
labelText: 'Message text',
),
inputFormatters: [new LengthLimitingTextInputFormatter(30)],
onSaved: (val) => newContact.messagetext = val,
),
new Container(
padding: const EdgeInsets.only( top: 20.0),
child: new RaisedButton(
color: const Color(0xFFF6D580),
child: const Text('Send',style: TextStyle(fontSize: 20.0),),
onPressed: _submitForm,
)),
],
))))),
);
}
}
Thank you!
User contributions licensed under CC BY-SA 3.0