Flutter抛出“无效的双重”当我更改值时错误

发布于 2025-02-01 12:42:46 字数 1272 浏览 2 评论 0原文

我正在尝试从Textfield获取值,并在滑块中使用该值。我已经实现了自己想要的东西,但是当我更改文本字段的价值时,我会遇到错误。

错误:

The following FormatException was thrown while dispatching notifications for TextEditingController:
Invalid double

代码:

  double minValue = 0.0;
  double maxValue = 25000.0;
  double _availableValue = 12450.0;

  void _setAmountValue() {
    print('Amount: ${double.parse(amountController.text)}');
    if (
        double.parse(amountController.text).roundToDouble() >= minValue &&
        double.parse(amountController.text).roundToDouble() <= maxValue
        ) {
      setState(() {
        _availableValue = double.parse(amountController.text).roundToDouble();
      });
    }
  }

TextField(
controller: amountController,
focusNode: amountFocus,
style: TextStyle(color: Color(0xff757575), fontSize: 15, fontFamily: 'Gilroy Medium'),
decoration: InputDecoration(hintText: "12,450.00",border: InputBorder.none,),maxLines: 1,),


Slider(
     value: availableValue.toDouble(),
     min: 0.0,
     max: 25000.0,
     onChanged: (double newValue) {
        setState(() {
          availableValue = newValue.toInt();
          // amountController.text = value.toString();
        });
      },
),

请在此问题上帮助我。提前致谢。

I am trying to get the value from the Textfield and use that value in the slider. I have achieved what i want but i am getting error when i change the value of the textfield.

Error:

The following FormatException was thrown while dispatching notifications for TextEditingController:
Invalid double

Code:

  double minValue = 0.0;
  double maxValue = 25000.0;
  double _availableValue = 12450.0;

  void _setAmountValue() {
    print('Amount: ${double.parse(amountController.text)}');
    if (
        double.parse(amountController.text).roundToDouble() >= minValue &&
        double.parse(amountController.text).roundToDouble() <= maxValue
        ) {
      setState(() {
        _availableValue = double.parse(amountController.text).roundToDouble();
      });
    }
  }

TextField(
controller: amountController,
focusNode: amountFocus,
style: TextStyle(color: Color(0xff757575), fontSize: 15, fontFamily: 'Gilroy Medium'),
decoration: InputDecoration(hintText: "12,450.00",border: InputBorder.none,),maxLines: 1,),


Slider(
     value: availableValue.toDouble(),
     min: 0.0,
     max: 25000.0,
     onChanged: (double newValue) {
        setState(() {
          availableValue = newValue.toInt();
          // amountController.text = value.toString();
        });
      },
),

Please help me on this issue. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野生奥特曼 2025-02-08 12:42:46

使用文本控制器从滑块设置值

void initState()
{
  super.initState();
  _sliderTextController.text=_sliderValue.toString();
}
double _sliderValue=50.0;

return Row(children: [
                  Slider(
                    min:0,
                    max:100,
                    value:_sliderValue,
                    onChanged: (value) => {
                     
                      setState((){_sliderValue=value;_sliderTextController.text=value.toString();})
                  },),
                  SizedBox(width:100,child:
                  TextFormField(controller: _sliderTextController,))
                ],);

use the text controller to set the value from the slider

void initState()
{
  super.initState();
  _sliderTextController.text=_sliderValue.toString();
}
double _sliderValue=50.0;

return Row(children: [
                  Slider(
                    min:0,
                    max:100,
                    value:_sliderValue,
                    onChanged: (value) => {
                     
                      setState((){_sliderValue=value;_sliderTextController.text=value.toString();})
                  },),
                  SizedBox(width:100,child:
                  TextFormField(controller: _sliderTextController,))
                ],);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文