I have 3 containers and in each picture, when you click on which an icon appears. How to assign a value to each container, so that when you click it, you can change it too. Or how to track which container was clicked on
class _EditAccountScreenState extends State<EditAccountScreen> {
bool checkboxValue = false;
...
child: GestureDetector(
onTap: () {
setState(() {
checkboxValue = !checkboxValue;
});
},
child: Padding(
child: Row(
children: <Widget> [
Container(
child: Stack(
children: <Widget>[
Image.asset('assets/images/telegram-512.png',fit: BoxFit.fill),
Positioned(
bottom: 0, right: 15, //give the values according to your requirement
child: checkboxValue
? Container(
decoration: BoxDecoration(
color: Colors.green,
borderRadius:BorderRadius.circular(100)
),
child: Icon(
Icons.check,
color: Colors.white,
size: 15,
)
)
: Container(),),],),),
Container(
child: Stack(
children: <Widget>[
Image.asset('assets/images/Viber-Logo.png',fit: BoxFit.fill),
Positioned(
bottom: 0, right: 15, //give the values according to your requirement
child: checkboxValue
? Container(
decoration: BoxDecoration(
color: Colors.green,
borderRadius:BorderRadius.circular(100)
),
child: Icon(
Icons.check,
color: Colors.white,
size: 15,
)
)
: Container(),),],),),
2
Answers
Use Gesture Detector to detect Taps on containers.
Here is an example related to this:
First of All Make a
Integer
Variable and then set its value to-1
by defaultNow, You Can
Wrap
each of your Container in seperateGestureDetector
, and then on theonTap
method you can set theselectedContainerIndex
value
of yourcontainer
according to you.See the below
Code
Now To know which
Container
was tapped you can always use theselectedContainerIndex
ValueNOTE: If your
containers
are more in numbers say 4 or 5, then I would recommend you to use good practice and show those using somedynamic
listview builder
, instead of hardcoding them each.