【发布时间】:2022-10-18 19:57:12
【问题描述】:
如果我想在颤动中创建提升按钮,如果条件为真,则背景为绿色,如果为假,则为蓝色
我可以使用带有提升按钮的 if 条件吗?
【问题讨论】:
-
风格:是真的吗? buttonStyle 1 : 按钮样式 2
标签: flutter
如果我想在颤动中创建提升按钮,如果条件为真,则背景为绿色,如果为假,则为蓝色
我可以使用带有提升按钮的 if 条件吗?
【问题讨论】:
标签: flutter
如果你想使用ButtonStyle:
ElevatedButton(
onPressed: () {},
child: Text('button'),
style: ButtonStyle(
backgroundColor:MaterialStateProperty.all( yourCondetion ? Colors.green : Colors.blue,),
),
),
如果你想使用ElevatedButton.styleFrom:
ElevatedButton(
onPressed: () {},
child: Text('button'),
style: ElevatedButton.styleFrom(
primary: yourCondetion ? Colors.green : Colors.blue,
),
),
【讨论】:
使用styleFrom 设置样式高架按钮:
ElevatedButton(
child: Text('Button'),
onPressed: () {},
style: ElevatedButton.styleFrom({
(condition) ? Colors.green : Colors.red,
}),
),
【讨论】:
应该是这样的:
ElevatedButton(
child: Text('My Button')
onPress: () {}
style: ElevatedButton.styleFrom(
primary: isTrue? Colors.green : Colors.blue
)
【讨论】:
您可以创建一个类型为ButtonStyle 的变量。我们经常使用包含每个按钮样式的文件。
例如:文件button_style.dart:
var btnStyleGreen = ButtonStyle(color: green)
var btnStyleBlue = ButtonStyle(color: blue)
您的工作文件:
ButtonStyle yourBtnStyle;
if (isBlue) yourBtnStyle = btnStyleGreen; else yourBtnStyle = btnStyleGreen;
ElevatedButton(
child: Text('Button'),
onPress: () {},
style: yourBtnStyle,
)
大型产品通常就是这样。
【讨论】:
ElevatedButton(
onPressed: () {},
child: Text('button'),
style: ButtonStyle(
backgroundColor:MaterialStateProperty.all( yourCondetion ? Colors.green : Colors.blue,),
),
)
【讨论】: