Variables 一种三变量方法
希望有人能帮助我。我希望你明白我的意思,因为我很难解释这个问题 我有 整数高度 整数宽度 整数深度 带有一个minusButton-slider-plusButton heightTextfield的行 widthTextfield中带有一个minusButton-slider-plusButton的行 depthTextfield中带有一个minusButton-slider-plusButton的行 所有的代码都写出来了,所以我经常重复我自己!唯一的区别是var是高度、宽度和深度。 这些变量显示在文本字段中 下面是我为减号按钮所做的操作,但它不起作用Variables 一种三变量方法,variables,input,flutter,methods,dart,Variables,Input,Flutter,Methods,Dart,希望有人能帮助我。我希望你明白我的意思,因为我很难解释这个问题 我有 整数高度 整数宽度 整数深度 带有一个minusButton-slider-plusButton heightTextfield的行 widthTextfield中带有一个minusButton-slider-plusButton的行 depthTextfield中带有一个minusButton-slider-plusButton的行 所有的代码都写出来了,所以我经常重复我自己!唯一的区别是var是高度、宽度和深度。 这些变量
AnimatedOpacity minusButton(int heightWidthDepth) {
return AnimatedOpacity(
opacity: heightWidthDepth == 1 ? 0.0 : 1.0,
duration: Duration(milliseconds: 500),
child: RawMaterialButton(
elevation: 6.0,
child: Icon(
MdiIcons.minus,
size: 15,
),
onPressed: () {
setState(() {
heightWidthDepth > 1
? heightWidthDepth--
: heightWidthDepth = heightWidthDepth;
calculate();
});
},
constraints: buttonSize,
shape: CircleBorder(),
fillColor: white,
),
);
}
//In each row I have instead of all the code:
minusButton(height),
minusButton(width),
minusButton(depth),
我认为heightWidthDepth将被输入变量height替换,而变量height将从heightWidthDepth获得值,但事实并非如此
我觉得有一个非常简单的解决方案,但我不明白
将
函数
作为参数传递,而不是整数
。在您的State
类中,我认为您可以在小部件中使用三个变量。因此:
int height = ..;
int width = ..;
int depth = ..;
AnimatedOpacity minusButton(Function onPressed) {
return AnimatedOpacity(
opacity: height == 1 ? 0.0 : 1.0,
duration: Duration(milliseconds: 500),
child: RawMaterialButton(
elevation: 6.0,
child: Icon(
Icons.remove,
size: 15,
),
onPressed: heightOnPressed,
shape: CircleBorder(),
fillColor: Colors.white,
),
);
}
void heightOnPressed() {
setState(() {
height > 1
? height--
: height = height;
});
print('$height');
}
不是逐个编写所有小部件,而是为每个小部件创建onPressed
函数,这些小部件操作相关的状态
变量
例如:
Row(children: <Widget>[
minusButton(heightOnPressed),
minusButton(widthOnPressed),
minusButton(depthOnPressed),
])
行(子项:[
最小按钮(高度ON按下),
最小按钮(宽度按下),
最小按钮(深度未按下),
])
因此,您有三个可变变量,打算与按钮交互,并且您试图将这三个变量用于每个按钮作为参数。但这不是你想要的。我理解正确了吗?为什么不用int 0,1再传递一个参数,2@Esen-mehmet,这是正确的,我添加了一个链接到我的网站是一个解释的应用程序,希望有意义。它可以工作,但是有很多重复的代码。@AmitPrajapati我不明白你在说什么。很抱歉可变高度是一个显示为文本字段的值。是的,非常感谢您的解决方案。我刚刚试过,而且很有效。我在这里等了这么久才开口,为此挣扎了这么久。你让我以一种完全不同的方式思考。谢谢只有一个问题。我有一个文件formules.dart,其中有所有这些函数。当我将此函数移到此页面时,系统不知道设置状态。我需要进口什么东西吗?谢谢欢迎@Margriet,我很乐意帮忙<代码>设置状态在状态
类和“StatefulBuilder”小部件中可用。所以,它在空白省道文件中不起作用。您还需要导入material.dart
文件,仅此而已。您应该将这些方法和变量保存在State
类中。