Flutter:获取小部件的所有属性

时间:2019-09-27 15:16:48

标签: flutter dart properties widget

我想创建一个新的Widget来包裹IconButton并覆盖其onPressed属性,但是我希望这个新的Widget接受所有IconButton的其他属性。

在React Native中,我可以将一个组件的propsTypes设置为另一个组件的propsTypes

以下是我需要的示例:

class AudioWidget extends StatelessWidget {
  final Icon child;

  AudioWidget(this.child); // How do I get all of IconButton's properties?

  @override
  Widget build(BuildContext context) {
    return IconButton(
      icon: child,
      onPressed: () => play(context), // Some custom function
    );
  }
}

1 个答案:

答案 0 :(得分:0)

您可以通过使用骇客的方式使用构造函数来做到这一点

.navbar {
    z-index: 9 !important;
}

另一个具有多个参数的示例是

class AudioWidget extends StatelessWidget {
  final Icon child;

  AudioWidget(this.child):mySize=child.size; // In the contructor now we are getting the size and assignin it to OUR size
  final double mySize;
  @override
  Widget build(BuildContext context) {
    return IconButton(
      icon: child,
      onPressed: () {}// Some custom function
    );
  }
}