我有一个文本字段,用户可以在其中输入一个号码和一个 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*********。然后在按下该按钮时,调用应该连接到这个号码。如果占线或关机,则应该显示任何警报消息。
最佳答案
首先导入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';
}
关于flutter - 如何在 flutter 中调用应用程序时为调用按钮编写函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56785200/