stepper_test.dart 49.5 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/foundation.dart';
6 7
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
8
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
9 10

void main() {
11
  testWidgetsWithLeakTracking('Material3 has sentence case labels', (WidgetTester tester) async {
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(useMaterial3: true),
        home: Material(
          child: Stepper(
            onStepTapped: (int i) {},
            steps: const <Step>[
              Step(
                title: Text('Step 1'),
                content: SizedBox(
                  width: 100.0,
                  height: 100.0,
                ),
              ),
              Step(
                title: Text('Step 2'),
                content: SizedBox(
                  width: 100.0,
                  height: 100.0,
                ),
              ),
            ],
          ),
        ),
      ),
    );
    expect(find.text('Continue'), findsWidgets);
    expect(find.text('Cancel'), findsWidgets);
  });

42
  testWidgetsWithLeakTracking('Stepper tap callback test', (WidgetTester tester) async {
43 44 45
    int index = 0;

    await tester.pumpWidget(
46 47 48
      MaterialApp(
        home: Material(
          child: Stepper(
49 50 51
            onStepTapped: (int i) {
              index = i;
            },
52
            steps: const <Step>[
53 54 55
              Step(
                title: Text('Step 1'),
                content: SizedBox(
56
                  width: 100.0,
57 58
                  height: 100.0,
                ),
59
              ),
60 61 62
              Step(
                title: Text('Step 2'),
                content: SizedBox(
63 64 65 66 67 68 69 70 71 72 73 74 75
                  width: 100.0,
                  height: 100.0,
                ),
              ),
            ],
          ),
        ),
      ),
    );
    await tester.tap(find.text('Step 2'));
    expect(index, 1);
  });

