Search code examples
flutterflutter-test

Selecting value in Slider with Widget Test


I have a screen that contains a series of Sliders where the user can select if they agree/disagree with something, for example:

Slider(
  key: Key(questionId),
  min: 0,
  max: 5,
  divisions: 5,
  value: _value,
  onChanged: (value) {
     setState(() {
       _value = value;
     });
  },
),

I am currently implementing a UI test using testWidgets, in which the user selects multiple values in Sliders and then get stored when the user clicks on a button, and I'd like to be able to select a value, for example, to swipe the slider to value '2'.

How can I do that?

The only idea I have is to use tapAt with the exact pixel coordinates where the value is in the slider but seems extremely complicated to do.

For clarification I am asking about Widget Testing as described in: https://flutter.dev/docs/cookbook/testing/widget/introduction


Solution

  • It's probably no longer needed, but since this was the only related question I found when looking for the answer myself, here is my solution:

    WidgetTester has the functions 'drag/dragFrom/dragUntilVisible':

    await tester.drag(find.byType(Slider), Offset(100, 0));
    

    This will drag the Slider :)