Android 如何将onTap功能传递到颤振中的ListTile?
我的抽屉内容:Android 如何将onTap功能传递到颤振中的ListTile?,android,flutter,dart,visual-studio-code,flutter-layout,Android,Flutter,Dart,Visual Studio Code,Flutter Layout,我的抽屉内容: drawer: Drawer( elevation: 0.0, child: Container( color: kBackgroundColor, child: ListView( padding: EdgeInsets.zero, children: <Widget>[ DrawerHeader( decoration: BoxDecoration(
drawer: Drawer(
elevation: 0.0,
child: Container(
color: kBackgroundColor,
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
decoration: BoxDecoration(
color: Colors.grey,
image: DecorationImage(
alignment: Alignment.topRight,
image: AssetImage('images/banner.png'),
fit: BoxFit.cover,
),
),
child: Text('App Test'),
),
ReusableOptionTile(
textData: 'Home',
icon: Icons.home,
onTouch: () {
print('Home Clicked');
Navigator.pop(context);
},
),
]
),
),
),
我从抽屉中提取了列表磁贴,以使代码更加可重用,但现在无法传递不同磁贴的点击功能。
在错误点显示错误“无法将参数类型“Function”分配给参数类型“void Function()?”
如何解决此问题?
onTouch
应提取为无效函数
void onTouch(){
print('Home Clicked');
Navigator.pop(context);
}
然后
打电话
ListTile(
title: Row(
children: [
Icon(icon),
SizedBox(
width: 5.0,
),
Text('$textData'),
],
),
onTap: onTouch(),
);
您是否尝试将onTouch的类型更改为“void Function()?”?谢谢Arun,但现在出现了错误“正文可能正常完成,导致返回“null”,但返回类型可能是不可为null的类型。请尝试在末尾添加return或throw语句。”在调用点(从抽屉中)您必须在ReusableOptionFile中调用onTouch<代码>onTap:onTouch()。检查Len_X的答案,了解完整的结构。
ReusableOptionTile(
textData: 'Home',
icon: Icons.home,
onTouch: onTouch,
),
ListTile(
title: Row(
children: [
Icon(icon),
SizedBox(
width: 5.0,
),
Text('$textData'),
],
),
onTap: onTouch(),
);