Ask Your Question
2

How can the dismissible animation be applied to a GridView in Flutter?

asked 2023-07-01 20:50:13 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-07-01 20:55:02 +0000

lalupa gravatar image

The dismissible animation can be applied to a GridView in Flutter by wrapping each grid item in a Dismissible widget. Here's an example:

GridView.builder(
  itemCount: myList.length,
  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
    crossAxisCount: 2,
    mainAxisSpacing: 10.0,
    crossAxisSpacing: 10.0,
    childAspectRatio: 0.7,
  ),
  itemBuilder: (BuildContext context, int index) {
    return Dismissible(
      key: UniqueKey(),
      direction: DismissDirection.horizontal,
      onDismissed: (direction) {
        setState(() {
          myList.removeAt(index);
        });
      },
      child: GridTile(
        child: Image.asset(
          myList[index].image,
          fit: BoxFit.cover,
        ),
        footer: GridTileBar(
          backgroundColor: Colors.black45,
          title: Text(
            myList[index].name,
            style: TextStyle(fontSize: 16.0),
          ),
        ),
      ),
    );
  },
);

In this example, we're using a GridView.builder to build our grid dynamically. Each grid item is wrapped in a Dismissible widget with a UniqueKey for reusability. We've also specified the direction of the dismiss action as horizontal with the onDismissed method updating the list and rebuilding the grid view. This method removes the dismissed item from the list and updates the UI.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-01 20:50:13 +0000

Seen: 9 times

Last updated: Jul 01 '23