Search code examples
flutterontap

OnTap flutter function


I'm new to flutter, I've understood the basics pretty fast but I have this simple and dumb problem that I can't get over with. In my app, I have tried to link other pages with the button on my nav menu, so let's say I created a nav menu, and I have a button called settings, and for my settings menu I want to create a separate settings.dart other than main.dart so I can start with a clean code page (works better for me I think), and I want to link the button from the main.dart with the page from settings.dart.

I know it's dumb and simple but I just can't get it done - thanks!

ListTile(
            leading: Icon(Icons.person),
            title: Text('Cont'),
            onTap: () => null  ,
          ),
          ListTile(
            leading: Icon(Icons.favorite),
            title: Text('Favorite'),
            onTap: () => null,
          ),
          ListTile(
            leading: Icon(Icons.share),
            title: Text('Distribuie'),
            onTap: () => null,
          ),
          ListTile(
            leading: Icon(Icons.notifications),
            title: Text('Notificari'),
            onTap: () => null,
          ),
          Divider(),
          ListTile(
            leading: Icon(Icons.settings),
            title: Text('Setari'),
            onTap: () => null,
          ),
          ListTile(
            leading: Icon(Icons.description),
            title: Text('Policies'),
            onTap: () => null,
          ),
          Divider(),
          ListTile(
            title: Text('Exit'),
            leading: Icon(Icons.exit_to_app),
            onTap: () => null,
          ),

I searched and I discovered that many other say that onPressed() is better for this, but I've tried to do this but I just can't get it to work.

After the onPressed I tried to link the name with .dart in the function but nothing happened.

enter image description here

enter image description here


Solution

  • Read up on Flutter navigation basics

    Example:

      ListTile(
          leading: const Icon(Icons.settings),
          title: const Text('Setari'),
          onTap: () => Navigator.of(context).push(
            MaterialPageRoute(
              builder: (context) => SettingsPage(),
            ),
          ),
        );