如何从 Flutter 中拨打 phone?

How can I dial the phone from Flutter?

我正在构建一个 Flutter 应用程序,我想拨打 phone 号码以响应按钮点击。执行此操作的最佳方法是什么?

谢谢!

通常,要与底层平台交互,您必须编写特定于平台的代码并使用 platform channels. However, Flutter provides some points of integration with the platform out of the box. To dial the phone for instance, you can use the UrlLauncher.launch API 与 tel 方案进行拨号 phone.

UrlLauncher.launch("tel://<phone_number>"); 这样的东西应该在所有平台上都能正常工作。

请注意,这在模拟器中不起作用。因此,请确保您使用的是实际设备来对此进行测试。

此方法将打开拨号器:

_launchCaller() async {
    const url = "tel:1234567";   
    if (await canLaunch(url)) {
       await launch(url);
    } else {
      throw 'Could not launch $url';
    }   
}

编辑:

如果有人遇到错误:

在pubspec.yaml&运行中添加url_launcher:flutter get

还有import 'package:url_launcher/url_launcher.dart';

您可以使用 url_launcher 小部件 (https://pub.dev/packages/url_launcher)

  1. 将此添加到您的包的 pubspec.yaml 文件中:dependencies: url_launcher: ^5.7.10

  2. 安装它:$ flutter pub get

  3. 导入它import 'package:url_launcher/url_launcher.dart';

  4. 在您的 class 中定义此方法,以便您可以从代码中的任何操作调用:

     Future<void> _makePhoneCall(String url) async {
     if (await canLaunch(url)) {
       await launch(url);
     } else {
       throw 'Could not launch $url';
     }
    

    }

  5. 构建小部件内:

     IconButton(icon: new Icon(Icons.phone),
        onPressed: () 
        {
           setState(() {
              _makePhoneCall('tel:0597924917');
           });
        },
      ),
    

注1:phone号码要写'tel'前缀:'tel:0123456789'

注意2:有时在手机中关闭应用程序并重新打开它会无法正常工作,因此flutter可以成功注入新widget的代码。

too easy

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

void main() {
  runApp(Scaffold(
    body: Center(
      child: RaisedButton(
        onPressed: _callNumber,
        child: Text('Call Number'),
      ),
    ),
  ));
}

_callNumber() async{
  const number = '08592119XXXX'; //set the number here
  bool res = await FlutterPhoneDirectCaller.callNumber(number);
}

按照以下步骤操作:

  1. 在你的 pubspec.yaml

    中添加 url_launcher: Latest version 依赖项
  2. 转到\android\app\src\main\AndroidManifest.xml

  3. 之前添加查询行,如下所示:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
         package="com.example.flutter">
         <queries>
             <intent>
                 <action android:name="android.intent.action.DIAL" />
                 <data android:scheme="tel" />
             </intent>
         </queries>
        <application ...
    

如需更多查询,请关注此more queries

4.Add拨号功能,并设置url、final url ="tel:$phoneNumber";像这样:

  Future<void> dialNumber(
      {required String phoneNumber, required BuildContext context}) async {
    final url = "tel:$phoneNumber";
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      ShowSnackBar.showSnackBar(context, "Unable to call $phoneNumber");
    }

    return;
  }
  1. 完成

URL 启动器已经更新了它们的依赖项,所以这里是完成这项工作的更新方法。它适用于 android ios

final Uri launchUri = Uri(
  scheme: 'tel',
  path: number,
);
await launchUrl(launchUri);

没用过这个插件的就imoprt这个插件吧

UrlLauncher