Skip to content

no object passed when an event occur in any flutter widget  #50634

Description

@lionviskame

i have noticed that there is no object (widget in current state) passed when an event occur in any flutter widget .
if we take this example

class _FavoriteWidgetState extends State<FavoriteWidget> {
  // ···
  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          padding: EdgeInsets.all(0),
          child: IconButton(
            icon: (_isFavorited ? Icon(Icons.star) : Icon(Icons.star_border)),
            color: Colors.red[500],
           onPressed: (){//some work}
          ),
        ),
        SizedBox(
          width: 18,
          child: Container(
            child: Text('$_favoriteCount'),
          ),
        ),
      ],
    );
  }
}

we notice "onPressed" within "IconButton" widget doesn't have a function signature that allow the object(widget in the current state) to be passed as argument like java(native android app) or c#(winforms) did , that would be hell for dynamic widget generation or some generic logic. it would be very nice if you take this as a new feature or fix . thx

Activity

  1. added
    frameworkflutter/packages/flutter repository. See also f: labels.
    c: proposalA detailed proposal for a change to Flutter
    on Feb 12, 2020
  2. goderbauer commented on Feb 12, 2020

    @goderbauer
    Member

    Not sure why you need it, but you could just call another function from the onPressed handler and pass it the current widget state (this):

            Container(
              padding: EdgeInsets.all(0),
              child: IconButton(
                icon: (_isFavorited ? Icon(Icons.star) : Icon(Icons.star_border)),
                color: Colors.red[500],
               onPressed: (){
                 yourFunction(this); // <--
               }
              ),
            ),

    Does that work for you? What more would you need? And for what use case?

  3. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Feb 12, 2020
  4. lionviskame commented on Feb 13, 2020

    @lionviskame
    Author

    thx for the response , i try that but the this return the parent object of Containerwhich is in our case
    _FavoriteWidgetState ;

  5. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Feb 13, 2020
  6. goderbauer commented on Feb 14, 2020

    @goderbauer
    Member

    You can pass whatever you want in there. It doesn't have to be this.

    Also, why do you need this? What is the use case?

  7. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Feb 14, 2020
  8. lionviskame commented on Feb 14, 2020

    @lionviskame
    Author

    You can pass whatever you want in there. It doesn't have to be this.
    the problem is not this , what i realy want is some thing similar to this:

    onPressed: (IconButton object){
                //some code
              }
    

    why do you need this? What is the use case?
    this example is not my case i just copy it from flutter documentation for demonstration
    my case is the folowing

    Container(
              padding: EdgeInsets.all(32),
             child:  Column(
            children: <Widget>[
              for (Category i in allCategories )
             SwitchListTile(
               key: Key(i.id.toString()),
              activeColor: Colors.amber,
               activeTrackColor: Colors.red,
               inactiveTrackColor: Colors.blueAccent,
               title: Text(i.titre,style: TextStyle(color: Colors.white),),
              value: _value,
              onChanged:(bool value){
                // some code
              },
               ),
            ],
          ),
        )
    

    i generate a dynamic widget list base on a object List (allCategories), and each widget in the widget List has a key which i need it to do my logic in the onChanged method , what i want is some thing similar to this .

    onChanged:(SwitchListTile value){
                // some code
              },
    
  9. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Feb 14, 2020
  10. zggoogle commented on Feb 14, 2020

    @zggoogle

    Thanks for your question lionviskame , I have the same problem ..

  11. changed the title [-]missing important feature[/-] [+]no object passed when an event occur in any flutter widget [/+] on Jul 31, 2020
  12. goderbauer commented on Oct 4, 2022

    @goderbauer
    Member

    I am going to close this as there are ways around this as described in my previous comments.

  13. github-actions commented on Oct 18, 2022

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  14. locked as resolved and limited conversation to collaborators on Oct 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterframeworkflutter/packages/flutter repository. See also f: labels.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions