Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a custom class that extends Textstyle. I want to change the background color property depending on parameter that I pass the class.

Suppose I pass parameter Yes for background color, it will show background color to Text, otherwise not.

Here is the code:



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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
256 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can achieve it like this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...