0

I have two images in flutter which is use for pin and unpin the data in list view so my program is that when i click on pin image than unpin image should be hide and when i click unpin image than pin image should hide. so how to implement this thing in flutter.

here is my demo code

class PinUnpinData extends StatefulWidget {
  @override
  _PinUnpinDataState createState() => _PinUnpinDataState();
}

class _PinUnpinDataState extends State<PinUnpinData> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: Text(
          "Pin-Unpin",
          style: TextStyle(fontSize: 20, color: Colors.white),
        ),
      ),
      backgroundColor: Colors.white,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            InkWell(
                onTap: () {
                  
                },
                child: Padding(
                  padding: const EdgeInsets.all(20),
                  child: Image.asset(
                    "assets/images/pin.png",
                    height: 20,
                    width: 20,
                  ),
                )),
            InkWell(
                onTap: () {},
                child: Padding(
                  padding: const EdgeInsets.all(20),
                  child: Image.asset(
                    "assets/images/unpin.png",
                    height: 20,
                    width: 20,
                  ),
                ))
          ],
        ),
      ),
    );
  }
}

1 Answers1

0

Create a local variable to keep the track of pinned state. Then update the state of that variable on tap of the button using the setState() method. Also, for showing the relevant image, just check the value of thepinned variable and show the relevant image, if it's true then show the unpin image else pin image.

class _PinUnpinDataState extends State<PinUnpinData> {

  bool pinned = false; // to keep track if it's pinned or not

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: Text(
          "Pin-Unpin",
          style: TextStyle(fontSize: 20, color: Colors.white),
        ),
      ),
      backgroundColor: Colors.white,
      body: Center(
        child: InkWell(
            onTap: () {
              setState(() {
                pinned = pinned ? false : true; // check if pinned is true, if its true then set it false and voice versa
              });
            },
            child: Padding(
              padding: const EdgeInsets.all(20),
              child: Image.asset(
                pinned
                    ? "assets/images/unpin.png" //show this image when it's pinned
                    : "assets/images/pin.png", // show this image when it not pinned
                height: 20,
                width: 20,
              ),
            )),
      ),
    );
  }
}
OMi Shah
  • 5,768
  • 3
  • 25
  • 34