动态设置颜色时出现死代码警告
我正在尝试使按钮在按下后变成灰色。我阅读了如何执行此操作的信息。我能找到的最好方法是使用三元运算符设置材质颜色,然后更改 setState(() {})
块中的条件:
Container vehicleButton(IconData icon_, String text_, {required Function onClickAction}){
const double buttonSizeX = 200;
const double buttonSizeY = 100;
const double iconSize = 60;
const double buttonTextSize = 15;
const double buttonMargin = 5;
Color buttonColor = const Color(0xff2196f3);
const Color iconColor = Color(0xffffffff);
const Color buttonTextColor = Color(0xffffffff);
bool pressed = false;
return Container(padding: const EdgeInsets.only(left: buttonMargin, right: buttonMargin, top: buttonMargin, bottom: buttonMargin), child: SizedBox.fromSize(
size: Size(buttonSizeX, buttonSizeY), child: Material(color: pressed ? Color.fromARGB(255, 143, 143, 143) : buttonColor, child: InkWell(
onTap: () {
setState(() {
pressed = true;
});
onClickAction();
},
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
Icon(icon_, color: iconColor, size: iconSize),
Text(text_, style: TextStyle(fontSize: buttonTextSize, color: buttonTextColor)),
])))));
}
但是,我收到一条警告: ? 之后的代码在我的三元运算符中是死代码。事实上,按钮按下后不会变成灰色。
我想也许 Material 的值是最终的并且无法更改,但这并不能解释为什么我在互联网上找到的所有示例都使用这种方法。
I'm trying to make buttons that turn grey after having been pressed. I read up on how to do this. The best way I could find is to set the material color with a ternary operator and then change the condition for that in a setState(() {})
block:
Container vehicleButton(IconData icon_, String text_, {required Function onClickAction}){
const double buttonSizeX = 200;
const double buttonSizeY = 100;
const double iconSize = 60;
const double buttonTextSize = 15;
const double buttonMargin = 5;
Color buttonColor = const Color(0xff2196f3);
const Color iconColor = Color(0xffffffff);
const Color buttonTextColor = Color(0xffffffff);
bool pressed = false;
return Container(padding: const EdgeInsets.only(left: buttonMargin, right: buttonMargin, top: buttonMargin, bottom: buttonMargin), child: SizedBox.fromSize(
size: Size(buttonSizeX, buttonSizeY), child: Material(color: pressed ? Color.fromARGB(255, 143, 143, 143) : buttonColor, child: InkWell(
onTap: () {
setState(() {
pressed = true;
});
onClickAction();
},
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
Icon(icon_, color: iconColor, size: iconSize),
Text(text_, style: TextStyle(fontSize: buttonTextSize, color: buttonTextColor)),
])))));
}
However, I'm getting a warning that the code after the ? in my ternary operator is dead code. And indeed, the button does not turn grey after pressing.
I thought maybe the values of Material are final and cannot be changed, but this wouldn't explain why all the examples I could find on the internet use this method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您有:
Dart 分析工具正确报告
Color.fromARGB(...)
是死代码,因为条件三元运算符检查 本地 的状态Pressed
变量在检查时始终false
。尽管onTap
处理程序设置pressed = true
,但它设置的是本地pressed
变量的状态,该变量永远不会被再次读取。您可能希望将
pressed
设为包含vehicleButton
方法的任何State
类的成员变量。You have:
The Dart analysis tools are correctly reporting that
Color.fromARGB(...)
is dead code because the conditional ternary operator checks the state of the localpressed
variable, which at the time it's checked, is alwaysfalse
. Although theonTap
handler setspressed = true
, it's setting the state of the localpressed
variable, which will never be read again.You likely intend for
pressed
to be a member variable of whateverState
class contains thevehicleButton
method.我建议您不要使用 SizedBox,而是使用 flutter dev 提供的按钮: https:// /docs.flutter.dev/release/writing-changes/buttons
您可以进行的一个简单尝试是:
Instead of using a SizedBox I suggest you to use the buttons provided by flutter dev: https://docs.flutter.dev/release/breaking-changes/buttons
A simple try you can give is:
请记住,您必须定义变量 build(BuildContext context)
keep in mind that you have to define your variable build(BuildContext context)