stepper_test.dart 52.1 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 8 9
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
10
  testWidgets('Material3 has sentence case labels', (WidgetTester tester) async {
11 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
    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);
  });

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

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

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

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

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

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

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

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

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

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

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

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

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

    await tester.pumpWidget(
222
      MaterialApp(
223 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
        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);
  });

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

459
    Widget builder(BuildContext context, ControlsDetails details) {
460 461 462 463 464 465 466
      return Container(
        margin: const EdgeInsets.only(top: 16.0),
        child: ConstrainedBox(
          constraints: const BoxConstraints.tightFor(height: 48.0),
          child: Row(
            children: <Widget>[
              TextButton(
467
                onPressed: details.onStepContinue,
468 469 470 471 472
                child: const Text('Let us continue!'),
              ),
              Container(
                margin: const EdgeInsetsDirectional.only(start: 8.0),
                child: TextButton(
473
                  onPressed: details.onStepCancel,
474
                  child: const Text('Cancel This!'),
475
                ),
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

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

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

    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.
541
      if (!details.isActive) {
542
        return Container();
543
      }
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

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

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

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

647
  testWidgets('Nested stepper error test', (WidgetTester tester) async {
648 649
    late FlutterErrorDetails errorDetails;
    final FlutterExceptionHandler? oldHandler = FlutterError.onError;
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
    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'),
                      ),
                    ],
673
                  ),
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
                ),
                const Step(
                  title: Text('Step 1'),
                  content: Text('A'),
                ),
              ],
            ),
          ),
        ),
      );
    } finally {
      FlutterError.onError = oldHandler;
    }

    expect(errorDetails.stack, isNotNull);
    // Check the ErrorDetails without the stack trace
690 691
    final String fullErrorMessage = errorDetails.toString();
    final List<String> lines = fullErrorMessage.split('\n');
692 693
    // The lines in the middle of the error message contain the stack trace
    // which will change depending on where the test is run.
694 695 696 697 698 699
    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'
700
      'The following assertion was thrown building Stepper(',
701 702 703 704 705 706 707 708
    ));
    // 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'
709
      'https://material.io/archive/guidelines/components/steppers.html#steppers-usage',
710
    ));
711 712
  });

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

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

743
  testWidgets('Stepper physics scroll error test', (WidgetTester tester) async {
744 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
    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);
  });
775

776
  testWidgets("Vertical Stepper can't be focused when disabled.", (WidgetTester tester) async {
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
    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();

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

800
  testWidgets("Horizontal Stepper can't be focused when disabled.", (WidgetTester tester) async {
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
    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();

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

825
  testWidgets('Stepper header title should not overflow', (WidgetTester tester) async {
826 827 828 829 830 831 832 833 834 835 836 837
    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),
838
                    content: Text('Text content'),
839 840 841 842 843 844 845 846 847 848 849 850
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );

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

851
  testWidgets('Stepper header subtitle should not overflow', (WidgetTester tester) async {
852 853 854 855 856 857 858 859 860 861 862 863 864
    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),
865
                    content: Text('Text content'),
866 867 868 869 870 871 872 873 874 875 876
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );

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

878
  testWidgets('Material2 - Stepper enabled button styles', (WidgetTester tester) async {
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
    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>(
900
        find.descendant(of: find.widgetWithText(TextButton, label), matching: find.byType(Material)),
901 902 903 904 905
      );
    }

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

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

909 910 911 912 913
    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);
914 915 916 917 918 919 920 921 922
    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);

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

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

932 933 934 935
    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);
936 937
  });

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

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

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

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

969 970
    const String continueStr = 'Continue';
    const String cancelStr = 'Cancel';
971 972
    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);
973 974 975
    expect(buttonMaterial(continueStr).color!.value, themeLight.colorScheme.primary.value);
    expect(buttonMaterial(continueStr).textStyle!.color!.value, 0xffffffff);
    expect(buttonMaterial(continueStr).shape, buttonShape);
976 977 978 979
    expect(
      tester.getRect(find.widgetWithText(TextButton, continueStr)),
      rectMoreOrLessEquals(continueButtonRect, epsilon: 0.001),
    );
980 981 982 983

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

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

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

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

1010
  testWidgets('Material2 - Stepper disabled button styles', (WidgetTester tester) async {
1011 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
    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);
1046
    await tester.pumpWidget(buildFrame(themeDark));
1047 1048
    await tester.pumpAndSettle(); // Complete the theme animation.

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

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

1056
  testWidgets('Material3 - Stepper disabled button styles', (WidgetTester tester) async {
1057 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
    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,
    );
1114
  });
TheBirb's avatar
TheBirb committed
1115

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

1119
    for (final StepperType type in StepperType.values) {
TheBirb's avatar
TheBirb committed
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
      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);
    }
  });
1144

1145
  testWidgets('ScrollController is passed to the stepper listview', (WidgetTester tester) async {
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
    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,
1161
                  ),
1162 1163
                ),
              ],
1164 1165
            ),
          ),
1166 1167
        ),
      );
1168

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

