Skip to content

[Widget test] Make IndexedStack hide non-active children from finder #111478

@simolus3

Description

@simolus3

Use case

An IndexedStack only ever shows one of its children. Other children aren't rendered or hit-tested. In this sense, I think these other children could be considered off-stage.

However, a finder in widget tests will still find every children:

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('should only find current widget', (WidgetTester tester) async {
    // Only the `1` text widget will be shown
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: IndexedStack(
        index: 1,
        children: [for (var i = 0 ; i < 10;i++) Text('$i')],
      ),
    ));

    expect(find.text('1'), findsOneWidget);
    expect(find.text('2'), findsNothing); // This currently fails, I believe it should pass
    expect(find.text('2', skipOffstage: false), findsOneWidget); // This should continue to pass
  });
}

Proposal

I believe it would be more intuitive for IndexedStack to override debugVisitOnstageChildren so that it only reports the currently-active child.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projecta: tests"flutter test", flutter_test, or one of our testsc: proposalA detailed proposal for a change to Flutterframeworkflutter/packages/flutter repository. See also f: labels.r: fixedIssue is closed as already fixed in a newer version

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions