Home > Net >  How do I get the value from slider in the textbox in Flutter?
How do I get the value from slider in the textbox in Flutter?

Time:03-17

I have a slider which is taking input from the user, I want to get that input in the textfield as the value of the slider keeps changing. Following is the image of the slider and the textfield. enter image description here

Code =>

SliderTheme(
          data: SliderThemeData(
            trackHeight: 8,
             ...
             ...
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Padding(
                padding: EdgeInsets.fromLTRB(0, 50, 0, 20),
                child: Text(
                  'Enter the amount you wish to add',
                 
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 28.0),
                child: SizedBox(
                  child: Row(
                    children: [
                      buildSideLabel(sliderMinValue),
                      Expanded(
                        child: Slider(
                          value: sliderAmount,
                          min: sliderMinValue,
                          max: sliderMaxValue,
                          divisions: 50000,
                          label: sliderAmount.round().toString(),
                          onChanged: (sliderAmount) => setState(
                              () => this.sliderAmount = sliderAmount),
                        ),
                      ),
                      buildSideLabel(sliderMaxValue),
                    ],
                  ),
                ),
              ),
              Padding(
                padding: EdgeInsets.only(top: 25),
                child: Container(
                  child: TextField(
                    onChanged: (value) {
                      setState(() {
                        amount = value;
                      });
                    },
                    keyboardType: TextInputType.number,
                    inputFormatters: [
                      LengthLimitingTextInputFormatter(7),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),

What should I do to get the slider value in the text field?

CodePudding user response:

Here you can get slider value in your textfield, First create TeztEditingController

TextEditingController valueTextController = TextEditingController();

Then on change of slider perfome the following action

Center(
        child: Column(
          children: <Widget>[
            TextField(
               controller: valueTextController,
            ),
            Expanded(
              child: Slider(
                  value: 5,
                  min: 0,
                  max: 10,
                  divisions: 50000,
                  onChanged: (sliderAmount) {
                    print(sliderAmount);
                    setState(
                            () => valueTextController.text = sliderAmount.toString());
                  }
              ),
            ),
          ],
        ),
      ),

And you will get the updated value everytime on changing slider value

CodePudding user response:

Try below code hope its help to you. Refer image

  • Related