3

i have this function that creates the menu item but it has no functionality, i want is so that i can send it to what page i want when declaring it. How can i do this?

final List<MenuItem> options = [
MenuItem(Icons.home, 'Home'),
MenuItem(Icons.person, 'Profile'),
MenuItem(Icons.web_asset, 'Website'),
MenuItem(Icons.settings, 'Settings'),

];

             children: options.map((item) {
            
            return ListTile(
              
              onTap: () {
            Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => HERE I WANT TO PUT A CUSTOM PAGE FOR EACH ONE()),
                );
          },
              leading: Icon(
                item.icon,
                color: Colors.white,
                size: 35,
              ),
              title: Text(
                item.title,
                style: TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.bold,
                    color: Colors.white),
              ),
            );
          }).to
Andrei Marin
  • 616
  • 1
  • 6
  • 16

1 Answers1

4

Add Function OnTap; to your model like below :

class MenuItem{
  Icon icon;
  String title;
  Function OnTap;

  MenuItem(this.icon, this.title, this.OnTap);
}

and next add new objects;

final List<MenuItem> options = [
MenuItem(Icons.home, 'Home',(){print("define_onclick_here");}),
MenuItem(Icons.person, 'Profile',(){print("define_onclick_here");}),
MenuItem(Icons.web_asset, 'Website',(){print("define_onclick_here");}),
MenuItem(Icons.settings, 'Settings',(){print("define_onclick_here");}),];

and finally add item.OnTap(); in list item onTap:

  children: options.map((item) {
        
        return ListTile(
          
          onTap: () {
        item.OnTap();
      },
          leading: Icon(
            item.icon,
            color: Colors.white,
            size: 35,
          ),
          title: Text(
            item.title,
            style: TextStyle(
                fontSize: 14,
                fontWeight: FontWeight.bold,
                color: Colors.white),
          ),
        );
      }).to
Milad jalali
  • 622
  • 9
  • 10
  • one thing. I add this to the define_onclick_here ("""Navigator.push( context, MaterialPageRoute(builder: (context) => Profile()), );""") and nothing happens. What should i add there? – Andrei Marin Oct 12 '20 at 06:23
  • 1
    @AndreiMarin use (){ Navigator.push( context, MaterialPageRoute(builder: (context) => Profile())); } – Milad jalali Oct 12 '20 at 12:46
  • Do you have any idea why i get the error 'unidentified name context' ? https://gyazo.com/ff7c46856e2bf15b7d60910dccfb2e87 – Andrei Marin Oct 12 '20 at 16:53
  • @AndreiMarin may be you don't access context in this file, please read below page: https://stackoverflow.com/questions/52962112/how-to-navigate-without-context-in-flutter-app – Milad jalali Oct 14 '20 at 11:35