简体   繁体   中英

flutter: how to add on tap in Expansiontile

I'm using Expansiontile in flutter and it works fine but i want to have a click action on offer to route to new page but it's not working with me as shown below i used ontap and it gives me error.however, it works with listTile where i can click on sub offer to route me to page1, is there a way to do it?

drawer: Drawer(
  child: ListView(
    padding: EdgeInsets.zero,
    children: <Widget>[
      DrawerHeader(
        child: Text('project1',
            textAlign: TextAlign.center,
            style:
                TextStyle(fontSize: 25, fontFamily: 'VampiroOne', color: Colors.grey)),
        decoration: BoxDecoration(
            image: DecorationImage(
                image: AssetImage("assets/images/drawer-back.png"),
                fit: BoxFit.cover)),
      ),
      ListTile(
        leading: Icon(Icons.home),
        title: Text('Home'),
        onTap: () {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => MyHomePage(),
            ),
          );
        },
      ),
      ExpansionTile(
          title: Text(
            "Offers",
          ),
          leading: CircleAvatar(
            backgroundImage: AssetImage("assets/images/bin.png"),
          ),
          onTap: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => MyHomePage(),
              ),
            );
          },
          children: <Widget>[
            ListTile(
                title: Text(
                  'sub offers',
                ),
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => page1(),
                    ),
                  );
                }),
          ]),
    ],
  ),
)

ExpantionTile has the onExpansionChanged which tells you when the widget is tapped.

Here is your code with the use of onExpansionChanged :

Drawer(
  child: ListView(
    padding: EdgeInsets.zero,
    children: <Widget>[
      DrawerHeader(
        child: Text('project1',
            textAlign: TextAlign.center,
            style:
            TextStyle(fontSize: 25, fontFamily: 'VampiroOne', color: Colors.grey)),
        decoration: BoxDecoration(
            image: DecorationImage(
                image: AssetImage("assets/images/drawer-back.png"),
                fit: BoxFit.cover)),
      ),
      ListTile(
        leading: Icon(Icons.home),
        title: Text('Home'),
        onTap: () {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => MyHomePage(),
            ),
          );
        },
      ),
      ExpansionTile(
        onExpansionChanged: (bool isExpanded) {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => MyHomePage(),
            ),
          );
        },
        title: Text(
          "Offers",
        ),
        leading: CircleAvatar(
          backgroundImage: AssetImage("assets/images/bin.png"),
        ),
        children: <Widget>[
          ListTile(
            title: Text(
              'sub offers',
            ),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => page1(),
                ),
              );
            },
          ),
        ],
      ),
    ],
  ),
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM