Melvin Abraham
Melvin Abraham

Reputation: 3036

Flutter: Change the parameter passed to a "StatefulWidget"

I have a StatefulWidget named BounceContainer. This class contains a parameter child like a typical Flutter widget. Basically, it bounces the child whenever the user taps on it. The way I pass the child parameter as of now is as follow:

class BounceContainer extends StatefulWidget {
    final Widget child;                 // Declaring "child"
    BounceContainer({this.child});      // Initializing "child"

    // Passing "child" to State

    @override
    _BounceContainerState createState() => _BounceContainerState(this.child);
}

class _BounceContainerState extends State<BounceContainer> {
    Widget child;
    _BounceContainerState(this.child);  // Receiving "child"

    @override 
    Widget build(BuildContext context) {
        ...
    }
}

The problem, in this case, is that even if the child parameter changes, the child itself does not update.

For example, if I have a button whose color changes from grey to any random color based on AnimationController and I pass this button as child parameter to BounceContainer class, the button still remains grey and calling setState() (either from the main program or BounceContainer class) also does not force update the child widget.

What's the proper way (and also efficient way) to work around this problem?

Upvotes: 4

Views: 5900

Answers (1)

siega
siega

Reputation: 2868

You don't need to pass all your parameters to State class. You can access them with widget.child

Upvotes: 4

Related Questions