[英]How can you make a widget inherit from another widget? - Flutter
I want to be able to inherit from let's say a base IconButton that has a few properties. 我希望能够从具有一些属性的基本IconButton继承。 The buttons that inherit from this class should have access to the base class properties without overriding them.
从此类继承的按钮应该可以访问基类属性,而不会覆盖它们。
class BaseButton extends StatelessWidget {
final IconData icon;
final double size;
final Color color;
final Function onPressed;
const BaseButton({
Key key,
this.icon,
this.size = 30,
this.color = Colors.white,
this.onPressed,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return IconButton(
onPressed: onPressed,
icon: Icon(icon),
iconSize: size,
color: color,
);
}
}
I would like to use this base class to generate widgets with little hassle: 我想使用这个基类来生成具有很少麻烦的小部件:
class AddButton extends BaseButton {
@override
Widget build(BuildContext context) {
return BaseButton(icon: Icons.add);
}
}
class CancelButton extends BaseButton {
@override
Widget build(BuildContext context) {
return BaseButton(icon: Icons.close);
}
}
And I want to be able to easily access the properties of the inherited base class. 而且我希望能够轻松访问继承的基类的属性。
Think you want something like: 想想您想要的东西:
class AddButton extends BaseButton {
const AddButton({
Key key,
double size,
Color color,
this.onPressed,
}) : super(key: key, icon: Icons.add, size: size, color: color, onPressed: onPressed);
}
class CancelButton extends BaseButton {
const CancelButton({
Key key,
double size,
Color color,
this.onPressed,
}) : super(key: key, icon: Icons.close, size: size, color: color, onPressed: onPressed);
}
No need to override build in AddButton or CancelButton. 无需覆盖AddButton或CancelButton中的构建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.