toggle method

Future<void> toggle(
  1. String id, {
  2. TestStep? step,
  3. int? timeoutMs,
  4. Future<void> onBeforeToggle()?,
})

Implementation

Future<void> toggle(
  String id, {
  TestStep? step,
  int? timeoutMs,
  Future<void> Function()? onBeforeToggle,
}) async {
  final baseFinder = assertions.finderForId(id);
  if (baseFinder.evaluate().isEmpty) {
    await waitFor(
      id: id,
      timeoutMs: timeoutMs ?? config.defaultWaitTimeout.inMilliseconds,
    );
  }
  final finder = _interactiveFinder(baseFinder);
  _expectSingleWidget(finder, id, 'toggle');
  await tester.ensureVisible(finder);

  final control = find.descendant(
    of: finder,
    matching: find.byWidgetPredicate(
      (widget) =>
          widget is Switch || widget is CupertinoSwitch || widget is Checkbox,
    ),
  );
  await _emitEvidenceBoundary(step);
  await onBeforeToggle?.call();
  await tester.tap(control.evaluate().isNotEmpty ? control.first : finder);
  await runtime.settleAfterAction();
}