0

我有一个扩展 Textstyle 的自定义类。我想根据传递给类的参数更改背景颜色属性。

假设我为背景颜色传递参数 Yes,它将向 Text 显示背景颜色,否则不会。

这是代码:



class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          children: [
            Text(
              "aaaaaaaaaaaaaaa",
              style: CtrBlblStyle(PARAMETER:"Y"),
            )
          ],
        ),
      ),
    );
  }
}

class CtrPublic {
  static const Color backgroundColor = Colors.green;
}

class CtrBlblStyle extends TextStyle {
  final backgroundColor;
  CtrBlblStyle({

**//if(parameter='Y'{
//    this.backgroundColor = CtrPublic.backgroundColor,}
//endif**

//    this.backgroundColor = CtrPublic.backgroundColor,
  });
}

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

4

2 回答 2

2

你可以像这样实现它:

class CtrBlblStyle extends TextStyle {
  final Color backgroundColor;
  CtrBlblStyle({
    String parameter,
  }) : this.backgroundColor =
            parameter == 'Y' ? CtrPublic.backgroundColor : Colors.transparent;
}
于 2021-01-11T02:34:13.123 回答
0

在您的 TextStyle 中,您可以使用以下语法:

color: yes?Colors.blue:Colors.transparent

“是”应该是一个布尔值

于 2021-01-11T02:27:22.453 回答