繁体   English   中英

如何从 ElevatedButton onPressed 调用 function - Flutter

[英]How to call a function from ElevatedButton onPressed - Flutter

我是 flutter 的新手,我尝试将呼叫 function 和 email function 添加到 Elevated Button。 当有人点击呼叫按钮时,呼叫应该是 go 到电话号码就可以了。 当点击 email 按钮时,应该有机会发送 email。

这是我的代码文件:

 Center(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: SizedBox(
                      width: 164,
                      height: 105,
                      child: ElevatedButton(
                          onPressed: () {},
                          style: ElevatedButton.styleFrom(
                            primary: const Color.fromRGBO(189, 31, 45, 1),
                            onPrimary: Colors.grey,
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(13),
                            ),
                          ),
                          child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: <Widget>[
                                Padding(
                                  padding: const EdgeInsets.all(8.0),
                                  child: Image.asset(
                                    'assets/images/phone-call.png',
                                    width: 39,
                                    height: 39,
                                  ),
                                ),
                                const Text("+94112802471",
                                    style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 13,
                                        fontFamily: 'VisbyCF'))
                              ])),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(18),
                    child: SizedBox(
                      width: 164,
                      height: 105,
                      child: ElevatedButton(
                          onPressed: () {},
                          style: ElevatedButton.styleFrom(
                            primary: Color.fromARGB(255, 245, 245, 245),
                            onPrimary: const Color.fromRGBO(189, 31, 45, 1),
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(12),
                            ),
                          ),
                          child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: <Widget>[
                                Padding(
                                  padding: const EdgeInsets.all(5.0),
                                  child: Image.asset(
                                    'assets/images/Layer 2.png',
                                    width: 39,
                                    height: 39,
                                  ),
                                ),
                                const Text("contact@archmage.lk",
                                    style: TextStyle(
                                        color:
                                            Color.fromARGB(227, 146, 146, 146),
                                        fontSize: 13,
                                        fontFamily: 'VisbyCF'))
                              ])),
                    ),
                  ),
                ],
              ),
            ),

在这里我使用了两个升高的按钮,呼叫按钮和 email 按钮

使用url_launcher package 你可以实现这个:

对于公开通话,请执行以下操作:

onPressed: () async{

    final Uri launchUri = Uri(
      scheme: 'tel',
      path:'+94112802471',
    );
    if (!await launchUrl(launchUri)) {
       throw 'Could not launch $launchUri';
    }
},

要发送 email,请执行以下操作:

onPressed: () async{
    
 final Uri params = Uri(
   scheme: 'mailto',
   path: 'email@example.com',
   query: '...', // <--- add subject and body here
 );

 var url = params.toString();
 if (!await launchUrl(url)) {
   throw 'Could not launch $url';
 }
},

Not :不要忘记 package 网站中提到的配置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM