Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

text - How to assign different property values depending on parameters in Flutter?

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


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

1 Answer

0 votes
by (71.8m points)

You can achieve it like this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...