Flutter 颤振,将表单数据发送到电子邮件

Flutter 颤振,将表单数据发送到电子邮件,flutter,Flutter,我想创建一个联系人表单,并想知道:如何将联系人表单中的数据发送到我的电子邮件? 我想看一个有效的例子。 我想提交这样一份表格: return new Scaffold( appBar: new AppBar( title: new Text(widget.title), actions: <Widget>[ new IconButton(icon: const Icon(Icons.save), onPressed: () {}) ],

我想创建一个联系人表单,并想知道:如何将联系人表单中的数据发送到我的电子邮件? 我想看一个有效的例子。 我想提交这样一份表格:

    return new Scaffold(
  appBar: new AppBar(
    title: new Text(widget.title),
    actions: <Widget>[
      new IconButton(icon: const Icon(Icons.save), onPressed: () {})
    ],
  ),
  body: new Column(
    children: <Widget>[
      new ListTile(
        leading: const Icon(Icons.person),
        title: new TextField(
          decoration: new InputDecoration(
            hintText: "Name",
          ),
        ),
      ),
      new ListTile(
        leading: const Icon(Icons.phone),
        title: new TextField(
          decoration: new InputDecoration(
            hintText: "Phone",
          ),
        ),
      ),
      new ListTile(
        leading: const Icon(Icons.email),
        title: new TextField(
          decoration: new InputDecoration(
            hintText: "Email",
          ),
        ),
      ),
返回新脚手架(
appBar:新的appBar(
标题:新文本(widget.title),
行动:[
新图标按钮(图标:const icon(Icons.save),按下:(){})
],
),
正文:新栏目(
儿童:[
新ListTile(
前导:常量图标(Icons.person),
标题:新文本字段(
装饰:新的输入装饰(
hintText:“名称”,
),
),
),
新ListTile(
前导:常量图标(Icons.phone),
标题:新文本字段(
装饰:新的输入装饰(
hintText:“电话”,
),
),
),
新ListTile(
引导:常量图标(Icons.email),
标题:新文本字段(
装饰:新的输入装饰(
hintText:“电子邮件”,
),
),
),

您可以导航到
默认电子邮件应用程序
。您还可以从您的颤振应用程序设置以下属性

  • 到邮件ID
  • 主题
  • body
  • 使用
    url\u启动器
    插件

    步骤:

  • 将其添加到包的pubspec.yaml文件:
    url\u启动器:“^3.0.1”

  • main.dart
    文件

  • 导入“包装:颤振/材料.省道”;
    导入“package:url_launcher/url_launcher.dart”;
    void main();
    类MyApp扩展了StatefulWidget{
    @凌驾
    _MyAppState createState()=>new_MyAppState();
    }
    类MyAppState扩展了状态{
    @凌驾
    小部件构建(构建上下文){
    归还新脚手架(
    正文:新中心(
    子项:新建RaisedButton(按下时:()=>_启动URL('xxx@gmail.com','flatter Email Test','Hello flatter',child:new Text('Send mail'),),
    ),
    );
    }
    _启动URL(字符串tomaild、字符串主题、字符串正文)异步{
    var url='mailto:$tomaild?subject=$subject&body=$body';
    如果(等待canLaunch(url)){
    等待发射(url);
    }否则{
    抛出“无法启动$url”;
    }
    }
    }
    
    请检查来自Flitter的“mailer”软件包。它将使用smtp从后台发送电子邮件,而无需打开userinterface应用程序。它有gmail、yahoo mail和mailgun选项来发送电子邮件

    参考链接:

    试试这个软件包。下面是一个从他们的

    导入'dart:async';
    导入“dart:io”;
    进口“包装:颤振/材料.省道”;
    导入“package:flatter_email_sender/flatter_email_sender.dart”;
    导入“包:image_picker/image_picker.dart”;
    void main()=>runApp(MyApp());
    类MyApp扩展了StatefulWidget{
    @凌驾
    _MyAppState createState()=>\u MyAppState();
    }
    类MyAppState扩展了状态{
    字符串连接;
    最终_recipientController=文本编辑控制器(
    正文:'example@example.com',
    );
    final _subjectController=TextEditingController(文本:“主题”);
    最终_bodyController=文本编辑控制器(
    文本:“邮件正文”,
    );
    最终GlobalKey _scaffoldKey=GlobalKey();
    Future send()异步{
    最终电子邮件=电子邮件(
    正文:_bodyController.text,
    主题:_subjectController.text,
    收件人:[[u recipientController.text],
    附件路径:附件,
    );
    字符串平台响应;
    试一试{
    等待电子邮件发件人。发送(电子邮件);
    platformResponse=‘成功’;
    }捕获(错误){
    platformResponse=error.toString();
    }
    如果(!已安装)返回;
    _scaffoldKey.currentState.showSnackBar(SnackBar(
    内容:文本(平台响应),
    ));
    }
    @凌驾
    小部件构建(构建上下文){
    最终小部件图像路径=文本(附件??”);
    返回材料PP(
    主题:主题数据(原色:Colors.red),
    家:脚手架(
    钥匙:_scaffoldKey,
    appBar:appBar(
    标题:文本(“插件示例应用程序”),
    行动:[
    图标按钮(
    按:发送,
    图标:图标(Icons.send),
    )
    ],
    ),
    正文:SingleChildScrollView(
    儿童:中心(
    孩子:填充(
    填充:边缘设置。全部(8.0),
    子:列(
    mainAxisSize:mainAxisSize.max,
    //mainAxisAlignment:mainAxisAlignment.spaceBetween,
    crossAxisAlignment:crossAxisAlignment.center,
    儿童:[
    填充物(
    填充:边缘设置。全部(8.0),
    孩子:TextField(
    控制器:_recipientController,
    装饰:输入装饰(
    边框:OutlineInputBorder(),
    labelText:“收件人”,
    ),
    ),
    ),
    填充物(
    填充:边缘设置。全部(8.0),
    孩子:TextField(
    控制器:_subjectController,
    装饰:输入装饰(
    边框:OutlineInputBorder(),
    labelText:“主题”,
    ),
    ),
    ),
    填充物(
    填充:边缘设置。全部(8.0),
    孩子:TextField(
    控制器:_bodyController,
    最大行数:10,
    装饰:输入装饰(
    labelText:“正文”,边框:OutlineInputBorder()),
    ),
    ),
    imagePath,
    ],
    ),
    ),
    ),
    ),
    浮动操作按钮:浮动AC
    
    import 'package:flutter/material.dart';
    import 'package:url_launcher/url_launcher.dart';
    
    void main() => runApp(new MaterialApp(home: new MyApp(), debugShowCheckedModeBanner: false,));
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => new _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: new Center(
            child: new RaisedButton(onPressed: () => _launchURL('xxx@gmail.com', 'Flutter Email Test', 'Hello Flutter'), child: new Text('Send mail'),),
          ),
        );
      }
    
      _launchURL(String toMailId, String subject, String body) async {
        var url = 'mailto:$toMailId?subject=$subject&body=$body';
        if (await canLaunch(url)) {
          await launch(url);
        } else {
          throw 'Could not launch $url';
        }
      }
    }
    
    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> {
      String attachment;
    
      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],
          attachmentPath: attachment,
        );
    
        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) {
        final Widget imagePath = Text(attachment ?? '');
    
        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: Center(
                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()),
                        ),
                      ),
                      imagePath,
                    ],
                  ),
                ),
              ),
            ),
            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(() {
          attachment = pick.path;
        });
      }
    }