Home > database >  How to propagate onTap to child function in Flutter
How to propagate onTap to child function in Flutter

Time:05-31

I have parent widget with button and I want to call function in child widget which is quite complex so I don't have to move it in parent. My idea was to add somekind of listener in child but I am not sure whats the best way. Another idea is to pass valueNotifier and listen every change in child and call function which I want. I know this is wrong on many levels but I think it's worth of questioning.

EDIT simplified with example code:

class ParentWidget extends StatelessWidget {
  const ParentWidget({ Key? key }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          child: TextButton(
            onPressed: () {
              // call child's _showDialog()
            },
            child: Text("show dialog"),
          )
        ),
      ],
    );
  }
}

class ChildWidget extends StatelessWidget {
  const ChildWidget({ Key? key }) : super(key: key);

  _showDialog() {
    // .....
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text("This is child"),
    );
  }
}

CodePudding user response:

On the child widget you write a function like this:

class ChildWidget extends StatelessWidget {
  childFunc(BuildContext context) {
    print('Do Something');
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        // child body
        );
  }
}

In the parent you can call the child function like this:

 onPressed: () {
            ChildWidget().childFunc(context);
          },

CodePudding user response:

Try below code

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: ParentWidget(),
        ),
      ),
    );
  }
}

class ParentWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return TextButton(
      child: Text('Show Dialog'),
      onPressed: () {
        ChildWidget().showDialogBox(context);
      },
    );
  }
}

class ChildWidget extends StatelessWidget {
  const ChildWidget({Key? key}) : super(key: key);

  showDialogBox(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text("Alert Dialog title"),
          content: Text("Alert Dialog body"),
          actions: [
            TextButton(
              child: Text("Close"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text("This is child"),
    );
  }
}

Test above code on dartpad

  • Related