颤振Json数据转换
我正在从用户处获取联系信息。并通过电子邮件发送给另一个人。但我想以结构化格式发送信息,例如表格等。 现在电子邮件看起来像:{“姓名”:“约翰逊”,“日期”:“12-12-2020”,“电话”:(387)890-0987”,“电子邮件”:usename@domain.com“}颤振Json数据转换,json,flutter,email,encode,Json,Flutter,Email,Encode,我正在从用户处获取联系信息。并通过电子邮件发送给另一个人。但我想以结构化格式发送信息,例如表格等。 现在电子邮件看起来像:{“姓名”:“约翰逊”,“日期”:“12-12-2020”,“电话”:(387)890-0987”,“电子邮件”:usename@domain.com“} 我想以表格的形式发送数据。如下所示 或 不像 {“姓名”:“约翰逊”,“日期”:“12-12-2020”,“电话”:(387)890-0987”,“电子邮件”:usename@domain.com“} 这是我的密码:
我想以表格的形式发送数据。如下所示 或 不像 {“姓名”:“约翰逊”,“日期”:“12-12-2020”,“电话”:(387)890-0987”,“电子邮件”:usename@domain.com“} 这是我的密码:
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,
)),
],
))))),
);
}
}
导入“dart:ui”;
进口“包装:颤振/材料.省道”;
导入“包:flifter/services.dart”;
导入“dart:async”;
导入“dart:convert”;
导入“package:mailer2/mailer.dart”;
void main();
类StepSix会话Six{
未来createContact(联系人)异步{
试一试{
字符串json=_toJson(联系人);
var options=new GmailSmtpOptions()
…用户名='myusername'
..密码='mypassword';
var emailTransport=新的SmtpTransport(选项);
var信封=新信封()
…来自email@gmail.com'
..收件人。添加('anotheremail@outlook.com')
…主题='测试2'
..html=json;
emailTransport.send(信封)
。然后((信封)=>打印('Email sent!'))
.catchError((e)=>print('Error occurrent:$e');
}捕获(e){
打印('服务器异常!!!');
印刷品(e);
返回null;
}
}
字符串_toJson(联系人){
var mapData=新映射();
mapData[“Name”]=contact.Name;
mapData[“Phone”]=contact.phones;
mapData[“Email”]=contact.Email;
mapData[“Message Subject”]=contact.messagesubject;//会话1第一次的日期
mapData[“Message Text”]=contact.messagetext;//会话1第一次的日期
字符串json=jsonEncode(mapData);
返回json;
}
}
班级联系{
字符串名;
字符串电话=“”;
字符串=“”;
字符串消息主题;
字符串消息文本;
}
布尔isValidPhoneNumber(字符串输入){
final RegExp regex=new RegExp(r'^\(\d\d\d\)\d\d\-\d\d\d$);
返回regex.hasMatch(输入);
}
void showMessage(字符串消息,[MaterialColor=Colors.red]){
_scaffoldKey.currentState
.showSnackBar(新SnackBar(背景颜色:颜色,内容:新文本(消息));
}
void _submitForm(){
final FormState form=\u formKey.currentState;
如果(!form.validate()){
showMessage('表单无效!请检查并更正');
}否则{
form.save();//这将调用每个onsave事件
var stepsixsessionsix=新的stepsixsessionsix();
步骤六sessionsix.createContact(newContact);
showMessage('Form Submitted',Colors.blue);
}
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回新材料PP(
标题:“颤振形式演示”,
主题:新主题数据(
颜色:颜色。白色,
主样本:颜色。黄色,
),
主页:新MyHomePage(标题:“颤振表单演示”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>new_MyHomePageState();
}
联系人newContact=新联系人();
最终GlobalKey _scaffoldKey=新的GlobalKey();
final GlobalKey _formKey=新GlobalKey();
类_MyHomePageState扩展状态{
bool isValidEmail(字符串输入){
最终RegExp regex=new RegExp(r“^[a-zA-Z0-9.!$%&”*+/=?^{124;}-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]
{0253}[a-zA-Z0-9])?(?:\[a-zA-Z0-9](?:[a-zA-Z0-9-]{0253}[a-zA-Z0-9])?)*$”;
返回regex.hasMatch(输入);
}
双项高度=30;
小部件getTextField({String hint='components',小部件后缀}){
返回容器(
高度:itemsHeight,
孩子:TextField(
装饰:输入装饰(
边框:大纲输入边框(
borderRadius:borderRadius.0,
borderSide:borderSide(颜色:Colors.white,宽度:1)),
提示:,
contentPadding:EdgeInsets.all(
0),//更改每个值,并设置0个剩余值。
后缀:后缀,
),
扩展:false,
maxLines:1,
控制器:TextEditingController(),
),
);
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
钥匙:_scaffoldKey,
appBar:新的appBar(
标题:新文本(“联系人”),
背景颜色:颜色(0xFFF6D580),
),
背景颜色:Colors.black,
正文:新安全区(
上图:错,
底部:错误,
儿童:新表格(
键:_formKey,
自动验证:true,
子容器:新容器(
填充:仅限常量边集(顶部:20.0),
子:SingleChildScrollView(
子:新列(
儿童:[
新文本(“联系我们”,样式:TextStyle(字体大小:25.0,颜色:常量
颜色(0xFFFFFF)),textAlign:textAlign.center),
新TextFormField(
样式:TextStyle(
颜色:颜色,白色,
),
装饰:常量输入装饰(
图标:常量图标(Icons.per