Home > Net >  Flutter throws an "Invalid double" error when i change the value
Flutter throws an "Invalid double" error when i change the value

Time:05-27

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.

CodePudding user response:

use the text controller to set the value from the slider

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,))
                ],);

CodePudding user response:

i have updated my code

  Widget build(BuildContext context) {
    return dialogBox1();
  }

Widget dialogBox1() => AlertDialog(
        shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.all(
            Radius.circular(
              20.0,
            ),
          ),
        ),
        contentPadding: EdgeInsets.all(0),
        content: Container(
          decoration: const BoxDecoration(
              gradient: LinearGradient(colors: [Color(0xffFFE4C8), Colors.white], begin: Alignment.topCenter, end: Alignment.center),
              borderRadius: BorderRadius.all(Radius.circular(20))),
          height: 300,
          width: MediaQuery.of(context).size.width,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              SizedBox(
                height: 20,
              ),
              Padding(
                padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      'Amount',
                      style: TextStyle(fontSize: 15, fontFamily: 'Gilroy Medium'),
                    ),
                    Row(
                      children: [
                        Text(
                          '\u{20B9} ',
                          style: TextStyle(color: Color(0xff757575), fontSize: 15, fontFamily: 'Gilroy Medium'),
                        ),
                        Container(
                            width: 100,
                            child: TextField(
                              keyboardType: TextInputType.number,
                              controller: amountController,
                              focusNode: amountFocus,
                              style: TextStyle(color: Color(0xff757575), fontSize: 15, fontFamily: 'Gilroy Medium'),
                              decoration: InputDecoration(
                                hintText: _availableValue.toString(),
                                border: InputBorder.none,
                              ),
                              maxLines: 1,
                            )),
                        InkWell(
                          child: SvgPicture.asset(editIcon),
                          onTap: () {
                            amountFocus.requestFocus();
                          },
                        )
                      ],
                    )
                  ],
                ),
              ),
              SliderTheme(
                data: SliderThemeData(
                  thumbShape: SliderThumbShape(inColor: 0xffFFBE78),
                ),
                child: Slider(
                  value: _availableValue,
                  min: minValue,
                  max: maxValue,
                  onChanged: (double newValue) {
                    setState(() {
                      value = newValue.toInt();
                      amountController.text = value.toString();
                    });
                  },
                  activeColor: Color(0xffFFBE78),
                  inactiveColor: Color(0xfff5e0c5),
                ),
              ),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 20.0),
                child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: const [
                  Text(
                    "0",
                    style: TextStyle(fontFamily: 'Gilroy Light', fontSize: 15, color: Color(0xff7d7d7d)),
                  ),
                  Text(
                    "25K",
                    style: TextStyle(fontFamily: 'Gilroy Light', fontSize: 15, color: Color(0xff7d7d7d)),
                  ),
                ]),
              ),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 20),
                child: InkWell(
                  onTap: () {
                  },
                  child: Container(
                    decoration: BoxDecoration(color: Colors.black, borderRadius: BorderRadius.all(Radius.circular(40))),
                    child: Padding(
                      padding: const EdgeInsets.all(10.0),
                      child: const Center(
                          child: Text(
                        'Save',
                        style: TextStyle(fontSize: 15, fontFamily: 'Gilroy Medium', color: Colors.white),
                      )),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      );

  • Related