76
  testWidgetsWithLeakTracking('Stepper expansion test', (WidgetTester tester) async {
77
    await tester.pumpWidget(
78 79 80 81
      MaterialApp(
        home: Center(
          child: Material(
            child: Stepper(
82
              steps: const <Step>[
83 84 85
                Step(
                  title: Text('Step 1'),
                  content: SizedBox(
86 87 88 89
                    width: 100.0,
                    height: 100.0,
                  ),
                ),
90 91 92
                Step(
                  title: Text('Step 2'),
                  content: SizedBox(
93 94 95 96 97 98 99 100 101
                    width: 200.0,
                    height: 200.0,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
102 103 104 105 106 107
    );

    RenderBox box = tester.renderObject(find.byType(Stepper));
    expect(box.size.height, 332.0);

    await tester.pumpWidget(
108 109 110 111
      MaterialApp(
        home: Center(
          child: Material(
            child: Stepper(
112
              currentStep: 1,
113
              steps: const <Step>[
114 115 116
                Step(
                  title: Text('Step 1'),
                  content: SizedBox(
117 118 119 120
                    width: 100.0,
                    height: 100.0,
                  ),
                ),
121 122 123
                Step(
                  title: Text('Step 2'),
                  content: SizedBox(
124 125 126 127 128 129 130 131 132
                    width: 200.0,
                    height: 200.0,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
133 134 135 136 137 138 139 140 141 142
    );

    await tester.pump(const Duration(milliseconds: 100));
    box = tester.renderObject(find.byType(Stepper));
    expect(box.size.height, greaterThan(332.0));
    await tester.pump(const Duration(milliseconds: 100));
    box = tester.renderObject(find.byType(Stepper));
    expect(box.size.height, 432.0);
  });

143
  testWidgetsWithLeakTracking('Stepper horizontal size test', (WidgetTester tester) async {
144
    await tester.pumpWidget(
145 146 147 148
      MaterialApp(
        home: Center(
          child: Material(
            child: Stepper(
149
              type: StepperType.horizontal,
150
              steps: const <Step>[
151 152 153
                Step(
                  title: Text('Step 1'),
                  content: SizedBox(
154 155 156 157 158 159 160 161 162
                    width: 100.0,
                    height: 100.0,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
163 164
    );

165
    final RenderBox box = tester.renderObject(find.byType(Stepper));
166 167 168
    expect(box.size.height, 600.0);
  });

169
  testWidgetsWithLeakTracking('Stepper visibility test', (WidgetTester tester) async {
170
    await tester.pumpWidget(
171 172 173
      MaterialApp(
        home: Material(
          child: Stepper(
174
            type: StepperType.horizontal,
175
            steps: const <Step>[
176 177 178
              Step(
                title: Text('Step 1'),
                content: Text('A'),
179
              ),
180 181 182
              Step(
                title: Text('Step 2'),
                content: Text('B'),
183 184 185 186 187
              ),
            ],
          ),
        ),
      ),
188 189 190 191 192 193
    );

    expect(find.text('A'), findsOneWidget);
    expect(find.text('B'), findsNothing);

    await tester.pumpWidget(
194 195 196
      MaterialApp(
        home: Material(
          child: Stepper(
197 198
            currentStep: 1,
            type: StepperType.horizontal,
199
            steps: const <Step>[
200 201 202
              Step(
                title: Text('Step 1'),
                content: Text('A'),
203
              ),
204 205 206
              Step(
                title: Text('Step 2'),
                content: Text('B'),
207 208 209 210 211
              ),
            ],
          ),
        ),
      ),
212 213 214 215 216 217
    );

    expect(find.text('A'), findsNothing);
    expect(find.text('B'), findsOneWidget);
  });

218
  testWidgetsWithLeakTracking('Material2 - Stepper button test', (WidgetTester tester) async {
219 220 221 222
    bool continuePressed = false;
    bool cancelPressed = false;

    await tester.pumpWidget(
223
      MaterialApp(
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
        theme: ThemeData(useMaterial3: false),
        home: Material(
          child: Stepper(
            type: StepperType.horizontal,
            onStepContinue: () {
              continuePressed = true;
            },
            onStepCancel: () {
              cancelPressed = true;
            },
            steps: const <Step>[
              Step(
                title: Text('Step 1'),
                content: SizedBox(
                  width: 100.0,
                  height: 100.0,
                ),
              ),
              Step(
                title: Text('Step 2'),
                content: SizedBox(
                  width: 200.0,
                  height: 200.0,
                ),
              ),
            ],
          ),
        ),
      ),
    );

    await tester.tap(find.text('CONTINUE'));
    await tester.tap(find.text('CANCEL'));

    expect(continuePressed, isTrue);
    expect(cancelPressed, isTrue);
  });

262
  testWidgetsWithLeakTracking('Material3 - Stepper button test', (WidgetTester tester) async {
263 264 265 266 267 268
    bool continuePressed = false;
    bool cancelPressed = false;

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(useMaterial3: true),
269 270
        home: Material(
          child: Stepper(
271 272 273 274 275 276 277
            type: StepperType.horizontal,
            onStepContinue: () {
              continuePressed = true;
            },
            onStepCancel: () {
              cancelPressed = true;
            },
278
            steps: const <Step>[
279 280 281
              Step(
                title: Text('Step 1'),
                content: SizedBox(
282 283 284 285
                  width: 100.0,
                  height: 100.0,
                ),
              ),
286 287 288
              Step(
                title: Text('Step 2'),
                content: SizedBox(
289 290 291 292 293 294 295 296
                  width: 200.0,
                  height: 200.0,
                ),
              ),
            ],
          ),
        ),
      ),
297 298
    );

299 300
    await tester.tap(find.text('Continue'));
    await tester.tap(find.text('Cancel'));
301 302 303 304 305

    expect(continuePressed, isTrue);
    expect(cancelPressed, isTrue);
  });

306
  testWidgetsWithLeakTracking('Stepper disabled step test', (WidgetTester tester) async {
307 308 309
    int index = 0;

    await tester.pumpWidget(
310 311 312
      MaterialApp(
        home: Material(
          child: Stepper(
313 314 315
            onStepTapped: (int i) {
              index = i;
            },
316
            steps: const <Step>[
317 318 319
              Step(
                title: Text('Step 1'),
                content: SizedBox(
320 321 322 323
                  width: 100.0,
                  height: 100.0,
                ),
              ),
324 325
              Step(
                title: Text('Step 2'),
326
                state: StepState.disabled,
327
                content: SizedBox(
328 329 330 331 332 333 334 335
                  width: 100.0,
                  height: 100.0,
                ),
              ),
            ],
          ),
        ),
      ),
336 337 338 339 340 341
    );

    await tester.tap(find.text('Step 2'));
    expect(index, 0);
  });

342
  testWidgetsWithLeakTracking('Stepper scroll test', (WidgetTester tester) async {
343
    await tester.pumpWidget(
344 345 346
      MaterialApp(
        home: Material(
          child: Stepper(
347
            steps: const <Step>[
348 349 350
              Step(
                title: Text('Step 1'),
                content: SizedBox(
351 352 353 354
                  width: 100.0,
                  height: 300.0,
                ),
              ),
355 356 357
              Step(
                title: Text('Step 2'),
                content: SizedBox(
358 359 360 361
                  width: 100.0,
                  height: 300.0,
                ),
              ),
362 363 364
              Step(
                title: Text('Step 3'),
                content: SizedBox(
365 366 367 368 369 370 371 372
                  width: 100.0,
                  height: 100.0,
                ),
              ),
            ],
          ),
        ),
      ),
373 374
    );

375
    final ScrollableState scrollableState = tester.firstState(find.byType(Scrollable));
376
    expect(scrollableState.position.pixels, 0.0);
377 378 379

    await tester.tap(find.text('Step 3'));
    await tester.pumpWidget(
380 381 382
      MaterialApp(
        home: Material(
          child: Stepper(
383
            currentStep: 2,
384
            steps: const <Step>[
385 386 387
              Step(
                title: Text('Step 1'),
                content: SizedBox(
388
                  width: 100.0,
389 390
                  height: 300.0,
                ),
391
              ),
392 393 394
              Step(
                title: Text('Step 2'),
                content: SizedBox(
395
                  width: 100.0,
396 397 398
                  height: 300.0,
                ),
              ),
399 400 401
              Step(
                title: Text('Step 3'),
                content: SizedBox(
402 403 404 405 406 407 408 409 410 411 412 413 414 415
                  width: 100.0,
                  height: 100.0,
                ),
              ),
            ],
          ),
        ),
      ),
    );

    await tester.pump(const Duration(milliseconds: 100));
    expect(scrollableState.position.pixels, greaterThan(0.0));
  });

416
  testWidgetsWithLeakTracking('Stepper index test', (WidgetTester tester) async {
417
    await tester.pumpWidget(
418 419 420 421
      MaterialApp(
        home: Center(
          child: Material(
            child: Stepper(
422
              steps: const <Step>[
423 424
                Step(
                  title: Text('A'),
425
                  state: StepState.complete,
426
                  content: SizedBox(
427 428 429 430
                    width: 100.0,
                    height: 100.0,
                  ),
                ),
431 432 433
                Step(
                  title: Text('B'),
                  content: SizedBox(
434 435 436 437 438 439 440 441 442
                    width: 100.0,
                    height: 100.0,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
443 444 445 446 447 448
    );

    expect(find.text('1'), findsNothing);
    expect(find.text('2'), findsOneWidget);
  });

449
  testWidgetsWithLeakTracking('Stepper custom controls test', (WidgetTester tester) async {
450 451 452 453 454 455 456 457 458 459
    bool continuePressed = false;
    void setContinue() {
      continuePressed = true;
    }

    bool canceledPressed = false;
    void setCanceled() {
      canceledPressed = true;
    }

460
    Widget builder(BuildContext context, ControlsDetails details) {
461 462 463 464 465 466 467
      return Container(
        margin: const EdgeInsets.only(top: 16.0),
        child: ConstrainedBox(
          constraints: const BoxConstraints.tightFor(height: 48.0),
          child: Row(
            children: <Widget>[
              TextButton(
468
                onPressed: details.onStepContinue,
469 470 471 472 473
                child: const Text('Let us continue!'),
              ),
              Container(
                margin: const EdgeInsetsDirectional.only(start: 8.0),
                child: TextButton(
474
                  onPressed: details.onStepCancel,
475
                  child: const Text('Cancel This!'),
476
                ),
477 478
              ),
            ],
479
          ),
480 481 482
        ),
      );
    }
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527

    await tester.pumpWidget(
      MaterialApp(
        home: Center(
          child: Material(
            child: Stepper(
              controlsBuilder: builder,
              onStepCancel: setCanceled,
              onStepContinue: setContinue,
              steps: const <Step>[
                Step(
                  title: Text('A'),
                  state: StepState.complete,
                  content: SizedBox(
                    width: 100.0,
                    height: 100.0,
                  ),
                ),
                Step(
                  title: Text('B'),
                  content: SizedBox(
                    width: 100.0,
                    height: 100.0,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );

    // 2 because stepper creates a set of controls for each step
    expect(find.text('Let us continue!'), findsNWidgets(2));
    expect(find.text('Cancel This!'), findsNWidgets(2));

    await tester.tap(find.text('Cancel This!').first);
    await tester.pumpAndSettle();
    await tester.tap(find.text('Let us continue!').first);
    await tester.pumpAndSettle();

    expect(canceledPressed, isTrue);
    expect(continuePressed, isTrue);
  });

528
testWidgetsWithLeakTracking('Stepper custom indexed controls test', (WidgetTester tester) async {
529 530 531 532 533 534 535 536 537 538 539 540 541

    int currentStep = 0;
    void setContinue() {
      currentStep += 1;
    }

    void setCanceled() {
      currentStep -= 1;
    }

    Widget builder(BuildContext context, ControlsDetails details) {
      // For the purposes of testing, only render something for the active
      // step.
542
      if (!details.isActive) {
543
        return Container();
544
      }
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622

      return Container(
        margin: const EdgeInsets.only(top: 16.0),
        child: ConstrainedBox(
          constraints: const BoxConstraints.tightFor(height: 48.0),
          child: Row(
            children: <Widget>[
              TextButton(
                onPressed: details.onStepContinue,
                child: Text('Continue to ${details.stepIndex + 1}'),
              ),
              Container(
                margin: const EdgeInsetsDirectional.only(start: 8.0),
                child: TextButton(
                  onPressed: details.onStepCancel,
                  child: Text('Return to ${details.stepIndex - 1}'),
                ),
              ),
            ],
          ),
        ),
      );
    }

    await tester.pumpWidget(
      MaterialApp(
        home: Center(
          child: Material(
            child: StatefulBuilder(
              builder: (BuildContext context, StateSetter setState) {
                return Stepper(
                  currentStep: currentStep,
                  controlsBuilder: builder,
                  onStepCancel: () => setState(setCanceled),
                  onStepContinue: () => setState(setContinue),
                  steps: const <Step>[
                    Step(
                      title: Text('A'),
                      state: StepState.complete,
                      content: SizedBox(
                        width: 100.0,
                        height: 100.0,
                      ),
                    ),
                    Step(
                      title: Text('C'),
                      content: SizedBox(
                        width: 100.0,
                        height: 100.0,
                      ),
                    ),
                  ],
                );
              },
            ),
          ),
        ),
      ),
    );

    // Never mind that there is no Step -1 or Step 2 -- actual build method
    // implementations would make those checks.
    expect(find.text('Return to -1'), findsNWidgets(1));
    expect(find.text('Continue to 1'), findsNWidgets(1));
    expect(find.text('Return to 0'), findsNWidgets(0));
    expect(find.text('Continue to 2'), findsNWidgets(0));

    await tester.tap(find.text('Continue to 1').first);
    await tester.pumpAndSettle();

    // Never mind that there is no Step -1 or Step 2 -- actual build method
    // implementations would make those checks.
    expect(find.text('Return to -1'), findsNWidgets(0));
    expect(find.text('Continue to 1'), findsNWidgets(0));
    expect(find.text('Return to 0'), findsNWidgets(1));
    expect(find.text('Continue to 2'), findsNWidgets(1));
  });

623
  testWidgetsWithLeakTracking('Stepper error test', (WidgetTester tester) async {
624
    await tester.pumpWidget(
625 626 627 628
      MaterialApp(
        home: Center(
          child: Material(
            child: Stepper(
629
              steps: const <Step>[
630 631
                Step(
                  title: Text('A'),
632
                  state: StepState.error,
633
                  content: SizedBox(
634 635 636 637 638 639 640 641 642
                    width: 100.0,
                    height: 100.0,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
643 644 645 646
    );

    expect(find.text('!'), findsOneWidget);
  });
647

648
  testWidgetsWithLeakTracking('Nested stepper error test', (WidgetTester tester) async {
649 650
    late FlutterErrorDetails errorDetails;
    final FlutterExceptionHandler? oldHandler = FlutterError.onError;
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
    FlutterError.onError = (FlutterErrorDetails details) {
      errorDetails = details;
    };
    try {
      await tester.pumpWidget(
        MaterialApp(
          home: Material(
            child: Stepper(
              type: StepperType.horizontal,
              steps: <Step>[
                Step(
                  title: const Text('Step 2'),
                  content:  Stepper(
                    steps: const <Step>[
                      Step(
                        title: Text('Nested step 1'),
                        content: Text('A'),
                      ),
                      Step(
                        title: Text('Nested step 2'),
                        content: Text('A'),
                      ),
                    ],
674
                  ),
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
                ),
                const Step(
                  title: Text('Step 1'),
                  content: Text('A'),
                ),
              ],
            ),
          ),
        ),
      );
    } finally {
      FlutterError.onError = oldHandler;
    }

    expect(errorDetails.stack, isNotNull);
    // Check the ErrorDetails without the stack trace
691 692
    final String fullErrorMessage = errorDetails.toString();
    final List<String> lines = fullErrorMessage.split('\n');
693 694
    // The lines in the middle of the error message contain the stack trace
    // which will change depending on where the test is run.
695 696 697 698 699 700
    final String errorMessage = lines.takeWhile(
      (String line) => line != '',
    ).join('\n');
    expect(errorMessage.length, lessThan(fullErrorMessage.length));
    expect(errorMessage, startsWith(
      '══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞════════════════════════\n'
701
      'The following assertion was thrown building Stepper(',
702 703 704 705 706 707 708 709
    ));
    // The description string of the stepper looks slightly different depending
    // on the platform and is omitted here.
    expect(errorMessage, endsWith(
      '):\n'
      'Steppers must not be nested.\n'
      'The material specification advises that one should avoid\n'
      'embedding steppers within steppers.\n'
710
      'https://material.io/archive/guidelines/components/steppers.html#steppers-usage',
711
    ));
712 713
  });

714
  ///https://github.com/flutter/flutter/issues/16920
715
  testWidgetsWithLeakTracking('Stepper icons size test', (WidgetTester tester) async {
716
    await tester.pumpWidget(
717
      MaterialApp(
718
        home: Material(
719
          child: Stepper(
720
            steps: const <Step>[
721 722
              Step(
                title: Text('A'),
723
                state: StepState.editing,
724
                content: SizedBox(width: 100.0, height: 100.0),
725
              ),
726 727
              Step(
                title: Text('B'),
728
                state: StepState.complete,
729
                content: SizedBox(width: 100.0, height: 100.0),
730 731 732 733 734 735 736 737 738 739 740 741 742
              ),
            ],
          ),
        ),
      ),
    );

    RenderBox renderObject = tester.renderObject(find.byIcon(Icons.edit));
    expect(renderObject.size, equals(const Size.square(18.0)));

    renderObject = tester.renderObject(find.byIcon(Icons.check));
    expect(renderObject.size, equals(const Size.square(18.0)));
  });
743

744
  testWidgetsWithLeakTracking('Stepper physics scroll error test', (WidgetTester tester) async {
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: ListView(
            children: <Widget>[
              Stepper(
                steps: const <Step>[
                  Step(title: Text('Step 1'), content: Text('Text 1')),
                  Step(title: Text('Step 2'), content: Text('Text 2')),
                  Step(title: Text('Step 3'), content: Text('Text 3')),
                  Step(title: Text('Step 4'), content: Text('Text 4')),
                  Step(title: Text('Step 5'), content: Text('Text 5')),
                  Step(title: Text('Step 6'), content: Text('Text 6')),
                  Step(title: Text('Step 7'), content: Text('Text 7')),
                  Step(title: Text('Step 8'), content: Text('Text 8')),
                  Step(title: Text('Step 9'), content: Text('Text 9')),
                  Step(title: Text('Step 10'), content: Text('Text 10')),
                ],
              ),
              const Text('Text After Stepper'),
            ],
          ),
        ),
      ),
    );

    await tester.fling(find.byType(Stepper), const Offset(0.0, -100.0), 1000.0);
    await tester.pumpAndSettle();

    expect(find.text('Text After Stepper'), findsNothing);
  });
776

777
  testWidgetsWithLeakTracking("Vertical Stepper can't be focused when disabled.", (WidgetTester tester) async {
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: Stepper(
            steps: const <Step>[
              Step(
                title: Text('Step 0'),
                state: StepState.disabled,
                content: Text('Text 0'),
              ),
            ],
          ),
        ),
      ),
    );
    await tester.pump();

795
    final FocusNode disabledNode = Focus.of(tester.element(find.text('Step 0')), scopeOk: true);
796 797 798 799 800
    disabledNode.requestFocus();
    await tester.pump();
    expect(disabledNode.hasPrimaryFocus, isFalse);
  });

801
  testWidgetsWithLeakTracking("Horizontal Stepper can't be focused when disabled.", (WidgetTester tester) async {
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: Stepper(
            type: StepperType.horizontal,
            steps: const <Step>[
              Step(
                title: Text('Step 0'),
                state: StepState.disabled,
                content: Text('Text 0'),
              ),
            ],
          ),
        ),
      ),
    );
    await tester.pump();

820
    final FocusNode disabledNode = Focus.of(tester.element(find.text('Step 0')), scopeOk: true);
821 822 823 824
    disabledNode.requestFocus();
    await tester.pump();
    expect(disabledNode.hasPrimaryFocus, isFalse);
  });
825

826
  testWidgetsWithLeakTracking('Stepper header title should not overflow', (WidgetTester tester) async {
827 828 829 830 831 832 833 834 835 836 837 838
    const String longText =
        'A long long long long long long long long long long long long text';

    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: ListView(
            children: <Widget>[
              Stepper(
                steps: const <Step>[
                  Step(
                    title: Text(longText),
839
                    content: Text('Text content'),
840 841 842 843 844 845 846 847 848 849 850 851
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );

    expect(tester.takeException(), isNull);
  });

852
  testWidgetsWithLeakTracking('Stepper header subtitle should not overflow', (WidgetTester tester) async {
853 854 855 856 857 858 859 860 861 862 863 864 865
    const String longText =
        'A long long long long long long long long long long long long text';

    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: ListView(
            children: <Widget>[
              Stepper(
                steps: const <Step>[
                  Step(
                    title: Text('Regular title'),
                    subtitle: Text(longText),
866
                    content: Text('Text content'),
867 868 869 870 871 872 873 874 875 876 877
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );

    expect(tester.takeException(), isNull);
  });
878

879
  testWidgetsWithLeakTracking('Material2 - Stepper enabled button styles', (WidgetTester tester) async {
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
    Widget buildFrame(ThemeData theme) {
      return MaterialApp(
        theme: theme,
        home: Material(
          child: Stepper(
            type: StepperType.horizontal,
            onStepCancel: () { },
            onStepContinue: () { },
            steps: const <Step>[
              Step(
                title: Text('step1'),
                content: SizedBox(width: 100, height: 100),
              ),
            ],
          ),
        ),
      );
    }

    Material buttonMaterial(String label) {
      return tester.widget<Material>(
901
        find.descendant(of: find.widgetWithText(TextButton, label), matching: find.byType(Material)),
902 903 904 905 906
      );
    }

    const OutlinedBorder buttonShape = RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(2)));

907
    final ThemeData themeLight = ThemeData(useMaterial3: false);
908 909
    await tester.pumpWidget(buildFrame(themeLight));

910 911 912 913 914
    const String continueStr = 'CONTINUE';
    const String cancelStr = 'CANCEL';
    const Rect continueButtonRect = Rect.fromLTRB(24.0, 212.0, 168.0, 260.0);
    const Rect cancelButtonRect = Rect.fromLTRB(176.0, 212.0, 292.0, 260.0);
    expect(buttonMaterial(continueStr).color!.value, 0xff2196f3);
915 916 917 918 919 920 921 922 923
    expect(buttonMaterial(continueStr).textStyle!.color!.value, 0xffffffff);
    expect(buttonMaterial(continueStr).shape, buttonShape);
    expect(tester.getRect(find.widgetWithText(TextButton, continueStr)), continueButtonRect);

    expect(buttonMaterial(cancelStr).color!.value, 0);
    expect(buttonMaterial(cancelStr).textStyle!.color!.value, 0x8a000000);
    expect(buttonMaterial(cancelStr).shape, buttonShape);
    expect(tester.getRect(find.widgetWithText(TextButton, cancelStr)), cancelButtonRect);

924
    final ThemeData themeDark = ThemeData.dark(useMaterial3: false);
925
    await tester.pumpWidget(buildFrame(themeDark));
926 927
    await tester.pumpAndSettle(); // Complete the theme animation.

928
    expect(buttonMaterial(continueStr).color!.value, 0);
929
    expect(buttonMaterial(continueStr).textStyle!.color!.value, 0xffffffff);
930 931
    expect(buttonMaterial(continueStr).shape, buttonShape);
    expect(tester.getRect(find.widgetWithText(TextButton, continueStr)), continueButtonRect);
932

933 934 935 936
    expect(buttonMaterial(cancelStr).color!.value, 0);
    expect(buttonMaterial(cancelStr).textStyle!.color!.value, 0xb3ffffff);
    expect(buttonMaterial(cancelStr).shape, buttonShape);
    expect(tester.getRect(find.widgetWithText(TextButton, cancelStr)), cancelButtonRect);
937 938
  });

939
  testWidgetsWithLeakTracking('Material3 - Stepper enabled button styles', (WidgetTester tester) async {
940 941 942 943 944 945
    Widget buildFrame(ThemeData theme) {
      return MaterialApp(
        theme: theme,
        home: Material(
          child: Stepper(
            type: StepperType.horizontal,
946 947
            onStepCancel: () { },
            onStepContinue: () { },
948 949 950 951 952 953 954 955 956 957 958 959 960
            steps: const <Step>[
              Step(
                title: Text('step1'),
                content: SizedBox(width: 100, height: 100),
              ),
            ],
          ),
        ),
      );
    }

    Material buttonMaterial(String label) {
      return tester.widget<Material>(
961
        find.descendant(of: find.widgetWithText(TextButton, label), matching: find.byType(Material)),
962 963 964
      );
    }

965 966 967
    const OutlinedBorder buttonShape = RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(2)));

    final ThemeData themeLight = ThemeData(useMaterial3: true);
968
    await tester.pumpWidget(buildFrame(themeLight));
969

970 971
    const String continueStr = 'Continue';
    const String cancelStr = 'Cancel';
972 973
    const Rect continueButtonRect = Rect.fromLTRB(24.0, 212.0, 168.8, 260.0);
    const Rect cancelButtonRect = Rect.fromLTRB(176.8, 212.0, 293.4, 260.0);
974 975 976
    expect(buttonMaterial(continueStr).color!.value, themeLight.colorScheme.primary.value);
    expect(buttonMaterial(continueStr).textStyle!.color!.value, 0xffffffff);
    expect(buttonMaterial(continueStr).shape, buttonShape);
977 978 979 980
    expect(
      tester.getRect(find.widgetWithText(TextButton, continueStr)),
      rectMoreOrLessEquals(continueButtonRect, epsilon: 0.001),
    );
981 982 983 984

    expect(buttonMaterial(cancelStr).color!.value, 0);
    expect(buttonMaterial(cancelStr).textStyle!.color!.value, 0x8a000000);
    expect(buttonMaterial(cancelStr).shape, buttonShape);
985 986 987 988
    expect(
      tester.getRect(find.widgetWithText(TextButton, cancelStr)),
      rectMoreOrLessEquals(cancelButtonRect, epsilon: 0.001),
    );
989 990 991 992 993

    final ThemeData themeDark = ThemeData.dark(useMaterial3: true);
    await tester.pumpWidget(buildFrame(themeDark));
    await tester.pumpAndSettle(); // Complete the theme animation.

994
    expect(buttonMaterial(continueStr).color!.value, 0);
995 996
    expect(buttonMaterial(continueStr).textStyle!.color!.value, themeDark.colorScheme.onSurface.value);
    expect(buttonMaterial(continueStr).shape, buttonShape);
997 998 999 1000
    expect(
      tester.getRect(find.widgetWithText(TextButton, continueStr)),
      rectMoreOrLessEquals(continueButtonRect, epsilon: 0.001),
    );
1001

1002
    expect(buttonMaterial(cancelStr).color!.value, 0);
1003 1004
    expect(buttonMaterial(cancelStr).textStyle!.color!.value, 0xb3ffffff);
    expect(buttonMaterial(cancelStr).shape, buttonShape);
1005 1006 1007 1008
    expect(
      tester.getRect(find.widgetWithText(TextButton, cancelStr)),
      rectMoreOrLessEquals(cancelButtonRect, epsilon: 0.001),
    );
1009
  });
1010

1011
  testWidgetsWithLeakTracking('Material2 - Stepper disabled button styles', (WidgetTester tester) async {
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
    Widget buildFrame(ThemeData theme) {
      return MaterialApp(
        theme: theme,
        home: Material(
          child: Stepper(
            type: StepperType.horizontal,
            steps: const <Step>[
              Step(
                title: Text('step1'),
                content: SizedBox(width: 100, height: 100),
              ),
            ],
          ),
        ),
      );
    }

    Material buttonMaterial(String label) {
      return tester.widget<Material>(
        find.descendant(of: find.widgetWithText(TextButton, label), matching: find.byType(Material)),
      );
    }

    final ThemeData themeLight = ThemeData(useMaterial3: false);
    await tester.pumpWidget(buildFrame(themeLight));

    const String continueStr = 'CONTINUE';
    const String cancelStr = 'CANCEL';
    expect(buttonMaterial(continueStr).color!.value, 0);
    expect(buttonMaterial(continueStr).textStyle!.color!.value, 0x61000000);

    expect(buttonMaterial(cancelStr).color!.value, 0);
    expect(buttonMaterial(cancelStr).textStyle!.color!.value, 0x61000000);

    final ThemeData themeDark = ThemeData.dark(useMaterial3: false);
1047
    await tester.pumpWidget(buildFrame(themeDark));
1048 1049
    await tester.pumpAndSettle(); // Complete the theme animation.

1050
    expect(buttonMaterial(continueStr).color!.value, 0);
1051
    expect(buttonMaterial(continueStr).textStyle!.color!.value, 0x61ffffff);
1052

1053
    expect(buttonMaterial(cancelStr).color!.value, 0);
1054 1055 1056
    expect(buttonMaterial(cancelStr).textStyle!.color!.value, 0x61ffffff);
  });

1057
  testWidgetsWithLeakTracking('Material3 - Stepper disabled button styles', (WidgetTester tester) async {
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
    Widget buildFrame(ThemeData theme) {
      return MaterialApp(
        theme: theme,
        home: Material(
          child: Stepper(
            type: StepperType.horizontal,
            steps: const <Step>[
              Step(
                title: Text('step1'),
                content: SizedBox(width: 100, height: 100),
              ),
            ],
          ),
        ),
      );
    }

    Material buttonMaterial(String label) {
      return tester.widget<Material>(
        find.descendant(of: find.widgetWithText(TextButton, label), matching: find.byType(Material)),
      );
    }

    final ThemeData themeLight = ThemeData(useMaterial3: true);
    final ColorScheme colorsLight = themeLight.colorScheme;
    await tester.pumpWidget(buildFrame(themeLight));

    const String continueStr = 'Continue';
    const String cancelStr = 'Cancel';
    expect(buttonMaterial(continueStr).color!.value, 0);
    expect(
      buttonMaterial(continueStr).textStyle!.color!.value,
      colorsLight.onSurface.withOpacity(0.38).value,
    );

    expect(buttonMaterial(cancelStr).color!.value, 0);
    expect(
      buttonMaterial(cancelStr).textStyle!.color!.value,
      colorsLight.onSurface.withOpacity(0.38).value,
    );

    final ThemeData themeDark = ThemeData.dark(useMaterial3: true);
    final ColorScheme colorsDark = themeDark.colorScheme;
    await tester.pumpWidget(buildFrame(themeDark));
    await tester.pumpAndSettle(); // Complete the theme animation.

    expect(buttonMaterial(continueStr).color!.value, 0);
    expect(
      buttonMaterial(continueStr).textStyle!.color!.value,
      colorsDark.onSurface.withOpacity(0.38).value,
    );

    expect(buttonMaterial(cancelStr).color!.value, 0);
    expect(
      buttonMaterial(cancelStr).textStyle!.color!.value,
      colorsDark.onSurface.withOpacity(0.38).value,
    );
1115
  });
TheBirb's avatar
TheBirb committed
1116

1117
  testWidgetsWithLeakTracking('Vertical and Horizontal Stepper physics test', (WidgetTester tester) async {
TheBirb's avatar
TheBirb committed
1118 1119
    const ScrollPhysics physics = NeverScrollableScrollPhysics();

1120
    for (final StepperType type in StepperType.values) {
TheBirb's avatar
TheBirb committed
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
      await tester.pumpWidget(
        MaterialApp(
          home: Material(
            child: Stepper(
              physics: physics,
              type: type,
              steps: const <Step>[
                Step(
                  title: Text('Step 1'),
                  content: SizedBox(
                    width: 100.0,
                    height: 100.0,
                  ),
                ),
              ],
            ),
          ),
        ),
      );

      final ListView listView = tester.widget<ListView>(find.descendant(of: find.byType(Stepper), matching: find.byType(ListView)));
      expect(listView.physics, physics);
    }
  });
1145

1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
  testWidgetsWithLeakTracking('ScrollController is passed to the stepper listview', (WidgetTester tester) async {
    final ScrollController controller = ScrollController();
    addTearDown(() => controller.dispose());
    for (final StepperType type in StepperType.values) {
      await tester.pumpWidget(
        MaterialApp(
          home: Material(
            child: Stepper(
              controller: controller,
              type: type,
              steps: const <Step>[
                Step(
                  title: Text('Step 1'),
                  content: SizedBox(
                    width: 100.0,
                    height: 100.0,
1162
                  ),
1163 1164
                ),
              ],
1165 1166
            ),
          ),
1167 1168
        ),
      );
1169

1170 1171 1172 1173 1174 1175 1176
      final ListView listView = tester.widget<ListView>(
        find.descendant(of: find.byType(Stepper),
        matching: find.byType(ListView),
      ));
      expect(listView.controller, controller);
    }
  });
1177

1178
  testWidgetsWithLeakTracking('Stepper horizontal size test', (WidgetTester tester) async {
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
    // Regression test for https://github.com/flutter/flutter/pull/77732
    Widget buildFrame({ bool isActive = true, Brightness? brightness }) {
      return MaterialApp(
        theme: brightness == Brightness.dark ? ThemeData.dark() : ThemeData.light(),
        home: Scaffold(
          body: Center(
            child: Stepper(
              type: StepperType.horizontal,
              steps: <Step>[
                Step(
                  title: const Text('step'),
                  content: const Text('content'),
                  isActive: isActive,
                ),
              ],
            ),
          ),
        ),
      );
    }

    Color? circleFillColor() {
      final Finder container = find.widgetWithText(AnimatedContainer, '1');
      return (tester.widget<AnimatedContainer>(container).decoration as BoxDecoration?)?.color;
    }

    // Light theme
    final ColorScheme light = ThemeData.light().colorScheme;
1207
    await tester.pumpWidget(buildFrame(brightness: Brightness.light));
1208 1209 1210 1211 1212 1213 1214
    expect(circleFillColor(), light.primary);
    await tester.pumpWidget(buildFrame(isActive: false, brightness: Brightness.light));
    await tester.pumpAndSettle();
    expect(circleFillColor(), light.onSurface.withOpacity(0.38));

    // Dark theme
    final ColorScheme dark = ThemeData.dark().colorScheme;
1215
    await tester.pumpWidget(buildFrame(brightness: Brightness.dark));
1216 1217 1218 1219 1220 1221
    await tester.pumpAndSettle();
    expect(circleFillColor(), dark.secondary);
    await tester.pumpWidget(buildFrame(isActive: false, brightness: Brightness.dark));
    await tester.pumpAndSettle();
    expect(circleFillColor(), dark.background);
  });
1222

1223
  testWidgetsWithLeakTracking('Stepper custom elevation', (WidgetTester tester) async {
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
     const double elevation = 4.0;

     await tester.pumpWidget(
       MaterialApp(
         home: Material(
           child: SizedBox(
             width: 200,
             height: 75,
             child: Stepper(
               type: StepperType.horizontal,
               elevation: elevation,
               steps: const <Step>[
                 Step(
                   title: Text('Regular title'),
                   content: Text('Text content'),
                 ),
               ],
             ),
           ),
         ),
       ),
     );

     final Material material = tester.firstWidget<Material>(
       find.descendant(
         of: find.byType(Stepper),
         matching: find.byType(Material),
       ),
     );

     expect(material.elevation, elevation);
   });

1257
   testWidgetsWithLeakTracking('Stepper with default elevation', (WidgetTester tester) async {
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286

     await tester.pumpWidget(
       MaterialApp(
         home: Material(
           child: SizedBox(
             width: 200,
             height: 75,
             child: Stepper(
               type: StepperType.horizontal,
               steps: const <Step>[
                 Step(
                   title: Text('Regular title'),
                   content: Text('Text content')
                 ),
               ],
             ),
           ),
         ),
       ),
     );

     final Material material = tester.firstWidget<Material>(
       find.descendant(
         of: find.byType(Stepper),
         matching: find.byType(Material),
       ),
     );

     expect(material.elevation, 2.0);
1287 1288
   });

1289
  testWidgetsWithLeakTracking('Stepper horizontal preserves state', (WidgetTester tester) async {
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
    const Color untappedColor = Colors.blue;
    const Color tappedColor = Colors.red;
    int index = 0;

    Widget buildFrame() {
      return MaterialApp(
        home: Scaffold(
          body: Center(
            // Must break this out into its own widget purely to be able to call `setState()`
            child: StatefulBuilder(
              builder: (BuildContext context, StateSetter setState) {
                return Stepper(
                  onStepTapped: (int i) => setState(() => index = i),
                  currentStep: index,
                  type: StepperType.horizontal,
                  steps: const <Step>[
                    Step(
                      title: Text('Step 1'),
                      content: _TappableColorWidget(
                        key: Key('tappable-color'),
                        tappedColor: tappedColor,
                        untappedColor: untappedColor,
                      ),
                    ),
                    Step(
                      title: Text('Step 2'),
                      content: Text('Step 2 Content'),
                    ),
                  ],
                );
              },
            ),
          ),
        ),
      );
    }

    final Widget widget = buildFrame();
    await tester.pumpWidget(widget);

    // Set up a getter to examine the MacGuffin's color
    Color getColor() => tester.widget<ColoredBox>(
      find.descendant(of: find.byKey(const Key('tappable-color')), matching: find.byType(ColoredBox)),
    ).color;

    // We are on step 1
    expect(find.text('Step 2 Content'), findsNothing);
    expect(getColor(), untappedColor);

    await tester.tap(find.byKey(const Key('tap-me')));
    await tester.pumpAndSettle();
    expect(getColor(), tappedColor);

    // Now flip to step 2
    await tester.tap(find.text('Step 2'));
    await tester.pumpAndSettle();

    // Confirm that we did in fact flip to step 2
    expect(find.text('Step 2 Content'), findsOneWidget);

    // Now go back to step 1
    await tester.tap(find.text('Step 1'));
    await tester.pumpAndSettle();

    // Confirm that we flipped back to step 1
    expect(find.text('Step 2 Content'), findsNothing);

    // The color should still be `tappedColor`
    expect(getColor(), tappedColor);
  });
1360
       testWidgetsWithLeakTracking('Stepper custom margin', (WidgetTester tester) async {
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394

      const EdgeInsetsGeometry margin = EdgeInsetsDirectional.only(
        bottom: 20,
        top: 20,
      );

     await tester.pumpWidget(
       MaterialApp(
         home: Material(
           child: SizedBox(
             width: 200,
             height: 75,
             child: Stepper(
               margin: margin,
               steps: const <Step>[
                 Step(
                   title: Text('Regular title'),
                   content: Text('Text content')
                 ),
               ],
             ),
           ),
         ),
       ),
     );

     final Stepper material = tester.firstWidget<Stepper>(
       find.descendant(
         of: find.byType(Material),
         matching: find.byType(Stepper),
       ),
     );

     expect(material.margin, equals(margin));
1395
   });
1396

1397
  testWidgetsWithLeakTracking('Stepper with Alternative Label', (WidgetTester tester) async {
1398
    int index = 0;
1399 1400 1401
    late TextStyle bodyLargeStyle;
    late TextStyle bodyMediumStyle;
    late TextStyle bodySmallStyle;
1402 1403 1404 1405 1406 1407

    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: StatefulBuilder(
              builder: (BuildContext context, StateSetter setState) {
1408 1409 1410
            bodyLargeStyle = Theme.of(context).textTheme.bodyText1!;
            bodyMediumStyle = Theme.of(context).textTheme.bodyText2!;
            bodySmallStyle = Theme.of(context).textTheme.caption!;
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
            return Stepper(
              type: StepperType.horizontal,
              currentStep: index,
              onStepTapped: (int i) {
                setState(() {
                  index = i;
                });
              },
              steps: <Step>[
                Step(
                  title: const Text('Title 1'),
                  content: const Text('Content 1'),
1423
                  label: Text('Label 1', style: Theme.of(context).textTheme.bodySmall),
1424 1425 1426 1427
                ),
                Step(
                  title: const Text('Title 2'),
                  content: const Text('Content 2'),
1428
                  label: Text('Label 2', style: Theme.of(context).textTheme.bodyLarge),
1429 1430 1431 1432
                ),
                Step(
                  title: const Text('Title 3'),
                  content: const Text('Content 3'),
1433
                  label: Text('Label 3', style: Theme.of(context).textTheme.bodyMedium),
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
                ),
              ],
            );
          }),
        ),
      ),
    );

    // Check Styles of Label Text Widgets before tapping steps
    final Text label1TextWidget =
        tester.widget<Text>(find.text('Label 1'));
    final Text label3TextWidget =
        tester.widget<Text>(find.text('Label 3'));

1448 1449
    expect(bodySmallStyle, label1TextWidget.style);
    expect(bodyMediumStyle, label3TextWidget.style);
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460

    late Text selectedLabelTextWidget;
    late Text nextLabelTextWidget;

    // Tap to Step1 Label then, `index` become 0
    await tester.tap(find.text('Label 1'));
    expect(index, 0);

    // Check Styles of Selected Label Text Widgets and Another Label Text Widget
    selectedLabelTextWidget =
        tester.widget<Text>(find.text('Label ${index + 1}'));
1461
    expect(bodySmallStyle, selectedLabelTextWidget.style);
1462 1463
    nextLabelTextWidget =
        tester.widget<Text>(find.text('Label ${index + 2}'));
1464
    expect(bodyLargeStyle, nextLabelTextWidget.style);
1465 1466 1467 1468 1469 1470 1471 1472 1473


    // Tap to Step2 Label then, `index` become 1
    await tester.tap(find.text('Label 2'));
    expect(index, 1);

    // Check Styles of Selected Label Text Widgets and Another Label Text Widget
    selectedLabelTextWidget =
        tester.widget<Text>(find.text('Label ${index + 1}'));
1474
    expect(bodyLargeStyle, selectedLabelTextWidget.style);
1475 1476 1477

    nextLabelTextWidget =
        tester.widget<Text>(find.text('Label ${index + 2}'));
1478
    expect(bodyMediumStyle, nextLabelTextWidget.style);
1479
  });
1480

1481
  testWidgetsWithLeakTracking('Stepper Connector Style', (WidgetTester tester) async {
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
    const Color selectedColor = Colors.black;
    const Color disabledColor = Colors.white;
    int index = 0;

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: Center(
            child: StatefulBuilder(
              builder: (BuildContext context, StateSetter setState) {
                return Stepper(
                  type: StepperType.horizontal,
                  connectorColor: MaterialStateProperty.resolveWith<Color>((Set<MaterialState> states) =>
                  states.contains(MaterialState.selected)
                    ? selectedColor
                    : disabledColor),
                  onStepTapped: (int i) => setState(() => index = i),
                  currentStep: index,
                  steps: <Step>[
                    Step(
                      isActive: index >= 0,
                      title: const Text('step1'),
                      content: const Text('step1 content'),
                    ),
                    Step(
                      isActive: index >= 1,
                      title: const Text('step2'),
                      content: const Text('step2 content'),
                    ),
                  ],
                );
              },
            ),
          ),
        ),
      )
    );

    Color? circleColor(String circleText) => (tester.widget<AnimatedContainer>(
      find.widgetWithText(AnimatedContainer, circleText),
    ).decoration as BoxDecoration?)?.color;

    Color? lineColor(String keyStep) => tester.widget<Container>(find.byKey(Key(keyStep))).color;

    // Step 1
    // check if I'm in step 1
    expect(find.text('step1 content'), findsOneWidget);
    expect(find.text('step2 content'), findsNothing);

    expect(circleColor('1'), selectedColor);
    expect(circleColor('2'), disabledColor);
    // in two steps case there will be single line
    expect(lineColor('line0'), disabledColor);

    // now hitting step two
    await tester.tap(find.text('step2'));
    await tester.pumpAndSettle();

    // check if I'm in step 1
    expect(find.text('step1 content'), findsNothing);
    expect(find.text('step2 content'), findsOneWidget);

    expect(circleColor('1'), selectedColor);
    expect(circleColor('2'), selectedColor);

    expect(lineColor('line0'), selectedColor);
  });

1550
  testWidgetsWithLeakTracking('Stepper stepIconBuilder test', (WidgetTester tester) async {
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: Stepper(
            stepIconBuilder: (int index, StepState state) {
              if (state == StepState.complete) {
                return const FlutterLogo(size: 18);
              }
              return null;
            },
            steps: const <Step>[
              Step(
                title: Text('A'),
                state: StepState.complete,
                content: SizedBox(width: 100.0, height: 100.0),
              ),
              Step(
                title: Text('B'),
                state: StepState.editing,
                content: SizedBox(width: 100.0, height: 100.0),
              ),
              Step(
                title: Text('C'),
                state: StepState.error,
                content: SizedBox(width: 100.0, height: 100.0),
              ),
            ],
          ),
        ),
      ),
    );

    /// Finds the overridden widget for StepState.complete
    expect(find.byType(FlutterLogo), findsOneWidget);

    /// StepState.editing and StepState.error should have a default icon
    expect(find.byIcon(Icons.edit), findsOneWidget);
    expect(find.text('!'), findsOneWidget);
  });

1591
}
1592 1593

class _TappableColorWidget extends StatefulWidget {
1594
  const _TappableColorWidget({required this.tappedColor, required this.untappedColor, super.key,});
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629

  final Color tappedColor;
  final Color untappedColor;

  @override
  State<StatefulWidget> createState() => _TappableColorWidgetState();
}

class _TappableColorWidgetState extends State<_TappableColorWidget> {

  Color? color;

  @override
  void initState() {
    super.initState();
    color = widget.untappedColor;
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        setState((){
          color = widget.tappedColor;
        });
      },
      child: Container(
        key: const Key('tap-me'),
        height: 50,
        width: 50,
        color: color,
      ),
    );
  }
}