【问题标题】:How to make a phone call from a flutter app如何从 Flutter 应用程序拨打电话
【发布时间】:2018-01-13 08:53:15
【问题描述】:

我尝试通过我的 Flutter 应用拨打电话。使用以下代码:

UrlLauncher.launch('tel: xxxxxxxx');

我在 GitHub Flutter repo 上找到了这个函数:https://github.com/flutter/flutter/issues/4856

但这对我不起作用。这个函数是否还在 Flutter 中,在哪个包中?还是有更好的选择可以通过我的应用拨打电话?

【问题讨论】:

标签: flutter dart phone-call


【解决方案1】:

url_launcher 包中调用launch 方法:

launch("tel://214324234");

这是完整的代码:

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      home: new Home(),
    );
  }
}

class Home extends StatelessWidget {
  Home({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) => new Scaffold(
        appBar: new AppBar(
          title: new Text("View"),
        ),
        body: new Center(
          child: new FlatButton(
              onPressed: () => launch("tel://21213123123"),
              child: new Text("Call me")),
        ),
      );
}

void main() {
  runApp(
    new MyApp(),
  );
}

你也可以导入它然后使用

import 'package:url_launcher/url_launcher.dart' as UrlLauncher;
UrlLauncher.launch("tel://21213123123")

确保在 pubspec.yaml 文件的依赖项部分中包含一个条目:

url_launcher: ^1.0.2

【讨论】:

  • 你是如何在代码中使用它的?我的问题是找不到 UrlLauncher。
  • 你需要在 pubspec.yaml 中添加 'dependencies: url_launcher: "^3.0.0 " 并运行包升级
  • @GermanSaprykin 即使我在 pubspec.yaml 文件中添加了 url_launcher: ^4.0.3,我仍然缺少插件异常
  • @Nudge 不要忘记通过“flutter pub get”安装包并重新启动应用程序
  • 根据pub.dartlang.org/packages/url_launcher#-readme-tab-,电话地址应该是tel:,例如电话:+1 555 010 999
【解决方案2】:

您应该将其添加到您的 pubspec.yaml => url_launcher: ^5.0.2 然后点击 Packages get 。

在您的代码中添加导入: import 'package:url_launcher/url_launcher.dart' as UrlLauncher; 希望它有效 =)

import 'package:url_launcher/url_launcher.dart' as UrlLauncher;


    UrlLauncher.launch('tel:+${p.phone.toString()}')

        //if mail 
     UrlLauncher.launch('mailto:${p.email}'),

【讨论】:

  • 我猜这不会接通电话,而是将您重定向到带有电话号码的内置电话拨号器应用程序是否有办法从颤振应用程序本身单击按钮实际拨打电话
  • 我认为没有办法。
  • 那个叫做 truecaller 的应用怎么样,我认为它做的对吗?当然它肯定不是在 Flutter 中构建的,但是某种平台通道代码可以提供帮助
【解决方案3】:

如果您在单击按钮时没有得到任何操作,那么这就是因为这个而发生的。之所以发生这种情况,是因为您可能没有在号码前添加 tel://

这样做

完整代码如下

launch(('tel://${mobile_no}'));       //launch(('tel://99999xxxxx'));

1) 在 pubspec.yaml 中

dependencies:
  flutter:
    sdk: flutter

url_launcher: ^5.4.10

2) 导入到任何你想使用的地方

import 'package:url_launcher/url_launcher.dart';

3) 最后调用

onPressed: () {
  launch(('tel://${item.mobile_no}'));
},

【讨论】:

【解决方案4】:

这对我有用

使用this plugin

import 'package:flutter/material.dart';
    import 'dart:async';
    
    import 'package:flutter/services.dart';
    import 'package:flutter_phone_direct_caller/flutter_phone_direct_caller.dart';
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => new _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      TextEditingController _numberCtrl = new TextEditingController();
    
      @override
      void initState() {
        super.initState();
        _numberCtrl.text = "085921191121";
      }
    
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          home: new Scaffold(
            appBar: new AppBar(
              title: const Text('Plugin example app'),
            ),
            body: new Column(
              children:<Widget>[
                Padding(
                  padding: EdgeInsets.all(8.0),
                  child: TextField(
                    controller: _numberCtrl,
                    decoration: InputDecoration(
                      labelText: "Phone Number"
                    ),
                    keyboardType: TextInputType.number,
                  ),
                ),
                RaisedButton(
                  child: Text("Test Call"),
                  onPressed: () async{
                    FlutterPhoneDirectCaller.callNumber(_numberCtrl.text);
                  },
                )
              ]
            ),
          ),
        );
      }
    }

