Флаттер: получение всех свойств виджета - PullRequest
0 голосов
/ 27 сентября 2019

Я хочу создать новый 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 голосов
/ 27 сентября 2019

Вы "можете" сделать это, используя конструктор хакерским способом

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
    );
  }
}

Другой пример с более чем одним параметром:

class AudioWidget extends StatelessWidget {
  final Icon child;

  AudioWidget(this.child):
  this.mySize=child.size,
  this.myColor= child.color
  ;// How do I get all of IconButton's properties?
  final double mySize;
  final Color myColor;
  @override
  Widget build(BuildContext context) {
    return IconButton(
      icon: child,
      onPressed: () {}// Some custom function
    );
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...