Reputation: 43
My Drawer Content:
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);
},
),
]
),
),
),
Extracted List Tile Content:
class ReusableOptionTile extends StatelessWidget {
const ReusableOptionTile(
{required this.icon, required this.textData, required this.onTouch});
final IconData icon;
final String textData;
final Function onTouch;
@override
Widget build(BuildContext context) {
return ListTile(
title: Row(
children: [
Icon(icon),
SizedBox(
width: 5.0,
),
Text('$textData'),
],
),
onTap: onTouch, // ERROR POINT
);
}
}
I Extracted the List Tile from Drawer for making the code more reusable but now unable to pass the on tap function for different tile. at error point it is show error "The argument type 'Function' can't be assigned to the parameter type 'void Function()?'."
How to fix this issue?
Upvotes: 0
Views: 4488
Reputation: 863
onTouch
should be extracted as a void function
void onTouch(){
print('Home Clicked');
Navigator.pop(context);
}
then
ReusableOptionTile(
textData: 'Home',
icon: Icons.home,
onTouch: onTouch,
),
and called
ListTile(
title: Row(
children: [
Icon(icon),
SizedBox(
width: 5.0,
),
Text('$textData'),
],
),
onTap: onTouch(),
);
Upvotes: 2