title: How to test Cupertino Picker in Flutter
body: <p>I'm writing widget testing the Cupertino Picker for the different values chosen by the use. I can't find any good tutorial. I followed this <a href="https://github.com/flutter/flutter/blob/master/packages/flutter/test/cupertino/picker_test.dart" rel="nofollow noreferrer">https://github.com/flutter/flutter/blob/master/packages/flutter/test/cupertino/picker_test.dart</a> but this won't work for my case. In my case when the user chooses the value from the <code>picker</code> the test case should check whether the user chooses the correct value or default value. </p>
  
  <p>Cupertino Picker code : </p>
  
  <pre><code>List&lt;String&gt; ages1 = ["-- select --"];
  
  List&lt;String&gt; ages2 = List&lt;String&gt;.generate(
      45, (int index) =&gt; (21 + index).toString(),
      growable: false);
  
  List&lt;String&gt; ages = [ages1, ages2].expand((f) =&gt; f).toList();
  picker.dart:
    Widget _buildAgePicker(BuildContext context) {
      final FixedExtentScrollController scrollController =
          FixedExtentScrollController(initialItem: _selectedAgeIndex);
  
      return GestureDetector(
        key: Key("Age Picker"),
        onTap: () async {
          await showCupertinoModalPopup&lt;void&gt;(
            context: context,
            builder: (BuildContext context) {
              return _buildBottomPicker(
                CupertinoPicker(
                  key: Key("Age picker"),
                  scrollController: scrollController,
                  itemExtent: dropDownPickerItemHeight,
                  backgroundColor: Theme.of(context).canvasColor,
                  onSelectedItemChanged: (int index) {
                    setState(() {
                      _selectedAgeIndex = index;
                      ageValue = ages[index];
                      if (ageValue == S.of(context).pickerDefaultValue) {
                        ageDividerColor = Theme.of(context).errorColor;
                        errorText = S.of(context).pickerErrorMessage;
                        ageDividerWidth = 1.2;
                      } else {
                        ageDividerColor = Colors.black87;
                        errorText = "";
                        ageDividerWidth = 0.4;
                      }
                    });
                  },
                  children: List&lt;Widget&gt;.generate(ages.length, (int index) {
                    return Center(
                      child: Text(ages[index]),
                    );
                  }),
                ),
              );
            },
          );
        },
        child: _buildMenu(
          &lt;Widget&gt;[
            Text(
              S.of(context).Age,
              style: TextStyle(fontSize: 17.0),
            ),
            Text(
              ages[_selectedAgeIndex],
            ),
          ],
        ),
      );
    }
   Widget _buildMenu(List&lt;Widget&gt; children) {
      return Container(
        decoration: BoxDecoration(
          color: Theme.of(context).canvasColor,
        ),
        height: 44.0,
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16.0),
          child: SafeArea(
            top: false,
            bottom: false,
            child: DefaultTextStyle(
              style: const TextStyle(
                color: Colors.black,
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: children,
              ),
            ),
          ),
        ),
      );
    }
  
    Widget _buildBottomPicker(Widget picker) {
      return Container(
        height: dropDownPickerSheetHeight,
        padding: const EdgeInsets.only(top: 6.0),
        color: Theme.of(context).canvasColor,
        child: DefaultTextStyle(
          style: const TextStyle(
            color: Colors.black,
            fontSize: 22.0,
          ),
          child: GestureDetector(
            key: Key("picker"),
            onTap: () {},
            child: SafeArea(
              top: false,
              child: picker,
            ),
          ),
        ),
      );
    }
  </code></pre>
  
  <p>test code :</p>
  
  <pre><code>testWidgets("picker test",(WidgetTester tester)async{
     await tester.tap(find.byKey(Key("Age Picker")));
  
     await tester.drag(find.byKey(Key("Age Picker")), Offset(0.0,70.0));
  
     await tester.pumpAndSettle();
  
     expect(ages[1], "21");
  });
  </code></pre>