Parth Godhani
Parth Godhani

Reputation: 239

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

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?

Upvotes: 8

Views: 2588

Answers (1)

CopsOnRoad
CopsOnRoad

Reputation: 268314

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: () {},
)

Upvotes: 16

Related Questions