【问题标题】:How to write function for call button in calling app in flutter?如何在颤动中调用应用程序时为调用按钮编写函数?
【发布时间】:2019-11-09 03:08:48
【问题描述】:

我有一个文本字段,用户在其中输入一个数字和一个 Raisedbutton。通过单击该按钮,用户应该能够拨打该号码。如何为那个按钮编写 onPressed() 函数??

在这段代码之上,我有我的主类,其中 Home() 类被调用

import 'package:flutter/material.dart';

class Home extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return HomeState();
  }
}
class HomeState extends State<Home> {
  TextEditingController numcontroller = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          backgroundColor: Colors.deepPurple,
          title: Text('Calling App'),
        ),
        body: Column(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(top: 60.0, left: 10.0, right: 10.0),
              child: TextField(
                  controller: numcontroller,
                  decoration: InputDecoration(
                      labelText: 'Enter Phone Number',
                      border: OutlineInputBorder(
                          borderSide: BorderSide(
                            color: Colors.deepPurple,
                          )
                      )
                  )
              ),
            ),
            Container(
              height: 45.0,
              width: 90.0,
              margin: EdgeInsets.only(top: 40, left: 10, right: 10.0),
              child: RaisedButton(
                  color: Colors.deepPurple,
                  elevation: 7.0,
                  child: Text(
                    'Call',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 20.0,
                    ),
                  ),
                  onPressed: () {
                    _calling();
                  }
              ),
            )
          ],
        ),
      ),
    );
  }

  void _calling(){
  }
}

假设我输入了一个号码 94********。然后按下该按钮,呼叫应连接到该号码。如果忙或关机,则应显示任何警报消息。

【问题讨论】:

标签: flutter


【解决方案1】:

首先导入URL启动器依赖

  url_launcher: ^5.2.5

那么你的调用函数应该是这样的

_calling() async {
const url = 'tel:+12345678';
if (await canLaunch(url)) {
  await launch(url);
} else {
  throw 'Could not launch $url';
}

}

【讨论】:

    【解决方案2】:

    如果您想从应用程序实际拨打电话,那么您需要CALL_PHONE 权限,该权限被标记为敏感权限,您的应用程序将不兼容 Google Play 政策。

    您可以改为打开手机应用程序, 首先安装 url_launcher 在pubspec.yaml

    dependencies:
      url_launcher: ^5.0.3
    

    然后

    void _calling(){
        _launchURL(numcontroller.text);
    }
    
    
    _launchURL(String phone) async {
    
      const url = 'tel:$phone';
      if (await canLaunch(url)) {
        await launch(url);
      } else {
        throw 'Could not launch $url';
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-07
      • 2022-12-21
      • 1970-01-01
      • 2011-12-21
      • 2021-10-04
      • 2021-09-25
      • 2019-04-28
      • 2019-06-23
      相关资源
      最近更新 更多