Deepak Lohmod
Deepak Lohmod

Reputation: 2272

How can i swipe the elements in a grid view

I am trying to make a game. In which i have a matrix which have 9 elements in it i.e 3X3 matrix. As you can see in this picture so can anyone tell me how can implement swipe of 2 elements in it. For example of user wants to swipe 4 and 6 then he should simply swipe his finger over it to do so.

matrix

My code for Matrix:-

GridView.count(
            crossAxisCount: 3,
            shrinkWrap: true,
            children: List.generate(array.length, (index) => Container(
              decoration: BoxDecoration(
                  border: Border.all(color: Colors.white)
              ),
              child: Center(child: Text(array[index].toString(),style: TextStyle(color: Colors.white,fontSize: 20.0,fontWeight: FontWeight.w500),)),
            )),
          ),

Upvotes: 0

Views: 1212

Answers (1)

Tasnuva Tavasum oshin
Tasnuva Tavasum oshin

Reputation: 4740

wrap your widget in a dismissible.. this worked well in my case because it has an interactive animation, allowing a clear way for user to abort

    GridView.count(
        crossAxisCount: 3,
        shrinkWrap: true,
        children: List.generate(array.length, (index) => 

Dismissible(
            key: UniqueKey(),
            child:Container(
          decoration: BoxDecoration(
              border: Border.all(color: Colors.white)
          ),
          child: Center(child: Text(array[index].toString(),style: TextStyle(color: Colors.white,fontSize: 20.0,fontWeight: FontWeight.w500),)),
        )),
      ), //the widget you want the swipe to be detected on
            direction: DismissDirection.up, // or whatever
            confirmDismiss: (direction) {
              if (direction == DismissDirection.up) { // or other directions
                // Swiped up do your thing.
              }
              return Future.value(false); // always deny the actual dismiss, else it will expect the widget to be removed
            })

Upvotes: 2

Related Questions