Flutter 如何根据颤振中的参数指定不同的特性值?
我有一个扩展Textstyle的自定义类。 我想根据传递给类的参数更改“背景色”属性 假设我为背景色传递参数Yes,它将向文本显示背景色,否则不会 代码如下: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(
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;
}