Flutter: Is there an onhold option instead of oppressed once to repeat SetState

Issue

I want a button which I can hold down and then Setstate is repeating, unless I stop holding it.

I am a beginner and working at my first app. I want to calculate volume. People have to put in height, width and dept. I don’t want in text(form)field, because the keyboard is necessary. I found a solution with two buttons, an add and a minus button.

I have it working, but people have to push the button very often to set the value of height they want.

I thought instead of oppressed, I use something as on hold and the counter is adding quickly. But I don’t know the solution.

RawMaterialButton(
child: Icon(MdiIcons.minus),
onPressed: () {
setState(() {
width--;

Solution

Ok, I didn’t fix it. It was to complex for me as a beginner.
I found another solution.
I made a row, with three children:
a minus button and a plus button for fine tuning
a slider-bar for the big steps.

This is the code I used, do you have better solutions, let me know. I just place it for other beginners like me.
I want to thank special @Abbas.M who tried to help me.

Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    AnimatedOpacity(
                      opacity: height == 1 ? 0.0 : 1.0,
                      duration: Duration(milliseconds: 500),
                      child: RawMaterialButton(
                        child: Icon(MdiIcons.minus),
                        onPressed: () {
                          setState(() {
                            height > 1 ? height-- : height = height;
                          });
                        },
                        constraints: BoxConstraints.tightFor(
                          width: 25.0,
                          height: 25.0,
                        ),
                        shape: CircleBorder(),
                        fillColor: white,
                      ),
                    ),
                    SliderTheme(
                      data: SliderTheme.of(context).copyWith(
                        inactiveTrackColor: cAppBottomBar,
                        activeTrackColor: white,
                        thumbColor: white,
                        overlayColor: cShadow,
                        thumbShape:
                            RoundSliderThumbShape(enabledThumbRadius: 10.0),
                        overlayShape:
                            RoundSliderOverlayShape(overlayRadius: 17.0),
                      ),
                      child: Slider(
                          value: height.toDouble(),
                          min: 1,
                          max: 300,
                          onChanged: (value) {
                            setState(() {
                              height = value.toInt();
                            });
                          }),
                    ),
                    AnimatedOpacity(
                      opacity: height == 300 ? 0.0 : 1.0,
                      duration: Duration(milliseconds: 500),
                      child: RawMaterialButton(
                        child: Icon(Icons.add),
                        onPressed: () {
                          setState(() {
                            height < 300 ? height++ : height = height;
                          });
                        },
                        constraints: BoxConstraints.tightFor(
                          width: 25.0,
                          height: 25.0,
                        ),
                        shape: CircleBorder(),
                        fillColor: white,
                      ),
                    ),

Answered By – Margriet

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.