首页 > 解决方案 > 在 Flutter 中创建一个扩展另一个自定义类的类

问题描述

我创建了扩展StatelessWidget的类Blabel。我想将它扩展到Blabel1,保留它的所有属性并为其添加一些额外的属性

假设我想向textDirection新类添加属性。我该怎么做?

这是Blabel我拥有的课程代码:

class Blabel extends StatelessWidget {
  final String text;
  final TextStyle style;
  Blabel({
    this.text,
    this.style,
  });
  @override
  Widget build(BuildContext context) {
    return Text(
      text,
      style: style ?? CtrBlblStyle(),
    );
  }
}

标签: flutterclasstext

解决方案


我认为这是您正在寻找的东西:

class Blabel1 extends Blabel {
  final TextDirection textDirection; // example of new property
  Blabel1({
    String text,
    TextStyle style,
    this.textDirection,
  }) : super(text: text, style: style);
  @override
  Widget build(BuildContext context) {
    // You can change the build method to change the UI from Blabe
    return Text(
      text,
      style: style ?? CtrBlblStyle(),
    );
  }
}

推荐阅读