1

I just got calling methods from a child class working with help from someone in this thread.

What I am trying to do now, and I am not sure if it is different, is call a method in one child from another child of the same parent.

So visually:

Parent class
  - Method()
      ^
      |
  Child class

In the above, I can easily access the Parent class method from the child class using the callback function in the link provided above.

This does not appear to work in the below structure, and I can't figure this out from any of the threads I have read on calling methods from other classes:

      Parent class
      |           |
Child class 1    Child class 2 
     - Method()  <-- callback

Is the procedure for this structure handled differently? Is it possible or can you only ever callback to a parent method?

Bisclavret
  • 1,327
  • 9
  • 37
  • 65

2 Answers2

0

Although I think, in flutter, it would be better to use state changes to update/trigger calls on UI widgets, but for your speciffic case, the delegate pattern can work. Here's an example of I would do it.

abstract class TheTrigger { // you can use VoidCallback or whatever, this is just for the demo
  void triggerMe(); 
}

class ChildOneWidget extends StatelessWidget with TheTrigger {
  @override
  Widget build(BuildContext context) {
    return Container(); // add the content of the child one
  }

  @override
  void triggerMe() {
    // TODO: implement triggerMe 
  }
}

class ChildTwoWidget extends StatelessWidget {
  final TheTrigger trigger;

  const ChildTwoWidget({Key key, this.trigger}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      //something here that will trigger "the trigger"
      child: RaisedButton(onPressed: () {
        trigger?.triggerMe(); // you should use the "?" this will allow a bit more customisation on your widgets, you might want to use it without listener.
      }),
    );
  }
}

class ParrentWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    ChildOneWidget childOneWidget = ChildOneWidget();
    ChildTwoWidget childTwoWidget = ChildTwoWidget(trigger: childOneWidget); // here you set the "delegate"

    return ListView(
      children: <Widget>[childOneWidget, childTwoWidget], 
    );
  }
}

One more time, this is just a dumb example on how you can do it, but I would strongly recommend to use state to trigger changes on children, you'll have a more flexible widget tree.

danypata
  • 9,895
  • 1
  • 31
  • 44
  • Thanks for the reply @danypata. I can't get this working. The last part where you call the widgets tells me: "The argument type 'Type' can't be assigned to the parameter type 'TheTrigger'. – Bisclavret Mar 05 '19 at 01:38
0

The way flutter widgets work is that the children widgets in your case are in the parents widget tree. On the callback of Child class 2 you can use setState to rebuild your parent and thus rebuild any of its children, for example by changing the value of a parameter in Child class 1

Hussein Abdallah
  • 1,452
  • 11
  • 13