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

标签 flutter user-input

值得注意的是,我对 flutter 还很陌生,所以很多工作都是基于教程或视频...... 如果问题看起来很明显,我们深表歉意。

//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

关于flutter - 如何通过在 Flutter 中使用 onChanged 方法的 TextField 从用户获取整数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69190087/

相关文章:

dart - 如何在 flutter dart 中将 RevealedOffset 转换为数字

asynchronous - 如何使用异步函数异步监听 Firestore 中的值?

flutter - 向 Flutter 小部件添加一个空的 const 构造函数

image - 如何让 frame Builder 和 loadingBuilder 在 Image.network 中协同工作?

python - 将 imshow 与用户输入相结合

python - 如何将 url 作为用户输入传递以通过 Selenium 和 Python 进行调用?

flutter - 将 Flutter 动画直接渲染到视频

javascript - 需要制作网络表单来保存任何更改,即使不点击 "SEND"

php - HTML 输入只读安全风险?

ruby - 在 Ruby 中,如何在用户输入后在同一行打印下一个提示?