如何通过在Flutter中使用onChanged方法的TextField从用户获取整数值



值得注意的是,我对扑动很陌生,所以很多工作都是基于教程或视频…如果这个问题看起来很明显,我道歉。

//Build Length Box.
Widget buildLength() => buildHeader(
header: 'House Length: ',
child: Row(
children: [
// I want to be able to get user input(has to be an integer value for calculations further in the program)
// from this child: Text(), At the moment I am only able to get the input from the slider...
//I want to be able to use the slider or the text next to it.
//The first child widget should preferably work with the same onChangedLength method the slider uses...
Expanded(
flex: 1,    
child: Text(
length.toString(),
style: TextStyle(color: Colors.white),
),
),
Expanded(
flex: 9,
child: Slider( //TODO: Decide on the correct parameters for the slider, min/max/etc...
label: length.toString(),
value: (length ?? 0).toDouble(),
min: 0,
max: 100,
divisions: 100,
onChanged: (length) => onChangedLength(length.toInt()),
),
),
],
),
);

这里也提到了onChanged方法。

onChangedLength: (length) => setState(() => this.editLength = length),

我正在忙着使用onChanged的教程,但是如果这不起作用,我可以使用其他方法。

将修改后的长度值设置为editLength。但你使用length.toString()来显示,如果你声明的变量是int editLength

editLength.toString() // Text Widget
label: length.toString(), // Slider Widget
value: (editLength ?? 0).toDouble() // Slider Widget

或如果你声明的变量是int长度

onChanged: (newLength) => onChangedLength(newLength.toInt()), // Slider widget
onChangedLength(newLength) => setState(() => this.length = newLength); // onChangedLength function

最新更新