-1

i want to set clickable system for navigator push in index of GridView builder. but i am not finding the way how to do it because i am new in flutter. please help me. here all source code.

 class category_route extends StatelessWidget {

 @override
 Widget build(BuildContext context) {

return MaterialApp(
  home: Scaffold(
    
    body: Container(

      child: GridView.builder(

        itemCount: categoryTitleArray.length,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: (orientation == Orientation.portrait) ? 3 : 4,
          crossAxisSpacing: 5,
          mainAxisSpacing: 5,
          childAspectRatio: (itemWidth / itemHeight),
        ),

        itemBuilder: (BuildContext context, int index) {

          return new Card(
            elevation: 0,
            color: Colors.transparent,
            child: new Column(

              children: [

                Expanded(

                  child: Container(
                    
                    child: Image.asset(
                      categoryImageArray[index],
                      fit: BoxFit.contain,

                    ), ), ),
                
                Text(
                  "${categoryTitleArray[index]}",
                 
                )
         ],), );}, ),  ), ),); } }
Emma Scarlett
  • 47
  • 2
  • 9

1 Answers1

0

Wrap your Card with gesture detector and use its onTap property

       class category_route extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Container(
          child: GridView.builder(
            itemCount: categoryTitleArray.length,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: (orientation == Orientation.portrait) ? 3 : 4,
              crossAxisSpacing: 5,
              mainAxisSpacing: 5,
              childAspectRatio: (itemWidth / itemHeight),
            ),
            itemBuilder: (BuildContext context, int index) {
              return GestureDetector(
                onTap: (){//navigator push here}
                child: new Card(
                elevation: 0,
                color: Colors.transparent,
                child: new Column(
                  children: [
                    Expanded(
                      child: Container(
                        child: Image.asset(
                          categoryImageArray[index],
                          fit: BoxFit.contain,
                        ),
                      ),
                    ),
                    Text(
                      "${categoryTitleArray[index]}",
                    )
                  ],
                ),
              );
              )
            },
          ),
        ),
      ),
    );
  }
}

You can also use InkWell instead of gestureDetector which is same but with ripple effect.

Zain Ur Rehman
  • 287
  • 3
  • 14
  • hello friend, can you help me in this new question please? https://stackoverflow.com/questions/65000099/flutter-how-to-show-next-index-after-complete-a-specific-logic-in-swiper-wher – Emma Scarlett Nov 25 '20 at 13:13