1177
  testWidgets('Stepper horizontal size test', (WidgetTester tester) async {
1178 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
    // 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;
1206
    await tester.pumpWidget(buildFrame(brightness: Brightness.light));
1207 1208 1209 1210 1211 1212 1213
    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;
1214
    await tester.pumpWidget(buildFrame(brightness: Brightness.dark));
1215 1216 1217 1218 1219 1220
    await tester.pumpAndSettle();
    expect(circleFillColor(), dark.secondary);
    await tester.pumpWidget(buildFrame(isActive: false, brightness: Brightness.dark));
    await tester.pumpAndSettle();
    expect(circleFillColor(), dark.background);
  });
1221

1222
  testWidgets('Stepper custom elevation', (WidgetTester tester) async {
1223 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
     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);
   });

1256
   testWidgets('Stepper with default elevation', (WidgetTester tester) async {
1257 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

     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);
1286 1287
   });

1288
  testWidgets('Stepper horizontal preserves state', (WidgetTester tester) async {
1289 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
    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);
  });
1359
       testWidgets('Stepper custom margin', (WidgetTester tester) async {
1360 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

      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));
1394
   });
1395

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

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

    // 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'));

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

    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}'));
1460
    expect(bodySmallStyle, selectedLabelTextWidget.style);
1461 1462
    nextLabelTextWidget =
        tester.widget<Text>(find.text('Label ${index + 2}'));
1463
    expect(bodyLargeStyle, nextLabelTextWidget.style);
1464 1465 1466 1467 1468 1469 1470 1471 1472


    // 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}'));
1473
    expect(bodyLargeStyle, selectedLabelTextWidget.style);
1474 1475 1476

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

1480
  testWidgets('Stepper Connector Style', (WidgetTester tester) async {
1481 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
    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
1533
    expect(lineColor('line0'), selectedColor);
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548

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

1549
  testWidgets('Stepper stepIconBuilder test', (WidgetTester tester) async {
1550 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
    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);
  });

1590 1591 1592 1593 1594 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 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
  testWidgets('StepperProperties test', (WidgetTester tester) async {
    const Widget widget = SizedBox.shrink();

    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: Stepper(
            stepIconHeight: 24,
            stepIconWidth: 24,
            stepIconMargin: const EdgeInsets.all(8),
             steps: List<Step>.generate(3, (int index) {
               return Step(
                 title: Text('Step $index'),
                 content: widget,
               );
             }),
          ),
        ),
      ),
    );

    final Finder stepperFinder = find.byType(Stepper);
    final Stepper stepper = tester.widget<Stepper>(stepperFinder);

    expect(stepper.stepIconHeight, 24);
    expect(stepper.stepIconWidth, 24);
    expect(stepper.stepIconMargin, const EdgeInsets.all(8));
  });

  testWidgets('StepStyle test', (WidgetTester tester) async {
    final StepStyle stepStyle = StepStyle(
      color: Colors.white,
      errorColor: Colors.orange,
      connectorColor: Colors.red,
      connectorThickness: 2,
      border: Border.all(),
      gradient: const LinearGradient(
        colors: <Color>[Colors.red, Colors.blue],
      ),
      indexStyle: const TextStyle(color: Colors.black),
    );

    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: Stepper(
            steps: <Step>[
              Step(
                title: const Text('Regular title'),
                content: const Text('Text content'),
                stepStyle: stepStyle,
              ),
            ],
          ),
        ),
      ),
    );

    final Finder stepperFinder = find.byType(Stepper);
    final Stepper stepper = tester.widget<Stepper>(stepperFinder);
    final StepStyle? style = stepper.steps.first.stepStyle;

    expect(style?.color, stepStyle.color);
    expect(style?.errorColor, stepStyle.errorColor);
    expect(style?.connectorColor, stepStyle.connectorColor);
    expect(style?.connectorThickness, stepStyle.connectorThickness);
    expect(style?.border, stepStyle.border);
    expect(style?.gradient, stepStyle.gradient);
    expect(style?.indexStyle, stepStyle.indexStyle);

    //copyWith
    final StepStyle newStyle = stepStyle.copyWith(
      color: Colors.black,
      errorColor: Colors.red,
      connectorColor: Colors.blue,
      connectorThickness: 3,
      border: Border.all(),
      gradient: const LinearGradient(
        colors: <Color>[Colors.red, Colors.blue],
      ),
      indexStyle: const TextStyle(color: Colors.black),
    );

    expect(newStyle.color, Colors.black);
    expect(newStyle.errorColor, Colors.red);
    expect(newStyle.connectorColor, Colors.blue);
    expect(newStyle.connectorThickness, 3);
    expect(newStyle.border, stepStyle.border);
    expect(newStyle.gradient, stepStyle.gradient);
    expect(newStyle.indexStyle, stepStyle.indexStyle);

    //merge
    final StepStyle mergedStyle = stepStyle.merge(newStyle);

    expect(mergedStyle.color, Colors.black);
    expect(mergedStyle.errorColor, Colors.red);
    expect(mergedStyle.connectorColor, Colors.blue);
    expect(mergedStyle.connectorThickness, 3);
    expect(mergedStyle.border, stepStyle.border);
    expect(mergedStyle.gradient, stepStyle.gradient);
    expect(mergedStyle.indexStyle, stepStyle.indexStyle);
  });
1692
}
1693 1694

class _TappableColorWidget extends StatefulWidget {
1695
  const _TappableColorWidget({required this.tappedColor, required this.untappedColor, super.key,});
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730

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