0
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,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

kindly help. I'm receiving this error in Inkwell widget body, in onTap property, I created a final function variable OnTap, made it required and created a function OnNavigate that all menu items will call when tapped. But I'm getting this error. I've tried to change onTap to OnTap,ontap but doesn't works.

OnNavigate 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.

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56

2 Answers2

1

replace final Function onTap; with

 final VoidCallback onTap;
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • it works, what does it do? I am new to dart. also final Function() onTap worked for me too. – Jack Sparrow Aug 16 '22 at 05:46
  • onTap doesn't return anything, it is described as `void onTap` and `funftion` is a abstract class , you can do like `void Function()? onTap;` You can check the duplicate for more details – Md. Yeasin Sheikh Aug 16 '22 at 05:54
1

Please change this final Function onTap; to final Function() onTap; inside class _MenuItem

Hardik Mehta
  • 2,195
  • 1
  • 11
  • 14