class Menu extends StatelessWidget {
const Menu({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
var color = Theme.of(context).primaryColor;
return Drawer(
width: 200,
child: Column(
children: [
Container(
height: 100,
alignment: Alignment.bottomCenter,
child: Text(
'MENU',
style: TextStyle(
fontSize: 20,
color: color,
),
),
),
Divider(
height: 20,
color: Colors.black,
),
_MenuItem(
title: 'Accounts',
color: color,
icon: Icons.account_balance,
onTap: () => onNavigate(context, '/accounts'),
),
Divider(
height: 20,
color: Colors.black,
),
_MenuItem(
title: 'Budget Items',
color: color,
icon: Icons.attach_money,
onTap: () => onNavigate(context, '/items'),
),
Divider(
height: 20,
color: Colors.black,
),
_MenuItem(
title: 'Types',
color: color,
icon: Icons.widgets,
onTap: () => onNavigate(context, '/types'),
),
Divider(
height: 20,
color: Colors.black,
),
],
),
);
}
void onNavigate(BuildContext context, String uri) {
Navigator.of(context).pop();
Navigator.of(context).pushNamed(uri);
}
//This function serves two purposes: it pops the menu off the navigation stack. That way, when we come back to the home screen, the menu is not showing. It also pushes the new name route onto the stack.
}
class _MenuItem extends StatelessWidget {
const _MenuItem({
Key? key,
required this.color,
required this.title,
required this.icon,
required this.onTap,
}) : super(key: key);
final String title;
final Color color;
final IconData icon;
final Function onTap;
@override
Widget build(BuildContext context) {
return InkWell(
//Inkwell widget allow us to add a touch event handler to the child widget
onTap: onTap, //gives error on this line.
child: Opacity(
opacity: 0.6,
child: Container(
height: 70,
alignment: Alignment.center,
child: Column(
children: [
Icon(
icon,
color: color,
size: 50,
),
Text(
title,
style: TextStyle(
color: color,
fontSize: 14.0,
fontWeight: FontWeight.w500,
),
),
],
),
),
),
);
}
}
请帮助。我在Inkwell小部件主体中收到此错误,在onTap属性中,我创建了一个最终函数变量OnTap,将其设为必需并创建了一个函数OnNavigate,所有菜单项在点击时都将调用该函数。但是我收到了这个错误。我尝试将onTap更改为OnTap,onTap但不起作用。
OnNavigate函数有两个目的:它将菜单从导航堆栈中弹出。这样,当我们回到主屏幕时,菜单就不会显示。它还将新名称路由推送到堆栈上。
替换最终函数onTap;
用
final VoidCallback onTap;
请在类_MenuItem
内将这个最终的Function onTap;
更改为最终的Function()onTap;