为自定义小部件创建事件(如点击)

时间:2019-07-20 12:38:42

标签: flutter

我正在创建使用RaisedButton的自定义窗口小部件 我想将onTap的事件RaisedButton导出到父窗口小部件。我该怎么办?

1 个答案:

答案 0 :(得分:3)

// your custom widget
class MyCustomWidget extends StatefulWidget {
  final VoidCallback onTap;

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

  @override
  _MyCustomWidgetState createState() => _MyCustomWidgetState();
}

class _MyCustomWidgetState extends State<MyCustomWidget> {
  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      onPressed: widget.onTap, // this is called in child 
    );
  }
}

在父级中使用它,如:

MyCustomWidget(
  onTap: () {}, // when this is tapped in parent
);