Home > OS >  How to pass a key to a function?
How to pass a key to a function?

Time:10-08

I have several buttons, images to which I pass through the Map collection. When clicking on the button, I would like to receive the transmitted image key and write it to a variable.

I do not know how to correctly get the key from the button when you click on it, could you tell me? icon: Image(image: AssetImage(imagesIcon[1].toString())),

My function:

_clikFunction (){
   int keyNumber;
  keyNumber = imagesIcon[value];
  }

My collection:

  final imagesIcon  = <int, String>{
    1: IconImages.test1,
    2: IconImages.test2,
    3: IconImages.test3,
  }; 

My button:

Column(         
                    children: [
                      Container(
                        color: Colors.lightBlueAccent,
                        child: IconButton(
                                            icon:  Image(image: AssetImage(imagesIcon[1].toString())),
                                            onPressed: _clikFunction,
                                            ),
                      ),
                      SizedBox(height: 40,),
                      Container(
                        color: Colors.lightBlueAccent,
                        child: IconButton(  
                                            icon:  Image(image: AssetImage(imagesIcon[2].toString())),
                                            onPressed: _clikFunction,),
                      ),
                      SizedBox(height: 40,),
                      Container(
                        color: Colors.lightBlueAccent,
                        child: IconButton(
                                            icon:  Image(image: AssetImage(imagesIcon[3].toString())),
                                            onPressed: _clikFunction,), 
                      ),          
                    ],
                  ),

CodePudding user response:

Column(         
     children: [
                      Container(
                        color: Colors.lightBlueAccent,
                        child: IconButton(
                                            icon:  Image(image: AssetImage(imagesIcon[1].toString())),
                                            onPressed: _clikFunction(1),
                                            ),
                      ),
                      SizedBox(height: 40,),
                      Container(
                        color: Colors.lightBlueAccent,
                        child: IconButton(  
                                            icon:  Image(image: AssetImage(imagesIcon[2].toString())),
                                            onPressed: _clikFunction(2),),
                      ),
                      SizedBox(height: 40,),
                      Container(
                        color: Colors.lightBlueAccent,
                        child: IconButton(
                                            icon:  Image(image: AssetImage(imagesIcon[3].toString())),
                                            onPressed: _clikFunction(3),), 
                      ),          
                    ],
                  ),








_clikFunction (int key){

  imagesIcon[] = key;
  }

CodePudding user response:

I got the desired result in this way

_clikFunction (int key){
 int keyNumber;
  keyNumber = key;

 print(keyNumber);
}


Column(         
     children: [
                      Container(
                        color: Colors.lightBlueAccent,
                        child: IconButton(
                                            icon:  Image(image: AssetImage(imagesIcon[1].toString())),
                                            onPressed: () =>_keyFunction(1)),,
                      ),
                      SizedBox(height: 40,),
                      Container(
                        color: Colors.lightBlueAccent,
                        child: IconButton(  
                                            icon:  Image(image: AssetImage(imagesIcon[2].toString())),
                                            () =>_keyFunction(2)),

                      SizedBox(height: 40,),
                      Container(
                        color: Colors.lightBlueAccent,
                        child: IconButton(
                                            icon:  Image(image: AssetImage(imagesIcon[3].toString())),
                                           () =>_keyFunction(3)),
                    ],
                  ),
  • Related