如何从 flutter 应用程序进行 phone 调用

How to make a phone call from a flutter app

我尝试从我的 Flutter 应用中发起 phone 调用。使用以下代码:

UrlLauncher.launch('tel: xxxxxxxx');

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

但这对我不起作用。这个函数还在 Flutter 里吗,在哪个包里?或者是否有更好的选择来从我的应用程序进行 phone 调用?

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

你应该在你的 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}'),

我可以通过启动系统 phone 应用程序和连接呼叫来拨打 phone 电话:

这是您需要做的:

  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));
}

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

如果您在点击按钮时没有得到任何动作,那么这是因为这个原因。所以发生这种情况是因为您可能没有在号码前添加 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}'));
},

这对我有用

使用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);
                  },
                )
              ]
            ),
          ),
        );
      }
    }

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"); 

url_launcher: ^ latest Version Pubspec.yamal

注意:在 Pub 获取或升级之前删除 Pubspec.lock 有时它会产生不需要的问题。

导入包导入'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"); 

如果您使用 url_launcher 并且您的 phone 号码有加号,例如“+1111111111” ios 你应该使用 Uri class

final Uri phoneUrl = Uri(
  scheme: 'tel',
  path: '+11111111111',
);

if (await canLaunch(phoneUrl.toString())) {
  await launch(phoneUrl.toString());
} else {
  throw "Can't phone that number.";
}

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

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

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

要启动设备的拨号器,以下代码也可用于异常处理:

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");
  }
}

您可以通过两种方式完成:-

  • url_launcher 包将用于使用手机 phone 的默认应用程序实现 phone 通话。此软件包将自动将 phone 定向到默认的 phone 呼叫应用程序并打开拨号器屏幕进行呼叫。

  • flutter_phone_direct_caller 包不是最好的包,但我们可以直接从 phone 实现直接 phone 调用没有中间拨号器。

在您的 pubspec.yaml 文件中安装以下依赖项:

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.0
  flutter_phone_direct_caller: ^1.0.1
  url_launcher: ^5.7.10

使用“flutter_phone_direct_caller”进行 Phone 调用:插件 使用此插件实现 phone 调用非常简单。这个包为我们提供了 FlutterPhoneDirectCallerclass,它为我们提供了 callNumber() 方法,它接受一个 phone 号码。

_callNumber(String phoneNumber) async {
 String number = phoneNumber;
 await FlutterPhoneDirectCaller.callNumber(number);
}

您可以按如下方式在您的按钮中实现它

RaisedButton(
  child: Text("Call"),
  onPressed: () {
    _callNumber(textEditingController.text);
  },

)

使用“url_launcher”进行 Phone 调用:插件 这个包为我们提供了 launch(URL) 方法,该方法采用 URL 模式。 Scheme 非常重要,因为它指示URL。在 phone 个调用的情况下,我们使用“tel:”模式。

_launchPhoneURL(String phoneNumber) async {
  String url = 'tel:' + phoneNumber;
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

和凸起按钮:-

RaisedButton(
  child: Text("Call"),
  onPressed: () {
    _launchPhoneURL(textEditingController.text);
  },
)

如果你需要高级功能,你可以使用“flutter_voip_kit”,这是一个非常新的库,我也没有使用过它,但乍一看它很有前途..