【讨论】:

  • 你在 iPhone 上试过了吗?在 Android 上运行 iPhone 失败。
【解决方案5】:

我可以通过打开系统电话应用程序并拨打电话来拨打电话:

这是你需要做的:

  1. pubspec.yaml 添加包:

    意图:

  2. main.dart:

import 'package:flutter/material.dart';
import 'package:intent/intent.dart' as android_intent;
import 'package:intent/action.dart' as android_action;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);


  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    return (Scaffold(
      body: Center(
        child: RaisedButton(
                 onPressed: _launchURL,
                 child: Text('Dial a number'),
               )
      ),
    ));
  }
}


_launchURL() async {
  // Replace 12345678 with your tel. no.

  android_intent.Intent()
    ..setAction(android_action.Action.ACTION_CALL)
    ..setData(Uri(scheme: "tel", path: "12345678"))
    ..startActivity().catchError((e) => print(e));
}

然后,运行这个应用程序并点击“拨号”,系统电话应用程序将弹出并拨打电话。 (与url_launcher不同,您无需在System Phone App中按下绿色呼叫按钮)

【讨论】:

  • 那么IOS呢?
  • @Kenneth Li,我也想知道如何在 iOS 中实现。
  • 我在 IOS 中找不到任何自动“按下绿色按钮”的解决方案
  • @KennethLi 嗨,如何获取有关事件的信息,例如通话何时结束。你知道吗?我真的需要这个。
【解决方案6】:

url_launcher是通用的启动url、拨打号码和发送邮件的包。

  1. url_launcher: ^5.5.2 添加到pubspec.yaml 文件并运行flutter pub get
  2. 导入包import 'package:url_launcher/url_launcher.dart';
  3. 定义函数:
void launchUrl(String url) async {
  if (await canLaunch(url)) {
    launch(url);
  } else {
    throw "Could not launch $url";
  }
}
  1. 出于不同目的调用您的通用函数:
//for launching url
launchUrl("HTTP://example.com");

// for dial phone number
launchUrl("tel:+99364921507"); 

// for sending email
launchUrl("mailto:zehinz@gmail.com?subject=Meeting&body=Can we meet via Google Meet"); 

【讨论】:

    【解决方案7】:

    只需 url_launcher: ^ latest Version 在 Pubspec.yamal 中

    注意:在 Pub 获取或升级之前删除 Pubspec.lock 一段时间会产生不必要的问题。

    导入包 import 'package:url_launcher/url_launcher.dart';

    //for launching URL
    
    launchUrl("HTTP://website.com");
    
    
    // for dial phone number
    
    launchUrl("tel:+91963852741"); 
    
    
    // for sending email
    
    launchUrl("mailto:mail@gmail.com?subject=Meeting&body=Can we meet via Google Meet"); 
    

    【讨论】:

      【解决方案8】:

      如果你使用 url_launcher 并且你的电话号码有加号,如 ios 的“+1111111111”,你应该使用 Uri 类

      final Uri phoneUrl = Uri(
        scheme: 'tel',
        path: '+11111111111',
      );
      
      if (await canLaunch(phoneUrl.toString())) {
        await launch(phoneUrl.toString());
      } else {
        throw "Can't phone that number.";
      }
      

      【讨论】:

        【解决方案9】:

        要启动设备的拨号器,还可以使用以下代码进行异常处理:

        Future<void> launchPhoneDialer(String contactNumber) async {
          final Uri _phoneUri = Uri(
              scheme: "tel",
              path: contactNumber
          );
          try {
            if (await canLaunch(_phoneUri.toString()))
              await launch(_phoneUri.toString());
          } catch (error) {
            throw("Cannot dial");
          }
        }
        

        【讨论】:

          【解决方案10】:

          可以直接通过这个包调用flutter_phone_direct_caller

          创建函数并传递手机号码值:

            _callNumber(String mobile) async {
               await FlutterPhoneDirectCaller.callNumber(mobile);
            }
          

          【讨论】:

            猜你喜欢
            • 2017-08-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-06-19
            • 2023-03-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多