Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a event (like on tap) for a Custom widget

Tags:

flutter

I am creating my custom widget in which I am using RaisedButton I want to export onTap event from RaisedButton to parent widget. How can I do that?

like image 455
Parth Godhani Avatar asked Dec 23 '22 22:12

Parth Godhani


1 Answers

Create a custom widget:

class MyCustomWidget extends StatelessWidget {
  final String text;
  final VoidCallback? onTap;

  const MyCustomWidget(this.text, {
    Key? key,
    this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: onTap,
      child: Text(text),
    );
  }
}

Usage:

MyCustomWidget(
  'My button',
  onTap: () {},
)
like image 70
CopsOnRoad Avatar answered Jan 30 '23 07:01

CopsOnRoad