Flutter - widget testing for the Cupertino picker in flutter/Dart

1k views Asked by At

i am testing the Cupertino picker,for which i am writing widget testing and i am struggling to implement how the onselectItemChanged in testing and accordingly the values chosen by the wheeling of the cupertino picker. The testing has to be done so that the value when user chooses accordingly the test case has to written in such a way that it suits to different values chosen

List<String> ages1 = ["-- select --"];

List<String> ages2 = List<String>.generate(
        45, (int index) => (21 + index).toString(),
        growable: false);

    List<String> ages = [ages1, ages2].expand((f) => f).toList();
    picker.dart:
      Widget _buildAgePicker(BuildContext context) {
        final FixedExtentScrollController scrollController =
            FixedExtentScrollController(initialItem: _selectedAgeIndex);

        return GestureDetector(
          key: Key("Age Picker"),
          onTap: () async {
            await showCupertinoModalPopup<void>(
              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<Widget>.generate(ages.length, (int index) {
                      return Center(
                        child: Text(ages[index]),
                      );
                    }),
                  ),
                );
              },
            );
          },
          child: _buildMenu(
            <Widget>[
              Text(
                S.of(context).Age,
                style: TextStyle(fontSize: 17.0),
              ),
              Text(
                ages[_selectedAgeIndex],
              ),
            ],
          ),
        );
      }
     Widget _buildMenu(List<Widget> 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,
              ),
            ),
          ),
        );
      }

widget_test.dart:

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");
});
0

There are 0 answers