如何解决直接在 onTap 方法中调用函数还是在 Dart/Flutter 中使用胖运算符?
我在我的应用程序中创建了一个函数,它根据我的需要制作自定义小部件。这是函数的代码
Widget customCircularButton(
{String title,String subTitle,String img,void Function() onTap}) {
return Column(
mainAxisSize: MainAxisSize.min,children: [
GestureDetector(
onTap: onTap,child: CircleAvatar(
radius: screenWidth(context) / 6,backgroundColor: pColor,child: CircleAvatar(
radius: screenWidth(context) / 6 - 2,backgroundColor: Colors.white,backgroundImage: Image.asset(img).image,),Container(
margin: EdgeInsets.all(5),child: Text(
title,style: TextStyle(fontWeight: FontWeight.bold,fontSize: 20),Container(
margin: EdgeInsets.all(5),width: screenWidth(context) / 3,child: Text(
subTitle,style: TextStyle(fontWeight: FontWeight.w300,fontSize: 12),textAlign: TextAlign.center,))
],);
}
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,children: [
customCircularButton(
title: 'Donate',subTitle: 'Donate/Buy food for needy',img: 'assets/images/donate-food.png',onTap: donateDiaolg,customCircularButton(
title: 'Become Volunteer',subTitle: 'distribute food to the needy',img: 'assets/images/become-volunteer.png',onTap: () {
Navigator.push(
context,MaterialPageRoute(
builder: (context) => BecomeVolunteer()));
}),],
这里的 donateDialog 是另一个打开对话框的函数。
void donateDiaolg() {
showDialog(..implementation..);}
所以我的疑问是应该使用胖运算符来调用 donateDialog 函数,或者像上面的代码一样调用它。
我的基本问题是我应该使用这个
onTap: () => donateDialog(),
或者这个
onTap: donateDialog(),
或者这样做可以吗
onTap: donateDialog
解决方法
这取决于函数签名。例如 onTap 属性需要没有参数的函数。如果您的自定义函数相同,您可以直接使用您的函数名称,例如 onTap: myFunc。如果您的函数签名采用 1 个或多个参数,您应该将空函数传递给 onTap 并通过它调用您的函数。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。