Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何根据颤振中的参数指定不同的特性值?_Flutter_Dart_Text_Styles - Fatal编程技术网

Flutter 如何根据颤振中的参数指定不同的特性值?

Flutter 如何根据颤振中的参数指定不同的特性值?,flutter,dart,text,styles,Flutter,Dart,Text,Styles,我有一个扩展Textstyle的自定义类。 我想根据传递给类的参数更改“背景色”属性 假设我为背景色传递参数Yes,它将向文本显示背景色,否则不会 代码如下: class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Column(

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

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

代码如下:



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


在您的文本样式中,您可以使用以下sytax:

color: yes?Colors.blue:Colors.transparent

“是”应该是文本样式中的布尔值。您可以使用以下sytax:

color: yes?Colors.blue:Colors.transparent

“是”应该是一个布尔值

您可以这样实现:

class CtrBlblStyle extends TextStyle {
  final Color backgroundColor;
  CtrBlblStyle({
    String parameter,
  }) : this.backgroundColor =
            parameter == 'Y' ? CtrPublic.backgroundColor : Colors.transparent;
}

您可以这样实现:

class CtrBlblStyle extends TextStyle {
  final Color backgroundColor;
  CtrBlblStyle({
    String parameter,
  }) : this.backgroundColor =
            parameter == 'Y' ? CtrPublic.backgroundColor : Colors.transparent;
}