stepper_test.dart 37.2 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 10 11 12 13
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('Stepper tap callback test', (WidgetTester tester) async {
    int index = 0;

    await tester.pumpWidget(
14 15 16
      MaterialApp(
        home: Material(
          child: Stepper(
17 18 19
            onStepTapped: (int i) {
              index = i;
            },
20
            steps: const <Step>[
21 22 23
              Step(
                title: Text('Step 1'),
                content: SizedBox(
24
                  width: 100.0,
25 26
                  height: 100.0,
                ),
27
              ),
28 29 30
              Step(
                title: Text('Step 2'),
                content: SizedBox(
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
                  width: 100.0,
                  height: 100.0,
                ),
              ),
            ],
          ),
        ),
      ),
    );
    await tester.tap(find.text('Step 2'));
    expect(index, 1);
  });

  testWidgets('Stepper expansion test', (WidgetTester tester) async {
    await tester.pumpWidget(
46 47 48 49
      MaterialApp(
        home: Center(
          child: Material(
            child: Stepper(
50
              steps: const <Step>[
51 52 53
                Step(
                  title: Text('Step 1'),
                  content: SizedBox(
54 55 56 57
                    width: 100.0,
                    height: 100.0,
                  ),
                ),
58 59 60
                Step(
                  title: Text('Step 2'),
                  content: SizedBox(
61 62 63 64 65 66 67 68 69
                    width: 200.0,
                    height: 200.0,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
70 71 72 73 74 75
    );

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

    await tester.pumpWidget(
76 77 78 79
      MaterialApp(
        home: Center(
          child: Material(
            child: Stepper(
80
              currentStep: 1,
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 107 108 109 110 111 112
    );

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

  testWidgets('Stepper horizontal size test', (WidgetTester tester) async {
    await tester.pumpWidget(
113 114 115 116
      MaterialApp(
        home: Center(
          child: Material(
            child: Stepper(
117
              type: StepperType.horizontal,
118
              steps: const <Step>[
119 120 121
                Step(
                  title: Text('Step 1'),
                  content: SizedBox(
122 123 124 125 126 127 128 129 130
                    width: 100.0,
                    height: 100.0,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
131 132
    );

133
    final RenderBox box = tester.renderObject(find.byType(Stepper));
134 135 136 137 138
    expect(box.size.height, 600.0);
  });

  testWidgets('Stepper visibility test', (WidgetTester tester) async {
    await tester.pumpWidget(
139 140 141
      MaterialApp(
        home: Material(
          child: Stepper(
142
            type: StepperType.horizontal,
143
            steps: const <Step>[
144 145 146
              Step(
                title: Text('Step 1'),
                content: Text('A'),
147
              ),
148 149 150
              Step(
                title: Text('Step 2'),
                content: Text('B'),
151 152 153 154 155
              ),
            ],
          ),
        ),
      ),
156 157 158 159 160 161
    );

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

    await tester.pumpWidget(
162 163 164
      MaterialApp(
        home: Material(
          child: Stepper(
165 166
            currentStep: 1,
            type: StepperType.horizontal,
167
            steps: const <Step>[
168 169 170
              Step(
                title: Text('Step 1'),
                content: Text('A'),
171
              ),
172 173 174
              Step(
                title: Text('Step 2'),
                content: Text('B'),
175 176 177 178 179
              ),
            ],
          ),
        ),
      ),
180 181 182 183 184 185 186 187 188 189 190
    );

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

  testWidgets('Stepper button test', (WidgetTester tester) async {
    bool continuePressed = false;
    bool cancelPressed = false;

    await tester.pumpWidget(
191 192 193
      MaterialApp(
        home: Material(
          child: Stepper(
194 195 196 197 198 199 200
            type: StepperType.horizontal,
            onStepContinue: () {
              continuePressed = true;
            },
            onStepCancel: () {
              cancelPressed = true;
            },
201
            steps: const <Step>[
202 203 204
              Step(
                title: Text('Step 1'),
                content: SizedBox(
205 206 207 208
                  width: 100.0,
                  height: 100.0,
                ),
              ),
209 210 211
              Step(
                title: Text('Step 2'),
                content: SizedBox(
212 213 214 215 216 217 218 219
                  width: 200.0,
                  height: 200.0,
                ),
              ),
            ],
          ),
        ),
      ),
220 221 222 223 224 225 226 227 228 229 230 231 232
    );

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

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

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

    await tester.pumpWidget(
233 234 235
      MaterialApp(
        home: Material(
          child: Stepper(
236 237 238
            onStepTapped: (int i) {
              index = i;
            },
239
            steps: const <Step>[
240 241 242
              Step(
                title: Text('Step 1'),
                content: SizedBox(
243 244 245 246
                  width: 100.0,
                  height: 100.0,
                ),
              ),
247 248
              Step(
                title: Text('Step 2'),
249
                state: StepState.disabled,
250
                content: SizedBox(
251 252 253 254 255 256 257 258
                  width: 100.0,
                  height: 100.0,
                ),
              ),
            ],
          ),
        ),
      ),
259 260 261 262 263 264 265 266
    );

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

  testWidgets('Stepper scroll test', (WidgetTester tester) async {
    await tester.pumpWidget(
267 268 269
      MaterialApp(
        home: Material(
          child: Stepper(
270
            steps: const <Step>[
271 272 273
              Step(
                title: Text('Step 1'),
                content: SizedBox(
274 275 276 277
                  width: 100.0,
                  height: 300.0,
                ),
              ),
278 279 280
              Step(
                title: Text('Step 2'),
                content: SizedBox(
281 282 283 284
                  width: 100.0,
                  height: 300.0,
                ),
              ),
285 286 287
              Step(
                title: Text('Step 3'),
                content: SizedBox(
288 289 290 291 292 293 294 295
                  width: 100.0,
                  height: 100.0,
                ),
              ),
            ],
          ),
        ),
      ),
296 297
    );

298
    final ScrollableState scrollableState = tester.firstState(find.byType(Scrollable));
299
    expect(scrollableState.position.pixels, 0.0);
300 301 302

    await tester.tap(find.text('Step 3'));
    await tester.pumpWidget(
303 304 305
      MaterialApp(
        home: Material(
          child: Stepper(
306
            currentStep: 2,
307
            steps: const <Step>[
308 309 310
              Step(
                title: Text('Step 1'),
                content: SizedBox(
311
                  width: 100.0,
312 313
                  height: 300.0,
                ),
314
              ),
315 316 317
              Step(
                title: Text('Step 2'),
                content: SizedBox(
318
                  width: 100.0,
319 320 321
                  height: 300.0,
                ),
              ),
322 323 324
              Step(
                title: Text('Step 3'),
                content: SizedBox(
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
                  width: 100.0,
                  height: 100.0,
                ),
              ),
            ],
          ),
        ),
      ),
    );

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

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

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

372 373 374 375 376 377 378 379 380 381 382
  testWidgets('Stepper custom controls test', (WidgetTester tester) async {
    bool continuePressed = false;
    void setContinue() {
      continuePressed = true;
    }

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

383
    Widget builder(BuildContext context, ControlsDetails details) {
384 385 386 387 388 389 390
      return Container(
        margin: const EdgeInsets.only(top: 16.0),
        child: ConstrainedBox(
          constraints: const BoxConstraints.tightFor(height: 48.0),
          child: Row(
            children: <Widget>[
              TextButton(
391
                onPressed: details.onStepContinue,
392 393 394 395 396
                child: const Text('Let us continue!'),
              ),
              Container(
                margin: const EdgeInsetsDirectional.only(start: 8.0),
                child: TextButton(
397
                  onPressed: details.onStepCancel,
398
                  child: const Text('Cancel This!'),
399
                ),
400 401
              ),
            ],
402
          ),
403 404 405
        ),
      );
    }
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

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

451 452 453 454 455 456 457 458 459 460 461 462 463 464
testWidgets('Stepper custom indexed controls test', (WidgetTester tester) async {

    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.
465
      if (!details.isActive) {
466
        return Container();
467
      }
468 469 470 471 472 473 474 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 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545

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

546 547
  testWidgets('Stepper error test', (WidgetTester tester) async {
    await tester.pumpWidget(
548 549 550 551
      MaterialApp(
        home: Center(
          child: Material(
            child: Stepper(
552
              steps: const <Step>[
553 554
                Step(
                  title: Text('A'),
555
                  state: StepState.error,
556
                  content: SizedBox(
557 558 559 560 561 562 563 564 565
                    width: 100.0,
                    height: 100.0,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
566 567 568 569
    );

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

571
  testWidgets('Nested stepper error test', (WidgetTester tester) async {
572 573
    late FlutterErrorDetails errorDetails;
    final FlutterExceptionHandler? oldHandler = FlutterError.onError;
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
    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'),
                      ),
                    ],
597
                  ),
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
                ),
                const Step(
                  title: Text('Step 1'),
                  content: Text('A'),
                ),
              ],
            ),
          ),
        ),
      );
    } finally {
      FlutterError.onError = oldHandler;
    }

    expect(errorDetails.stack, isNotNull);
    // Check the ErrorDetails without the stack trace
614 615
    final String fullErrorMessage = errorDetails.toString();
    final List<String> lines = fullErrorMessage.split('\n');
616 617
    // The lines in the middle of the error message contain the stack trace
    // which will change depending on where the test is run.
618 619 620 621 622 623
    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'
624
      'The following assertion was thrown building Stepper(',
625 626 627 628 629 630 631 632
    ));
    // 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'
633
      'https://material.io/archive/guidelines/components/steppers.html#steppers-usage',
634
    ));
635 636
  });

637 638 639
  ///https://github.com/flutter/flutter/issues/16920
  testWidgets('Stepper icons size test', (WidgetTester tester) async {
    await tester.pumpWidget(
640
      MaterialApp(
641
        home: Material(
642
          child: Stepper(
643
            steps: const <Step>[
644 645
              Step(
                title: Text('A'),
646
                state: StepState.editing,
647
                content: SizedBox(width: 100.0, height: 100.0),
648
              ),
649 650
              Step(
                title: Text('B'),
651
                state: StepState.complete,
652
                content: SizedBox(width: 100.0, height: 100.0),
653 654 655 656 657 658 659 660 661 662 663 664 665
              ),
            ],
          ),
        ),
      ),
    );

    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)));
  });
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698

  testWidgets('Stepper physics scroll error test', (WidgetTester tester) async {
    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);
  });
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717

  testWidgets("Vertical Stepper can't be focused when disabled.", (WidgetTester tester) async {
    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();

718
    final FocusNode disabledNode = Focus.of(tester.element(find.text('Step 0')), scopeOk: true);
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
    disabledNode.requestFocus();
    await tester.pump();
    expect(disabledNode.hasPrimaryFocus, isFalse);
  });

  testWidgets("Horizontal Stepper can't be focused when disabled.", (WidgetTester tester) async {
    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();

743
    final FocusNode disabledNode = Focus.of(tester.element(find.text('Step 0')), scopeOk: true);
744 745 746 747
    disabledNode.requestFocus();
    await tester.pump();
    expect(disabledNode.hasPrimaryFocus, isFalse);
  });
748 749 750 751 752 753 754 755 756 757 758 759 760 761

  testWidgets('Stepper header title should not overflow', (WidgetTester tester) async {
    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),
762
                    content: Text('Text content'),
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );

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

  testWidgets('Stepper header subtitle should not overflow', (WidgetTester tester) async {
    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),
789
                    content: Text('Text content'),
790 791 792 793 794 795 796 797 798 799 800
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );

    expect(tester.takeException(), isNull);
  });
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823

  testWidgets('Stepper enabled button styles', (WidgetTester tester) async {
    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>(
824
        find.descendant(of: find.widgetWithText(TextButton, label), matching: find.byType(Material)),
825 826 827 828 829 830 831 832 833
      );
    }

    const OutlinedBorder buttonShape = RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(2)));
    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);

    await tester.pumpWidget(buildFrame(ThemeData.light()));

834 835
    expect(buttonMaterial('CONTINUE').color!.value, 0xff2196f3);
    expect(buttonMaterial('CONTINUE').textStyle!.color!.value, 0xffffffff);
836 837 838
    expect(buttonMaterial('CONTINUE').shape, buttonShape);
    expect(tester.getRect(find.widgetWithText(TextButton, 'CONTINUE')), continueButtonRect);

839 840
    expect(buttonMaterial('CANCEL').color!.value, 0);
    expect(buttonMaterial('CANCEL').textStyle!.color!.value, 0x8a000000);
841 842 843 844 845 846
    expect(buttonMaterial('CANCEL').shape, buttonShape);
    expect(tester.getRect(find.widgetWithText(TextButton, 'CANCEL')), cancelButtonRect);

    await tester.pumpWidget(buildFrame(ThemeData.dark()));
    await tester.pumpAndSettle(); // Complete the theme animation.

847 848
    expect(buttonMaterial('CONTINUE').color!.value, 0);
    expect(buttonMaterial('CONTINUE').textStyle!.color!.value,  0xffffffff);
849 850 851
    expect(buttonMaterial('CONTINUE').shape, buttonShape);
    expect(tester.getRect(find.widgetWithText(TextButton, 'CONTINUE')), continueButtonRect);

852 853
    expect(buttonMaterial('CANCEL').color!.value, 0);
    expect(buttonMaterial('CANCEL').textStyle!.color!.value, 0xb3ffffff);
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
    expect(buttonMaterial('CANCEL').shape, buttonShape);
    expect(tester.getRect(find.widgetWithText(TextButton, 'CANCEL')), cancelButtonRect);
  });

  testWidgets('Stepper disabled button styles', (WidgetTester tester) async {
    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>(
878
        find.descendant(of: find.widgetWithText(TextButton, label), matching: find.byType(Material)),
879 880 881 882 883
      );
    }

    await tester.pumpWidget(buildFrame(ThemeData.light()));

884 885
    expect(buttonMaterial('CONTINUE').color!.value, 0);
    expect(buttonMaterial('CONTINUE').textStyle!.color!.value, 0x61000000);
886

887 888
    expect(buttonMaterial('CANCEL').color!.value, 0);
    expect(buttonMaterial('CANCEL').textStyle!.color!.value, 0x61000000);
889 890 891 892

    await tester.pumpWidget(buildFrame(ThemeData.dark()));
    await tester.pumpAndSettle(); // Complete the theme animation.

893 894
    expect(buttonMaterial('CONTINUE').color!.value, 0);
    expect(buttonMaterial('CONTINUE').textStyle!.color!.value, 0x61ffffff);
895

896 897
    expect(buttonMaterial('CANCEL').color!.value, 0);
    expect(buttonMaterial('CANCEL').textStyle!.color!.value, 0x61ffffff);
898
  });
TheBirb's avatar
TheBirb committed
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927

  testWidgets('Vertical and Horizontal Stepper physics test', (WidgetTester tester) async {
    const ScrollPhysics physics = NeverScrollableScrollPhysics();

    for(final StepperType type in StepperType.values) {
      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);
    }
  });
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957

  testWidgets('Stepper horizontal size test', (WidgetTester tester) async {
    // 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;
958
    await tester.pumpWidget(buildFrame(brightness: Brightness.light));
959 960 961 962 963 964 965
    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;
966
    await tester.pumpWidget(buildFrame(brightness: Brightness.dark));
967 968 969 970 971 972
    await tester.pumpAndSettle();
    expect(circleFillColor(), dark.secondary);
    await tester.pumpWidget(buildFrame(isActive: false, brightness: Brightness.dark));
    await tester.pumpAndSettle();
    expect(circleFillColor(), dark.background);
  });
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 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

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

   testWidgets('Stepper with default elevation', (WidgetTester tester) async {

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

1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 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
  testWidgets('Stepper horizontal preserves state', (WidgetTester tester) async {
    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);
  });
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
       testWidgets('Stepper custom margin', (WidgetTester tester) async {

      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));
1146
   });
1147 1148 1149

  testWidgets('Stepper with Alternative Label', (WidgetTester tester) async {
    int index = 0;
1150 1151 1152
    late TextStyle bodyLargeStyle;
    late TextStyle bodyMediumStyle;
    late TextStyle bodySmallStyle;
1153 1154 1155 1156 1157 1158

    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: StatefulBuilder(
              builder: (BuildContext context, StateSetter setState) {
1159 1160 1161
            bodyLargeStyle = Theme.of(context).textTheme.bodyText1!;
            bodyMediumStyle = Theme.of(context).textTheme.bodyText2!;
            bodySmallStyle = Theme.of(context).textTheme.caption!;
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
            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'),
1174
                  label: Text('Label 1', style: Theme.of(context).textTheme.bodySmall),
1175 1176 1177 1178
                ),
                Step(
                  title: const Text('Title 2'),
                  content: const Text('Content 2'),
1179
                  label: Text('Label 2', style: Theme.of(context).textTheme.bodyLarge),
1180 1181 1182 1183
                ),
                Step(
                  title: const Text('Title 3'),
                  content: const Text('Content 3'),
1184
                  label: Text('Label 3', style: Theme.of(context).textTheme.bodyMedium),
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
                ),
              ],
            );
          }),
        ),
      ),
    );

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

1199 1200
    expect(bodySmallStyle, label1TextWidget.style);
    expect(bodyMediumStyle, label3TextWidget.style);
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211

    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}'));
1212
    expect(bodySmallStyle, selectedLabelTextWidget.style);
1213 1214
    nextLabelTextWidget =
        tester.widget<Text>(find.text('Label ${index + 2}'));
1215
    expect(bodyLargeStyle, nextLabelTextWidget.style);
1216 1217 1218 1219 1220 1221 1222 1223 1224


    // 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}'));
1225
    expect(bodyLargeStyle, selectedLabelTextWidget.style);
1226 1227 1228

    nextLabelTextWidget =
        tester.widget<Text>(find.text('Label ${index + 2}'));
1229
    expect(bodyMediumStyle, nextLabelTextWidget.style);
1230
  });
1231
}
1232 1233

class _TappableColorWidget extends StatefulWidget {
1234
  const _TappableColorWidget({required this.tappedColor, required this.untappedColor, super.key,});
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269

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