How to call parent widget function from child widget in Flutter

Viewed 8070

I have a Parent StatefulWidget and Child StatefulWidget. I want to call a function of parent widget i.e. goToPreviousItem from the child widget.

Parent Class

class Parent extends StatefulWidget {
 @override
 _ParentState createState() => _ParentState();
 }

class _ParentState extends State<Parent> {
 String item = 0;
 @override
 Widget build(BuildContext context) {
   return ChildClass();
 }


goToPreviousItem(value){
 setState(() {item = value});
 }
}

Child Class

class Child extends StatefulWidget {
 @override
 _ChildState createState() => _ChildState();
 }

class _ChildState extends State<Child> {
 @override
 Widget build(BuildContext context) {
   return Container(
  child: FlatButton(
    color: Color(0XFFEFEFEF),
    textColor: primaryColor,
    disabledColor: Colors.grey,
    disabledTextColor: Colors.black,
    padding: EdgeInsets.symmetric(
        vertical: 15.0, horizontal: 10.0),
    onPressed: () => goToPreviousItem(2),
    child: Text(
      "Update",
    ),
  ),
);
   }
 }

How can I do that?

2 Answers
  1. Create a Function type parameter inside your child widget.

    class Child extends StatefulWidget {
    
     Function function;
     Child({this.function});
    
     @override
    _ChildState createState() => _ChildState();
    }
    
    class _ChildState extends State<Child> {
    @override
    Widget build(BuildContext context) {
     return Container(
    child: FlatButton(
     color: Color(0XFFEFEFEF),
     textColor: primaryColor,
     disabledColor: Colors.grey,
     disabledTextColor: Colors.black,
     padding: EdgeInsets.symmetric(
         vertical: 15.0, horizontal: 10.0),
     onPressed: () => widget.function(2),
     child: Text(
       "Update",
             ),
          ),
        );
      }
    }
    
  2. Pass the function goToPreviousItem as a parameter from the parent widget to child widget.

    Child(function: goToPreviousItem);
    
  3. Access your function using widget.function(2) inside the child widget.

typedef parentFunctionCallback = void Function(String value);

class Parent extends StatefulWidget {
  @override
  _ParentState createState() => _ParentState();
}

class _ParentState extends State<Parent> {
  String item = 'data';
  
  @override
  Widget build(BuildContext context) {
    return ChildClass(goToPreviousItem);
  }
  
  goToPreviousItem(String value){
    setState(() {item = value;});
  }
}

class ChildClass extends StatefulWidget {
  const ChildClass(this.parentfunc);
  
  /// ToDo
  final parentFunctionCallback parentfunc; 
  
  @override
  _ChildState createState() => _ChildState();
}

class _ChildState extends State<ChildClass> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: FlatButton(
        color: Color(0XFFEFEFEF),
        textColor: primaryColor,
        disabledColor: Colors.grey,
        disabledTextColor: Colors.black,
        padding: EdgeInsets.symmetric(
            vertical: 15.0, horizontal: 10.0),
        onPressed: () => widget.parentfunc('mail'),
        child: Text(
          "Update",
        ),
      ),
    );
  }
}
Related