1

我正在测试Cupertino picker,为此我正在编写小部件测试,并且我正在努力实现onselectItemChanged测试中的方式以及相应地通过轮式选择的值cupertino picker。必须进行测试,以便用户选择相应的值时,测试用例必须以适合所选不同值的方式编写

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,
              ),
            ),
          ),
        );
      }

小部件_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");
});
4

0 回答 0