这是我班上的代码...
class List extends StatefulWidget {
@override
_ListState createState() => _ListState();
}
class _ListState extends State<List> {
Future<void> _makePhoneCall(String url) async {
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: customAppbar('Contact Us'),
body: ListView(children: <Widget>[
_listTile('1234-1234567-123', 'Exam Enquiry', Icons.call,
_makePhoneCall('tel:12345'))
]));
}
}
我想将_makePhoneCall函数传递给小部件_listTile但出现错误:
不能将参数类型“ Future”分配给参数类型“ Future Function(String)”
_listTile小部件的代码如下:
Widget _listTile(String title, String subTitle, IconData iconData,
Future<void> _makePhoneCall(String url)) {
return Padding(
padding: EdgeInsets.only(left: 24.0),
child: ListTile(
title: Text(title),
subtitle: Text(subTitle),
trailing: Padding(
padding: const EdgeInsets.all(0.0),
child: RawMaterialButton(
onPressed: () => _makePhoneCall,
child: Icon(
iconData,
color: Colors.white,
),
elevation: 2,
shape: CircleBorder(),
fillColor: Color(0xff7061aa),
),
),
),
);
}
答案 0 :(得分:0)
以下是必要的更改:
body: ListView(children: <Widget>[
_listTile('1234-1234567-123', 'Exam Enquiry', Icons.call,
_makePhoneCall)
]));
child: RawMaterialButton(
onPressed: () => _makePhoneCall('tel:12345'),