Home > Back-end >  Call method from onPressed of an IconButton
Call method from onPressed of an IconButton

Time:11-30

I have an IconButton and in the onPressed() method I want to call a void method that receives an incremental number as a parameter when the Button is clicked, then when obtaining this value it must call another void which calculates the days of weeks.

I have tested the function and if I put the values ​​in hard it does work, however when I put it in the widget it always returns the same value and does not change, no matter how much I put the setState method, I don't know how to increase said value.


Here I attach part of the code:



import 'package:intl/intl.dart'; import 'package:flutter/material.dart';

class ExampleScreen extends StatefulWidget {
   
   final String? text1;
   final String? text2;
   final String? text3;
   

  const ExampleScreen({Key? key, 
  this.text1, 
  this.text2, 
  this.text3}
  ) : super(key: key);


  @override
  State<ExampleScreen> createState() => _ExampleScreenState();
}
class _ExampleScreenState extends State<ExampleScreen> {
int counter = 1;

  void increase(){
    counter  ;
    setState(() {
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
    body: Padding(
      padding: const EdgeInsets.fromLTRB(10,0,0,0),
      child: Center(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
                              _calendar(widget.text1,widget.text2,widget.text3,counter)
              ],
            ),
          //),
        ),
    )
    );
  }
}

_calendar(text1,text2,text3, int counter) => Container(
               width: 250,
               height: 50,
               margin: const EdgeInsets.fromLTRB(50,0,0,0),
           alignment: Alignment.center,
           child: Wrap(
              children:[
                  Container(
                      height: 225,
                      width: 225,
 
                              child: Column(
                                children:  [
                                  const SizedBox(height: 10,),
                              Row(

                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children:  [
                                    IconButton(
                                      onPressed: () { 
                                       //When I call the function increase it tells me that the function is not declared

                                              calculation_days(counter); 
                                              print('amount :  $counter');
                                              
                                              }, 
                                      icon:const Icon(Icons.arrow_back_ios_new_outlined,size: 10)
                                      ),
                                    const Text('Title',style: TextStyle(fontSize: 12)),
                                    IconButton(
                                      onPressed: () {}, 
                                      icon:const Icon(Icons.arrow_forward_ios_outlined,size: 10)
                                      ),

                                    ],
                                ),
                               ]
                              ),
                          
                    ),       
                      
                  ]
           )
        );

void calculation_days(int week)
{
  int firstday;
  List<String> days;
  List<String> daysxWeek;
  days=findPreviousSaturdays1(count: week);
   
  firstday= int.parse(days[week-1]);
  daysxWeek=[firstday.toString()];
  daysxWeek.add(firstday.toString());
}

CodePudding user response:

since you declare it inside the StatefulWidget, not the State, you need to access it inside your onPressed method like this:

widget.increase()

 IconButton(
   onPressed: () {  
widget.increase(); // call it like this.
}, 
icon:const Icon(Icons.arrow_back_ios_new_outlined,size: 10)
);

Another solution is that you can move your increase() from StatefulWidget to the State class, then you can use it by calling increase() instead of widget.increase().

  • Related