How to make a phone call from a flutter app

后端 未结 7 881
迷失自我
迷失自我 2020-12-03 00:39

I try to make a phone call from my Flutter app. With the following code:

UrlLauncher.launch(\'tel: xxxxxxxx\');

I found this Function on the

7条回答
  •  自闭症患者
    2020-12-03 01:08

    I am able to make a phone call by bringing up the system phone app and CONNECT A CALL:

    Here's what you need to do:

    1. pubspec.yaml add package:

      intent:

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

    Then, after running this app and click the "Dial a number", the System Phone App will bring up and make a call. (Unlike url_launcher, you don't need to press the Green Call button in the System Phone App)

提交回复
热议问题