expansion_tile_test.dart 53 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 6 7 8 9
// This file is run as part of a reduced test set in CI on Mac and Windows
// machines.
@Tags(<String>['reduced-test-set'])
library;

10
import 'package:flutter/material.dart';
11
import 'package:flutter/rendering.dart';
12
import 'package:flutter_test/flutter_test.dart';
13

14
class TestIcon extends StatefulWidget {
15
  const TestIcon({super.key});
16 17 18 19 20 21

  @override
  TestIconState createState() => TestIconState();
}

class TestIconState extends State<TestIcon> {
22
  late IconThemeData iconTheme;
23 24 25 26 27 28 29 30 31

  @override
  Widget build(BuildContext context) {
    iconTheme = IconTheme.of(context);
    return const Icon(Icons.expand_more);
  }
}

class TestText extends StatefulWidget {
32
  const TestText(this.text, {super.key});
33 34 35 36 37 38 39 40

  final String text;

  @override
  TestTextState createState() => TestTextState();
}

class TestTextState extends State<TestText> {
41
  late TextStyle textStyle;
42 43 44 45 46 47 48 49

  @override
  Widget build(BuildContext context) {
    textStyle = DefaultTextStyle.of(context).style;
    return Text(widget.text);
  }
}

50
void main() {
51 52 53 54
  const Color dividerColor = Color(0x1f333333);
  const Color foregroundColor = Colors.blueAccent;
  const Color unselectedWidgetColor = Colors.black54;
  const Color headerColor = Colors.black45;
55

56 57 58 59 60 61 62
  Material getMaterial(WidgetTester tester) {
    return tester.widget<Material>(find.descendant(
      of: find.byType(ExpansionTile),
      matching: find.byType(Material),
    ));
  }

63
  testWidgets('ExpansionTile initial state', (WidgetTester tester) async {
64
    final Key topKey = UniqueKey();
65
    final Key tileKey = UniqueKey();
66 67 68
    const Key expandedKey = PageStorageKey<String>('expanded');
    const Key collapsedKey = PageStorageKey<String>('collapsed');
    const Key defaultKey = PageStorageKey<String>('default');
69

70
    await tester.pumpWidget(MaterialApp(
71
      theme: ThemeData(dividerColor: dividerColor),
72 73 74
      home: Material(
        child: SingleChildScrollView(
          child: Column(
75
            children: <Widget>[
76 77
              ListTile(title: const Text('Top'), key: topKey),
              ExpansionTile(
78 79 80
                key: expandedKey,
                initiallyExpanded: true,
                title: const Text('Expanded'),
81
                backgroundColor: Colors.red,
82
                children: <Widget>[
83
                  ListTile(
84
                    key: tileKey,
85 86 87
                    title: const Text('0'),
                  ),
                ],
88
              ),
89
              ExpansionTile(
90 91 92
                key: collapsedKey,
                title: const Text('Collapsed'),
                children: <Widget>[
93
                  ListTile(
94
                    key: tileKey,
95 96 97
                    title: const Text('0'),
                  ),
                ],
98
              ),
99
              const ExpansionTile(
100
                key: defaultKey,
101 102 103
                title: Text('Default'),
                children: <Widget>[
                  ListTile(title: Text('0')),
104 105 106 107 108 109
                ],
              ),
            ],
          ),
        ),
      ),
110 111 112
    ));

    double getHeight(Key key) => tester.getSize(find.byKey(key)).height;
113
    DecoratedBox getDecoratedBox(Key key) => tester.firstWidget(find.descendant(
114
      of: find.byKey(key),
115
      matching: find.byType(DecoratedBox),
116
    ));
117 118 119 120 121

    expect(getHeight(topKey), getHeight(expandedKey) - getHeight(tileKey) - 2.0);
    expect(getHeight(topKey), getHeight(collapsedKey) - 2.0);
    expect(getHeight(topKey), getHeight(defaultKey) - 2.0);

122
    ShapeDecoration expandedContainerDecoration = getDecoratedBox(expandedKey).decoration as ShapeDecoration;
123
    expect(expandedContainerDecoration.color, Colors.red);
124 125
    expect((expandedContainerDecoration.shape as Border).top.color, dividerColor);
    expect((expandedContainerDecoration.shape as Border).bottom.color, dividerColor);
126

127
    ShapeDecoration collapsedContainerDecoration = getDecoratedBox(collapsedKey).decoration as ShapeDecoration;
128
    expect(collapsedContainerDecoration.color, Colors.transparent);
129 130
    expect((collapsedContainerDecoration.shape as Border).top.color, Colors.transparent);
    expect((collapsedContainerDecoration.shape as Border).bottom.color, Colors.transparent);
131

132 133 134 135 136
    await tester.tap(find.text('Expanded'));
    await tester.tap(find.text('Collapsed'));
    await tester.tap(find.text('Default'));

    await tester.pump();
137 138 139

    // Pump to the middle of the animation for expansion.
    await tester.pump(const Duration(milliseconds: 100));
140
    final ShapeDecoration collapsingContainerDecoration = getDecoratedBox(collapsedKey).decoration as ShapeDecoration;
141
    expect(collapsingContainerDecoration.color, Colors.transparent);
142 143
    expect((collapsingContainerDecoration.shape as Border).top.color, const Color(0x15222222));
    expect((collapsingContainerDecoration.shape as Border).bottom.color, const Color(0x15222222));
144 145

    // Pump all the way to the end now.
146 147 148 149 150
    await tester.pump(const Duration(seconds: 1));

    expect(getHeight(topKey), getHeight(expandedKey) - 2.0);
    expect(getHeight(topKey), getHeight(collapsedKey) - getHeight(tileKey) - 2.0);
    expect(getHeight(topKey), getHeight(defaultKey) - getHeight(tileKey) - 2.0);
151 152

    // Expanded should be collapsed now.
153
    expandedContainerDecoration = getDecoratedBox(expandedKey).decoration as ShapeDecoration;
154
    expect(expandedContainerDecoration.color, Colors.transparent);
155 156
    expect((expandedContainerDecoration.shape as Border).top.color, Colors.transparent);
    expect((expandedContainerDecoration.shape as Border).bottom.color, Colors.transparent);
157 158

    // Collapsed should be expanded now.
159
    collapsedContainerDecoration = getDecoratedBox(collapsedKey).decoration as ShapeDecoration;
160
    expect(collapsedContainerDecoration.color, Colors.transparent);
161 162
    expect((collapsedContainerDecoration.shape as Border).top.color, dividerColor);
    expect((collapsedContainerDecoration.shape as Border).bottom.color, dividerColor);
Dan Field's avatar
Dan Field committed
163
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
164

165
  testWidgets('ExpansionTile Theme dependencies', (WidgetTester tester) async {
166 167 168 169 170 171 172 173
    final Key expandedTitleKey = UniqueKey();
    final Key collapsedTitleKey = UniqueKey();
    final Key expandedIconKey = UniqueKey();
    final Key collapsedIconKey = UniqueKey();

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
174
          useMaterial3: false,
175 176
          colorScheme: ColorScheme.fromSwatch().copyWith(primary: foregroundColor),
          unselectedWidgetColor: unselectedWidgetColor,
177
          textTheme: const TextTheme(titleMedium: TextStyle(color: headerColor)),
178 179 180 181 182 183 184 185 186 187 188
        ),
        home: Material(
          child: SingleChildScrollView(
            child: Column(
              children: <Widget>[
                const ListTile(title: Text('Top')),
                ExpansionTile(
                  initiallyExpanded: true,
                  title: TestText('Expanded', key: expandedTitleKey),
                  backgroundColor: Colors.red,
                  trailing: TestIcon(key: expandedIconKey),
189
                  children: const <Widget>[ListTile(title: Text('0'))],
190 191 192 193
                ),
                ExpansionTile(
                  title: TestText('Collapsed', key: collapsedTitleKey),
                  trailing: TestIcon(key: collapsedIconKey),
194
                  children: const <Widget>[ListTile(title: Text('0'))],
195 196 197 198 199 200 201 202
                ),
              ],
            ),
          ),
        ),
      ),
    );

203 204
    Color iconColor(Key key) => tester.state<TestIconState>(find.byKey(key)).iconTheme.color!;
    Color textColor(Key key) => tester.state<TestTextState>(find.byKey(key)).textStyle.color!;
205

206 207 208 209
    expect(textColor(expandedTitleKey), foregroundColor);
    expect(textColor(collapsedTitleKey), headerColor);
    expect(iconColor(expandedIconKey), foregroundColor);
    expect(iconColor(collapsedIconKey), unselectedWidgetColor);
210 211 212 213 214 215 216 217

    // Tap both tiles to change their state: collapse and extend respectively
    await tester.tap(find.text('Expanded'));
    await tester.tap(find.text('Collapsed'));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
    await tester.pump(const Duration(seconds: 1));

218 219 220 221
    expect(textColor(expandedTitleKey), headerColor);
    expect(textColor(collapsedTitleKey), foregroundColor);
    expect(iconColor(expandedIconKey), unselectedWidgetColor);
    expect(iconColor(collapsedIconKey), foregroundColor);
Dan Field's avatar
Dan Field committed
222
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
223

224
  testWidgets('ExpansionTile subtitle', (WidgetTester tester) async {
225 226 227 228 229 230 231 232 233 234 235 236 237 238
    await tester.pumpWidget(
      const MaterialApp(
        home: Scaffold(
          body: ExpansionTile(
            title: Text('Title'),
            subtitle: Text('Subtitle'),
            children: <Widget>[ListTile(title: Text('0'))],
          ),
        ),
      ),
    );

    expect(find.text('Subtitle'), findsOneWidget);
  });
239

240
  testWidgets('ExpansionTile maintainState', (WidgetTester tester) async {
241 242 243 244
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
          platform: TargetPlatform.iOS,
245
          dividerColor: dividerColor,
246
        ),
247
        home: const Material(
248 249
          child: SingleChildScrollView(
            child: Column(
250
              children: <Widget>[
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
                ExpansionTile(
                  title: Text('Tile 1'),
                  maintainState: true,
                  children: <Widget>[
                    Text('Maintaining State'),
                  ],
                ),
                ExpansionTile(
                  title: Text('Title 2'),
                  children: <Widget>[
                    Text('Discarding State'),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
270 271 272 273 274 275 276 277

     // This text should be offstage while ExpansionTile collapsed
     expect(find.text('Maintaining State', skipOffstage: false), findsOneWidget);
     expect(find.text('Maintaining State'), findsNothing);
     // This text shouldn't be there while ExpansionTile collapsed
     expect(find.text('Discarding State'), findsNothing);
   });

278
  testWidgets('ExpansionTile padding test', (WidgetTester tester) async {
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: Center(
          child: ExpansionTile(
            title: Text('Hello'),
            tilePadding: EdgeInsets.fromLTRB(8, 12, 4, 10),
          ),
        ),
      ),
    ));

    final Rect titleRect = tester.getRect(find.text('Hello'));
    final Rect trailingRect = tester.getRect(find.byIcon(Icons.expand_more));
    final Rect listTileRect = tester.getRect(find.byType(ListTile));
    final Rect tallerWidget = titleRect.height > trailingRect.height ? titleRect : trailingRect;

    // Check the positions of title and trailing Widgets, after padding is applied.
    expect(listTileRect.left, titleRect.left - 8);
    expect(listTileRect.right, trailingRect.right + 4);

    // Calculate the remaining height of ListTile from the default height.
    final double remainingHeight = 56 - tallerWidget.height;
    expect(listTileRect.top, tallerWidget.top - remainingHeight / 2 - 12);
    expect(listTileRect.bottom, tallerWidget.bottom + remainingHeight / 2 + 10);
  });
304

305
  testWidgets('ExpansionTile expandedAlignment test', (WidgetTester tester) async {
306
    await tester.pumpWidget(const MaterialApp(
307 308 309
      home: Material(
        child: Center(
          child: ExpansionTile(
310
            title: Text('title'),
311 312
            expandedAlignment: Alignment.centerLeft,
            children: <Widget>[
313 314
              SizedBox(height: 100, width: 100),
              SizedBox(height: 100, width: 80),
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
            ],
          ),
        ),
      ),
    ));

    await tester.tap(find.text('title'));
    await tester.pumpAndSettle();

    final Rect columnRect = tester.getRect(find.byType(Column).last);

    // The expandedAlignment is used to define the alignment of the Column widget in
    // expanded tile, not the alignment of the children inside the Column.
    expect(columnRect.left, 0.0);
    // The width of the Column is the width of the largest child. The largest width
    // being 100.0, the offset of the right edge of Column from X-axis should be 100.0.
    expect(columnRect.right, 100.0);
  });

334
  testWidgets('ExpansionTile expandedCrossAxisAlignment test', (WidgetTester tester) async {
335 336 337
    const Key child0Key = Key('child0');
    const Key child1Key = Key('child1');

338 339 340 341 342
    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: Center(
          child: ExpansionTile(
            title: Text('title'),
343 344 345 346 347
            // Set the column's alignment to Alignment.centerRight to test CrossAxisAlignment
            // of children widgets. This helps distinguish the effect of expandedAlignment
            // and expandedCrossAxisAlignment later in the test.
            expandedAlignment: Alignment.centerRight,
            expandedCrossAxisAlignment: CrossAxisAlignment.start,
348
            children: <Widget>[
349 350
              SizedBox(height: 100, width: 100, key: child0Key),
              SizedBox(height: 100, width: 80, key: child1Key),
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
            ],
          ),
        ),
      ),
    ));

    await tester.tap(find.text('title'));
    await tester.pumpAndSettle();

    final Rect columnRect = tester.getRect(find.byType(Column).last);
    final Rect child0Rect = tester.getRect(find.byKey(child0Key));
    final Rect child1Rect = tester.getRect(find.byKey(child1Key));

    // Since expandedAlignment is set to Alignment.centerRight, the column of children
    // should be aligned to the center right of the expanded tile. This provides confirmation
    // that the expandedCrossAxisAlignment.start is 700.0, where columnRect.left is.
    expect(columnRect.right, 800.0);
    // The width of the Column is the width of the largest child. The largest width
    // being 100.0, the offset of the left edge of Column from X-axis should be 700.0.
    expect(columnRect.left, 700.0);

    // Considering the value of expandedCrossAxisAlignment is CrossAxisAlignment.start,
    // the offset of the left edge of both the children from X-axis should be 700.0.
    expect(child0Rect.left, 700.0);
    expect(child1Rect.left, 700.0);
  });

378
  testWidgets('CrossAxisAlignment.baseline is not allowed', (WidgetTester tester) async {
379 380 381 382 383 384 385 386 387
    expect(
      () {
        MaterialApp(
          home: Material(
            child: ExpansionTile(
              initiallyExpanded: true,
              title: const Text('title'),
              expandedCrossAxisAlignment: CrossAxisAlignment.baseline,
            ),
388
          ),
389 390 391
        );
      },
      throwsA(isA<AssertionError>().having((AssertionError error) => error.toString(), '.toString()', contains(
392 393
        'CrossAxisAlignment.baseline is not supported since the expanded'
        ' children are aligned in a column, not a row. Try to use another constant.',
394 395
      ))),
    );
396 397
  });

398
  testWidgets('expandedCrossAxisAlignment and expandedAlignment default values', (WidgetTester tester) async {
399 400
    const Key child1Key = Key('child1');

401
    await tester.pumpWidget(const MaterialApp(
402 403 404
      home: Material(
        child: Center(
          child: ExpansionTile(
405
            title: Text('title'),
406
            children: <Widget>[
407 408
              SizedBox(height: 100, width: 100),
              SizedBox(height: 100, width: 80, key: child1Key),
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
            ],
          ),
        ),
      ),
    ));


    await tester.tap(find.text('title'));
    await tester.pumpAndSettle();

    final Rect columnRect = tester.getRect(find.byType(Column).last);
    final Rect child1Rect = tester.getRect(find.byKey(child1Key));

    // The default viewport size is Size(800, 600).
    // By default the value of extendedAlignment is Alignment.center, hence the offset
    // of left and right edges from x axis should be equal.
    expect(columnRect.left, 800 - columnRect.right);

    // By default the value of extendedCrossAxisAlignment is CrossAxisAlignment.center, hence
    // the offset of left and right edges from Column should be equal.
    expect(child1Rect.left - columnRect.left, columnRect.right - child1Rect.right);

  });

433
  testWidgets('childrenPadding default value', (WidgetTester tester) async {
434 435 436 437 438 439 440 441 442 443
    await tester.pumpWidget(
      const MaterialApp(
        home: Material(
          child: Center(
            child: ExpansionTile(
              title: Text('title'),
              children: <Widget>[
                SizedBox(height: 100, width: 100),
              ],
            ),
444 445 446
          ),
        ),
      ),
447
    );
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462

    await tester.tap(find.text('title'));
    await tester.pumpAndSettle();

    final Rect columnRect = tester.getRect(find.byType(Column).last);
    final Rect paddingRect = tester.getRect(find.byType(Padding).last);

    // By default, the value of childrenPadding is EdgeInsets.zero, hence offset
    // of all the edges from x-axis and y-axis should be equal for Padding and Column.
    expect(columnRect.top, paddingRect.top);
    expect(columnRect.left, paddingRect.left);
    expect(columnRect.right, paddingRect.right);
    expect(columnRect.bottom, paddingRect.bottom);
  });

463
  testWidgets('ExpansionTile childrenPadding test', (WidgetTester tester) async {
464 465 466 467 468 469 470 471 472 473 474
    await tester.pumpWidget(
      const MaterialApp(
        home: Material(
          child: Center(
            child: ExpansionTile(
              title: Text('title'),
              childrenPadding: EdgeInsets.fromLTRB(10, 8, 12, 4),
              children: <Widget>[
                SizedBox(height: 100, width: 100),
              ],
            ),
475 476 477
          ),
        ),
      ),
478
    );
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493

    await tester.tap(find.text('title'));
    await tester.pumpAndSettle();

    final Rect columnRect = tester.getRect(find.byType(Column).last);
    final Rect paddingRect = tester.getRect(find.byType(Padding).last);

    // Check the offset of all the edges from x-axis and y-axis after childrenPadding
    // is applied.
    expect(columnRect.left, paddingRect.left + 10);
    expect(columnRect.top, paddingRect.top + 8);
    expect(columnRect.right, paddingRect.right - 12);
    expect(columnRect.bottom, paddingRect.bottom - 4);
  });

494
  testWidgets('ExpansionTile.collapsedBackgroundColor', (WidgetTester tester) async {
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
    const Key expansionTileKey = Key('expansionTileKey');
    const Color backgroundColor = Colors.red;
    const Color collapsedBackgroundColor = Colors.brown;

    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: ExpansionTile(
          key: expansionTileKey,
          title: Text('Title'),
          backgroundColor: backgroundColor,
          collapsedBackgroundColor: collapsedBackgroundColor,
          children: <Widget>[
            SizedBox(height: 100, width: 100),
          ],
        ),
      ),
    ));

513
    ShapeDecoration shapeDecoration =  tester.firstWidget<DecoratedBox>(find.descendant(
514
      of: find.byKey(expansionTileKey),
515 516
      matching: find.byType(DecoratedBox),
    )).decoration as ShapeDecoration;
517

518
    expect(shapeDecoration.color, collapsedBackgroundColor);
519 520 521 522

    await tester.tap(find.text('Title'));
    await tester.pumpAndSettle();

523
    shapeDecoration =  tester.firstWidget<DecoratedBox>(find.descendant(
524
      of: find.byKey(expansionTileKey),
525 526
      matching: find.byType(DecoratedBox),
    )).decoration as ShapeDecoration;
527

528
    expect(shapeDecoration.color, backgroundColor);
529
  });
530

531
  testWidgets('ExpansionTile default iconColor, textColor', (WidgetTester tester) async {
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
    final ThemeData theme = ThemeData(useMaterial3: true);

    await tester.pumpWidget(MaterialApp(
      theme: theme,
      home: const Material(
        child: ExpansionTile(
          title: TestText('title'),
          trailing: TestIcon(),
          children: <Widget>[
            SizedBox(height: 100, width: 100),
          ],
        ),
      ),
    ));

    Color getIconColor() => tester.state<TestIconState>(find.byType(TestIcon)).iconTheme.color!;
    Color getTextColor() => tester.state<TestTextState>(find.byType(TestText)).textStyle.color!;

    expect(getIconColor(), theme.colorScheme.onSurfaceVariant);
    expect(getTextColor(), theme.colorScheme.onSurface);

    await tester.tap(find.text('title'));
    await tester.pumpAndSettle();

    expect(getIconColor(), theme.colorScheme.primary);
    expect(getTextColor(), theme.colorScheme.onSurface);
  });

560
  testWidgets('ExpansionTile iconColor, textColor', (WidgetTester tester) async {
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
    // Regression test for https://github.com/flutter/flutter/pull/78281

    const Color iconColor = Color(0xff00ff00);
    const Color collapsedIconColor = Color(0xff0000ff);
    const Color textColor = Color(0xff00ffff);
    const Color collapsedTextColor = Color(0xffff00ff);

    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: ExpansionTile(
          iconColor: iconColor,
          collapsedIconColor: collapsedIconColor,
          textColor: textColor,
          collapsedTextColor: collapsedTextColor,
          title: TestText('title'),
          trailing: TestIcon(),
          children: <Widget>[
            SizedBox(height: 100, width: 100),
          ],
        ),
      ),
    ));

    Color getIconColor() => tester.state<TestIconState>(find.byType(TestIcon)).iconTheme.color!;
    Color getTextColor() => tester.state<TestTextState>(find.byType(TestText)).textStyle.color!;

    expect(getIconColor(), collapsedIconColor);
    expect(getTextColor(), collapsedTextColor);

    await tester.tap(find.text('title'));
    await tester.pumpAndSettle();

    expect(getIconColor(), iconColor);
    expect(getTextColor(), textColor);
  });
596

597
  testWidgets('ExpansionTile Border', (WidgetTester tester) async {
598 599 600 601 602 603 604 605 606 607
    const Key expansionTileKey = PageStorageKey<String>('expansionTile');

    const Border collapsedShape = Border(
      top: BorderSide(color: Colors.blue),
      bottom: BorderSide(color: Colors.green)
    );
    final Border shape = Border.all(color: Colors.red);

    await tester.pumpWidget(MaterialApp(
      home: Material(
608 609 610 611 612 613 614 615 616 617
        child: ExpansionTile(
          key: expansionTileKey,
          title: const Text('ExpansionTile'),
          collapsedShape: collapsedShape,
          shape: shape,
          children: const <Widget>[
            ListTile(
              title: Text('0'),
            ),
          ],
618 619 620 621
        ),
      ),
    ));

622 623 624 625 626 627 628
    // When a custom shape is provided, ExpansionTile will use the
    // Material widget to draw the shape and background color
    // instead of a Container.
    Material material = getMaterial(tester);
    // ExpansionTile should be collapsed initially.
    expect(material.shape, collapsedShape);
    expect(material.clipBehavior, Clip.antiAlias);
629 630 631 632

    await tester.tap(find.text('ExpansionTile'));
    await tester.pumpAndSettle();

633 634 635 636
    // ExpansionTile should be Expanded now.
    material = getMaterial(tester);
    expect(material.shape, shape);
    expect(material.clipBehavior, Clip.antiAlias);
637 638
  });

639
  testWidgets('ExpansionTile platform controlAffinity test', (WidgetTester tester) async {
640 641 642 643 644 645 646 647 648 649 650 651 652
    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: ExpansionTile(
          title: Text('Title'),
        ),
      ),
    ));

    final ListTile listTile = tester.widget(find.byType(ListTile));
    expect(listTile.leading, isNull);
    expect(listTile.trailing.runtimeType, RotationTransition);
  });

653
  testWidgets('ExpansionTile trailing controlAffinity test', (WidgetTester tester) async {
654 655 656 657 658 659 660 661 662 663 664 665 666 667
    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: ExpansionTile(
          title: Text('Title'),
          controlAffinity: ListTileControlAffinity.trailing,
        ),
      ),
    ));

    final ListTile listTile = tester.widget(find.byType(ListTile));
    expect(listTile.leading, isNull);
    expect(listTile.trailing.runtimeType, RotationTransition);
  });

668
  testWidgets('ExpansionTile leading controlAffinity test', (WidgetTester tester) async {
669 670 671 672 673 674 675 676 677 678 679 680 681 682
    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: ExpansionTile(
          title: Text('Title'),
          controlAffinity: ListTileControlAffinity.leading,
        ),
      ),
    ));

    final ListTile listTile = tester.widget(find.byType(ListTile));
    expect(listTile.leading.runtimeType, RotationTransition);
    expect(listTile.trailing, isNull);
  });

683
  testWidgets('ExpansionTile override rotating icon test', (WidgetTester tester) async {
684 685 686 687 688 689 690 691 692 693 694 695 696 697
    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: ExpansionTile(
          title: Text('Title'),
          leading: Icon(Icons.info),
          controlAffinity: ListTileControlAffinity.leading,
        ),
      ),
    ));

    final ListTile listTile = tester.widget(find.byType(ListTile));
    expect(listTile.leading.runtimeType, Icon);
    expect(listTile.trailing, isNull);
  });
698

699
  testWidgets('Nested ListTile Semantics', (WidgetTester tester) async {
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
    tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
    final SemanticsHandle handle = tester.ensureSemantics();

    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: Column(
          children: <Widget>[
            ExpansionTile(
              title: Text('First Expansion Tile'),
            ),
            ExpansionTile(
              initiallyExpanded: true,
              title: Text('Second Expansion Tile'),
            ),
          ],
        ),
      ),
    ));

    await tester.pumpAndSettle();

    // Focus the first ExpansionTile.
    tester.binding.focusManager.primaryFocus?.nextFocus();
    await tester.pumpAndSettle();

    // The first list tile is focused.
    expect(
      tester.getSemantics(find.byType(ListTile).first),
      matchesSemantics(
        hasTapAction: true,
        hasEnabledState: true,
        isEnabled: true,
        isFocused: true,
        isFocusable: true,
        label: 'First Expansion Tile',
        textDirection: TextDirection.ltr,
      ),
    );

    // The first list tile is not focused.
    expect(
      tester.getSemantics(find.byType(ListTile).last),
      matchesSemantics(
        hasTapAction: true,
        hasEnabledState: true,
        isEnabled: true,
        isFocusable: true,
        label: 'Second Expansion Tile',
        textDirection: TextDirection.ltr,
      ),
    );
    handle.dispose();
  });

754
  testWidgets('ExpansionTile Semantics announcement', (WidgetTester tester) async {
755 756 757 758 759 760 761 762 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 789 790 791 792
    final SemanticsHandle handle = tester.ensureSemantics();
    const DefaultMaterialLocalizations localizations = DefaultMaterialLocalizations();
    await tester.pumpWidget(
      const MaterialApp(
        home: Material(
          child: ExpansionTile(
            title: Text('Title'),
            children: <Widget>[
              SizedBox(height: 100, width: 100),
            ],
          ),
        ),
      ),
    );

    // There is no semantics announcement without tap action.
    expect(tester.takeAnnouncements(), isEmpty);

    // Tap the title to expand ExpansionTile.
    await tester.tap(find.text('Title'));
    await tester.pumpAndSettle();

    // The announcement should be the opposite of the current state.
    // The ExpansionTile is expanded, so the announcement should be
    // "Expanded".
    expect(tester.takeAnnouncements().first.message, localizations.collapsedHint);

    // Tap the title to collapse ExpansionTile.
    await tester.tap(find.text('Title'));
    await tester.pumpAndSettle();

    // The announcement should be the opposite of the current state.
    // The ExpansionTile is collapsed, so the announcement should be
    // "Collapsed".
    expect(tester.takeAnnouncements().first.message, localizations.expandedHint);
    handle.dispose();
  });

793
  testWidgets('Semantics with the onTapHint is an ancestor of ListTile', (WidgetTester tester) async {
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
    // This is a regression test for https://github.com/flutter/flutter/pull/121624
    final SemanticsHandle handle = tester.ensureSemantics();
    const DefaultMaterialLocalizations localizations = DefaultMaterialLocalizations();

    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: Column(
          children: <Widget>[
            ExpansionTile(
              title: Text('First Expansion Tile'),
            ),
            ExpansionTile(
              initiallyExpanded: true,
              title: Text('Second Expansion Tile'),
            ),
          ],
        ),
      ),
    ));

    SemanticsNode semantics = tester.getSemantics(
      find.ancestor(
        of: find.byType(ListTile).first,
        matching: find.byType(Semantics),
      ).first,
    );
    expect(semantics, isNotNull);
    // The onTapHint is passed to semantics properties's hintOverrides.
    expect(semantics.hintOverrides, isNotNull);
    // The hint should be the opposite of the current state.
    // The first ExpansionTile is collapsed, so the hint should be
    // "double tap to expand".
    expect(semantics.hintOverrides!.onTapHint, localizations.expansionTileCollapsedTapHint);

    semantics = tester.getSemantics(
      find.ancestor(
        of: find.byType(ListTile).last,
        matching: find.byType(Semantics),
      ).first,
    );

    expect(semantics, isNotNull);
    // The onTapHint is passed to semantics properties's hintOverrides.
    expect(semantics.hintOverrides, isNotNull);
    // The hint should be the opposite of the current state.
    // The second ExpansionTile is expanded, so the hint should be
    // "double tap to collapse".
    expect(semantics.hintOverrides!.onTapHint, localizations.expansionTileExpandedTapHint);
    handle.dispose();
  });

845
  testWidgets('Semantics hint for iOS and macOS', (WidgetTester tester) async {
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
    final SemanticsHandle handle = tester.ensureSemantics();
    const DefaultMaterialLocalizations localizations = DefaultMaterialLocalizations();

    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: Column(
          children: <Widget>[
            ExpansionTile(
              title: Text('First Expansion Tile'),
            ),
            ExpansionTile(
              initiallyExpanded: true,
              title: Text('Second Expansion Tile'),
            ),
          ],
        ),
      ),
    ));

    SemanticsNode semantics = tester.getSemantics(
      find.ancestor(
        of: find.byType(ListTile).first,
        matching: find.byType(Semantics),
      ).first,
    );

    expect(semantics, isNotNull);
    expect(
      semantics.hint,
      '${localizations.expandedHint}\n ${localizations.expansionTileCollapsedHint}',
    );

    semantics = tester.getSemantics(
      find.ancestor(
        of: find.byType(ListTile).last,
        matching: find.byType(Semantics),
      ).first,
    );

    expect(semantics, isNotNull);
    expect(
      semantics.hint,
      '${localizations.collapsedHint}\n ${localizations.expansionTileExpandedHint}',
    );
    handle.dispose();
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS }));

893
  testWidgets('Collapsed ExpansionTile properties can be updated with setState', (WidgetTester tester) async {
894 895 896 897 898 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 928 929 930 931 932 933 934 935 936 937 938 939 940
    const Key expansionTileKey = Key('expansionTileKey');
    ShapeBorder collapsedShape = const RoundedRectangleBorder(
      borderRadius: BorderRadius.all(Radius.circular(4)),
    );
    Color collapsedTextColor = const Color(0xffffffff);
    Color collapsedBackgroundColor = const Color(0xffff0000);
    Color collapsedIconColor = const Color(0xffffffff);

    await tester.pumpWidget(MaterialApp(
      home: Material(
        child: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
            return Column(
              children: <Widget>[
                ExpansionTile(
                  key: expansionTileKey,
                  collapsedShape: collapsedShape,
                  collapsedTextColor: collapsedTextColor,
                  collapsedBackgroundColor: collapsedBackgroundColor,
                  collapsedIconColor: collapsedIconColor,
                  title: const TestText('title'),
                  trailing: const TestIcon(),
                  children: const <Widget>[
                    SizedBox(height: 100, width: 100),
                  ],
                ),
                // This button is used to update the ExpansionTile properties.
                FilledButton(
                  onPressed: () {
                    setState(() {
                      collapsedShape = const RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(16)),
                      );
                      collapsedTextColor = const Color(0xff000000);
                      collapsedBackgroundColor = const Color(0xffffff00);
                      collapsedIconColor = const Color(0xff000000);
                    });
                  },
                  child: const Text('Update collapsed properties'),
                ),
              ],
            );
          }
        ),
      ),
    ));

941 942 943 944
    // When a custom shape is provided, ExpansionTile will use the
    // Material widget to draw the shape and background color
    // instead of a Container.
    Material material = getMaterial(tester);
945 946

    // Test initial ExpansionTile properties.
947 948 949
    expect(material.shape, const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(4))));
    expect(material.color, const Color(0xffff0000));
    expect(material.clipBehavior, Clip.antiAlias);
950 951 952 953 954 955 956
    expect(tester.state<TestIconState>(find.byType(TestIcon)).iconTheme.color, const Color(0xffffffff));
    expect(tester.state<TestTextState>(find.byType(TestText)).textStyle.color, const Color(0xffffffff));

    // Tap the button to update the ExpansionTile properties.
    await tester.tap(find.text('Update collapsed properties'));
    await tester.pumpAndSettle();

957
    material = getMaterial(tester);
958 959

    // Test updated ExpansionTile properties.
960 961 962
    expect(material.shape, const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16))));
    expect(material.color, const Color(0xffffff00));
    expect(material.clipBehavior, Clip.antiAlias);
963 964 965 966
    expect(tester.state<TestIconState>(find.byType(TestIcon)).iconTheme.color, const Color(0xff000000));
    expect(tester.state<TestTextState>(find.byType(TestText)).textStyle.color, const Color(0xff000000));
  });

967
  testWidgets('Expanded ExpansionTile properties can be updated with setState', (WidgetTester tester) async {
968 969 970 971 972 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
    const Key expansionTileKey = Key('expansionTileKey');
    ShapeBorder shape = const RoundedRectangleBorder(
      borderRadius: BorderRadius.all(Radius.circular(12)),
    );
    Color textColor = const Color(0xff00ffff);
    Color backgroundColor = const Color(0xff0000ff);
    Color iconColor = const Color(0xff00ffff);

    await tester.pumpWidget(MaterialApp(
      home: Material(
        child: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
            return Column(
              children: <Widget>[
                ExpansionTile(
                  key: expansionTileKey,
                  shape: shape,
                  textColor: textColor,
                  backgroundColor: backgroundColor,
                  iconColor: iconColor,
                  title: const TestText('title'),
                  trailing: const TestIcon(),
                  children: const <Widget>[
                    SizedBox(height: 100, width: 100),
                  ],
                ),
                // This button is used to update the ExpansionTile properties.
                FilledButton(
                  onPressed: () {
                    setState(() {
                      shape = const RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(6)),
                      );
                      textColor = const Color(0xffffffff);
                      backgroundColor = const Color(0xff123456);
                      iconColor = const Color(0xffffffff);
                    });
                  },
                  child: const Text('Update collapsed properties'),
                ),
              ],
            );
          }
        ),
      ),
    ));

    // Tap to expand the ExpansionTile.
    await tester.tap(find.text('title'));
    await tester.pumpAndSettle();

1019 1020 1021 1022
    // When a custom shape is provided, ExpansionTile will use the
    // Material widget to draw the shape and background color
    // instead of a Container.
    Material material = getMaterial(tester);
1023 1024

    // Test initial ExpansionTile properties.
1025 1026 1027
    expect(material.shape, const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(12))));
    expect(material.color, const Color(0xff0000ff));
    expect(material.clipBehavior, Clip.antiAlias);
1028 1029 1030 1031 1032 1033 1034
    expect(tester.state<TestIconState>(find.byType(TestIcon)).iconTheme.color, const Color(0xff00ffff));
    expect(tester.state<TestTextState>(find.byType(TestText)).textStyle.color, const Color(0xff00ffff));

    // Tap the button to update the ExpansionTile properties.
    await tester.tap(find.text('Update collapsed properties'));
    await tester.pumpAndSettle();

1035
    material = getMaterial(tester);
1036 1037 1038 1039
    iconColor = tester.state<TestIconState>(find.byType(TestIcon)).iconTheme.color!;
    textColor = tester.state<TestTextState>(find.byType(TestText)).textStyle.color!;

    // Test updated ExpansionTile properties.
1040 1041 1042
    expect(material.shape, const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(6))));
    expect(material.color, const Color(0xff123456));
    expect(material.clipBehavior, Clip.antiAlias);
1043 1044 1045 1046
    expect(tester.state<TestIconState>(find.byType(TestIcon)).iconTheme.color, const Color(0xffffffff));
    expect(tester.state<TestTextState>(find.byType(TestText)).textStyle.color, const Color(0xffffffff));
  });

1047
  testWidgets('Override ExpansionTile animation using AnimationStyle', (WidgetTester tester) async {
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 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 1146 1147
    const Key expansionTileKey = Key('expansionTileKey');

    Widget buildExpansionTile({ AnimationStyle? animationStyle }) {
      return MaterialApp(
        home: Material(
          child: Center(
            child: ExpansionTile(
              key: expansionTileKey,
              expansionAnimationStyle: animationStyle,
              title: const TestText('title'),
              children: const <Widget>[
                SizedBox(height: 100, width: 100),
              ],
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildExpansionTile());

    double getHeight(Key key) => tester.getSize(find.byKey(key)).height;

    // Test initial ExpansionTile height.
    expect(getHeight(expansionTileKey), 58.0);

    // Test the default expansion animation.
    await tester.tap(find.text('title'));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50)); // Advance the animation by 1/4 of its duration.

    expect(getHeight(expansionTileKey), closeTo(67.4, 0.1));

    await tester.pump(const Duration(milliseconds: 50)); // Advance the animation by 2/4 of its duration.

    expect(getHeight(expansionTileKey), closeTo(89.6, 0.1));

    await tester.pumpAndSettle(); // Advance the animation to the end.

    expect(getHeight(expansionTileKey), 158.0);

    // Tap to collapse the ExpansionTile.
    await tester.tap(find.text('title'));
    await tester.pumpAndSettle();

    // Override the animation duration.
    await tester.pumpWidget(buildExpansionTile(animationStyle: AnimationStyle(duration: const Duration(milliseconds: 800))));
    await tester.pumpAndSettle();

    // Test the overridden animation duration.
    await tester.tap(find.text('title'));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 200)); // Advance the animation by 1/4 of its duration.

    expect(getHeight(expansionTileKey), closeTo(67.4, 0.1));

    await tester.pump(const Duration(milliseconds: 200)); // Advance the animation by 2/4 of its duration.

    expect(getHeight(expansionTileKey), closeTo(89.6, 0.1));

    await tester.pumpAndSettle(); // Advance the animation to the end.

    expect(getHeight(expansionTileKey), 158.0);

    // Tap to collapse the ExpansionTile.
    await tester.tap(find.text('title'));
    await tester.pumpAndSettle();

    // Override the animation curve.
    await tester.pumpWidget(buildExpansionTile(animationStyle: AnimationStyle(curve: Easing.emphasizedDecelerate)));
    await tester.pumpAndSettle();

    // Test the overridden animation curve.
    await tester.tap(find.text('title'));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50)); // Advance the animation by 1/4 of its duration.

    expect(getHeight(expansionTileKey), closeTo(141.2, 0.1));

    await tester.pump(const Duration(milliseconds: 50)); // Advance the animation by 2/4 of its duration.

    expect(getHeight(expansionTileKey), closeTo(153, 0.1));

    await tester.pumpAndSettle(); // Advance the animation to the end.

    expect(getHeight(expansionTileKey), 158.0);

    // Tap to collapse the ExpansionTile.
    await tester.tap(find.text('title'));

    // Test no animation.
    await tester.pumpWidget(buildExpansionTile(animationStyle: AnimationStyle.noAnimation));

    // Tap to expand the ExpansionTile.
    await tester.tap(find.text('title'));
    await tester.pump();

    expect(getHeight(expansionTileKey), 158.0);
  });

1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 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 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
  testWidgets('Material3 - ExpansionTile draws Inkwell splash on top of background color', (WidgetTester tester) async {
    const Key expansionTileKey = Key('expansionTileKey');
    const ShapeBorder shape = RoundedRectangleBorder(
      borderRadius: BorderRadius.all(Radius.circular(16)),
    );
    const ShapeBorder collapsedShape = RoundedRectangleBorder(
      borderRadius: BorderRadius.all(Radius.circular(16)),
    );
    const Color collapsedBackgroundColor = Color(0xff00ff00);
    const Color backgroundColor = Color(0xffff0000);

    await tester.pumpWidget(const MaterialApp(
      home: Material(
        child: Center(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 24.0),
            child: ExpansionTile(
              key: expansionTileKey,
              shape: shape,
              collapsedBackgroundColor: collapsedBackgroundColor,
              backgroundColor: backgroundColor,
              collapsedShape: collapsedShape,
              title: TestText('title'),
              trailing: TestIcon(),
              children: <Widget>[
                SizedBox(height: 100, width: 100),
              ],
            ),
          ),
        ),
      ),
    ));

    // Tap and hold the ExpansionTile to trigger ink splash.
    final Offset center = tester.getCenter(find.byKey(expansionTileKey));
    final TestGesture gesture = await tester.startGesture(center);
    await tester.pump(); // Start the splash animation.
    await tester.pump(const Duration(milliseconds: 100)); // Splash is underway.

    // Material 3 uses the InkSparkle which uses a shader, so we can't capture
    // the effect with paint methods. Use a golden test instead.
    // Check if the ink sparkle is drawn on top of the background color.
    await expectLater(
      find.byKey(expansionTileKey),
      matchesGoldenFile('expansion_tile.ink_splash.drawn_on_top_of_background_color.png'),
    );

    // Finish gesture to release resources.
    await gesture.up();
    await tester.pumpAndSettle();
  });

 testWidgets('Default clipBehavior when a shape is provided', (WidgetTester tester) async {
    await tester.pumpWidget(
      const MaterialApp(
        home: Scaffold(
          body: ExpansionTile(
            title: Text('Title'),
            subtitle: Text('Subtitle'),
            shape: StadiumBorder(),
            children: <Widget>[ListTile(title: Text('0'))],
          ),
        ),
      ),
    );

    expect(getMaterial(tester).clipBehavior, Clip.antiAlias);
  });

 testWidgets('Can override clipBehavior when a shape is provided', (WidgetTester tester) async {
    await tester.pumpWidget(
      const MaterialApp(
        home: Scaffold(
          body: ExpansionTile(
            title: Text('Title'),
            subtitle: Text('Subtitle'),
            shape: StadiumBorder(),
            clipBehavior: Clip.none,
            children: <Widget>[ListTile(title: Text('0'))],
          ),
        ),
      ),
    );

    expect(getMaterial(tester).clipBehavior, Clip.none);
  });

1235
  group('Material 2', () {
1236 1237 1238
    // These tests are only relevant for Material 2. Once Material 2
    // support is deprecated and the APIs are removed, these tests
    // can be deleted.
1239

1240
    testWidgets('ExpansionTile default iconColor, textColor', (WidgetTester tester) async {
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
      final ThemeData theme = ThemeData(useMaterial3: false);

      await tester.pumpWidget(MaterialApp(
        theme: theme,
        home: const Material(
          child: ExpansionTile(
            title: TestText('title'),
            trailing: TestIcon(),
            children: <Widget>[
              SizedBox(height: 100, width: 100),
            ],
          ),
        ),
      ));

      Color getIconColor() => tester.state<TestIconState>(find.byType(TestIcon)).iconTheme.color!;
      Color getTextColor() => tester.state<TestTextState>(find.byType(TestText)).textStyle.color!;

      expect(getIconColor(), theme.unselectedWidgetColor);
      expect(getTextColor(), theme.textTheme.titleMedium!.color);

      await tester.tap(find.text('title'));
      await tester.pumpAndSettle();

      expect(getIconColor(), theme.colorScheme.primary);
      expect(getTextColor(), theme.colorScheme.primary);
    });
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 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

    testWidgets('Material2 - ExpansionTile draws inkwell splash on top of background color', (WidgetTester tester) async {
      const Key expansionTileKey = Key('expansionTileKey');
      final ThemeData theme = ThemeData(useMaterial3: false);
      const ShapeBorder shape = RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(16)),
      );
      const ShapeBorder collapsedShape = RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(16)),
      );
      const Color collapsedBackgroundColor = Color(0xff00ff00);
      const Color backgroundColor = Color(0xffff0000);

      await tester.pumpWidget(MaterialApp(
        theme: theme,
        home: const Material(
          child: Center(
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 24.0),
              child: ExpansionTile(
                key: expansionTileKey,
                shape: shape,
                collapsedBackgroundColor: collapsedBackgroundColor,
                backgroundColor: backgroundColor,
                collapsedShape: collapsedShape,
                title: TestText('title'),
                trailing: TestIcon(),
                children: <Widget>[
                  SizedBox(height: 100, width: 100),
                ],
              ),
            ),
          ),
        ),
      ));

      // Tap and hold the ExpansionTile to trigger ink splash.
      final Offset center = tester.getCenter(find.byKey(expansionTileKey));
      final TestGesture gesture = await tester.startGesture(center);
      await tester.pump(); // Start the splash animation.
      await tester.pump(const Duration(milliseconds: 100)); // Splash is underway.

      final RenderObject inkFeatures = tester.allRenderObjects.firstWhere((RenderObject object) => object.runtimeType.toString() == '_RenderInkFeatures');
      // Check if the ink splash is drawn on top of the background color.
      expect(inkFeatures, paints..path(color: collapsedBackgroundColor)..circle(color: theme.splashColor));

      // Finish gesture to release resources.
      await gesture.up();
      await tester.pumpAndSettle();
    });
1318
  });
1319

1320
  testWidgets('ExpansionTileController isExpanded, expand() and collapse()', (WidgetTester tester) async {
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
    final ExpansionTileController controller = ExpansionTileController();

    await tester.pumpWidget(MaterialApp(
      home: Material(
        child: ExpansionTile(
          controller: controller,
          title: const Text('Title'),
          children: const <Widget>[
            Text('Child 0'),
          ],
        ),
      ),
    ));

    expect(find.text('Child 0'), findsNothing);
    expect(controller.isExpanded, isFalse);
    controller.expand();
    expect(controller.isExpanded, isTrue);
    await tester.pumpAndSettle();
    expect(find.text('Child 0'), findsOneWidget);
    expect(controller.isExpanded, isTrue);
    controller.collapse();
    expect(controller.isExpanded, isFalse);
    await tester.pumpAndSettle();
    expect(find.text('Child 0'), findsNothing);
  });

1348
  testWidgets('Calling ExpansionTileController.expand/collapsed has no effect if it is already expanded/collapsed', (WidgetTester tester) async {
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
    final ExpansionTileController controller = ExpansionTileController();

    await tester.pumpWidget(MaterialApp(
      home: Material(
        child: ExpansionTile(
          controller: controller,
          title: const Text('Title'),
          initiallyExpanded: true,
          children: const <Widget>[
            Text('Child 0'),
          ],
        ),
      ),
    ));

    expect(find.text('Child 0'), findsOneWidget);
    expect(controller.isExpanded, isTrue);
    controller.expand();
    expect(controller.isExpanded, isTrue);
    await tester.pump();
    expect(tester.hasRunningAnimations, isFalse);
    expect(find.text('Child 0'), findsOneWidget);
    controller.collapse();
    expect(controller.isExpanded, isFalse);
    await tester.pump();
    expect(tester.hasRunningAnimations, isTrue);
    await tester.pumpAndSettle();
    expect(controller.isExpanded, isFalse);
    expect(find.text('Child 0'), findsNothing);
    controller.collapse();
    expect(controller.isExpanded, isFalse);
    await tester.pump();
    expect(tester.hasRunningAnimations, isFalse);
  });

1384
  testWidgets('Call to ExpansionTileController.of()', (WidgetTester tester) async {
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
    final GlobalKey titleKey = GlobalKey();
    final GlobalKey childKey = GlobalKey();
    await tester.pumpWidget(MaterialApp(
      home: Material(
        child: ExpansionTile(
          initiallyExpanded: true,
          title: Text('Title', key: titleKey),
          children: <Widget>[
            Text('Child 0', key: childKey),
          ],
        ),
      ),
    ));

    final ExpansionTileController controller1 = ExpansionTileController.of(childKey.currentContext!);
    expect(controller1.isExpanded, isTrue);

    final ExpansionTileController controller2 = ExpansionTileController.of(titleKey.currentContext!);
    expect(controller2.isExpanded, isTrue);

    expect(controller1, controller2);
  });

1408
  testWidgets('Call to ExpansionTile.maybeOf()', (WidgetTester tester) async {
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
    final GlobalKey titleKey = GlobalKey();
    final GlobalKey nonDescendantKey = GlobalKey();
    await tester.pumpWidget(MaterialApp(
      home: Material(
        child: Column(
          children: <Widget>[
            ExpansionTile(
              title: Text('Title', key: titleKey),
              children: const <Widget>[
                Text('Child 0'),
              ],
            ),
            Text('Non descendant', key: nonDescendantKey),
          ],
        ),
      ),
    ));

    final ExpansionTileController? controller1 = ExpansionTileController.maybeOf(titleKey.currentContext!);
    expect(controller1, isNotNull);
    expect(controller1?.isExpanded, isFalse);

    final ExpansionTileController? controller2 = ExpansionTileController.maybeOf(nonDescendantKey.currentContext!);
    expect(controller2, isNull);
  });
1434

1435
  testWidgets('Check if dense, enableFeedback, visualDensity parameter is working', (WidgetTester tester) async {
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
    final GlobalKey titleKey = GlobalKey();
    final GlobalKey nonDescendantKey = GlobalKey();

    const bool dense = true;
    const bool enableFeedback = false;
    const VisualDensity visualDensity = VisualDensity.compact;

    await tester.pumpWidget(MaterialApp(
      home: Material(
        child: Column(
          children: <Widget>[
            ExpansionTile(
              dense: dense,
              enableFeedback: enableFeedback,
              visualDensity: visualDensity,
              title: Text('Title', key: titleKey),
              children: const <Widget>[
                Text('Child 0'),
              ],
            ),
            Text('Non descendant', key: nonDescendantKey),
          ],
        ),
      ),
    ));

    final Finder tileFinder = find.byType(ListTile);
    final ListTile tileWidget = tester.widget<ListTile>(tileFinder);
    expect(tileWidget.dense, dense);
    expect(tileWidget.enableFeedback, enableFeedback);
    expect(tileWidget.visualDensity, visualDensity);
  });
1468

1469
  testWidgets('ExpansionTileController should not toggle if disabled', (WidgetTester tester) async {
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
    final ExpansionTileController controller = ExpansionTileController();

    await tester.pumpWidget(MaterialApp(
      home: Material(
        child: ExpansionTile(
          enabled: false,
          controller: controller,
          title: const Text('Title'),
          children: const <Widget>[
            Text('Child 0'),
          ],
        ),
      ),
    ));

    expect(find.text('Child 0'), findsNothing);
    expect(controller.isExpanded, isFalse);
    await tester.tap(find.widgetWithText(ExpansionTile, 'Title'));
    await tester.pumpAndSettle();
    expect(find.text('Child 0'), findsNothing);
    expect(controller.isExpanded, isFalse);
    controller.expand();
    await tester.pumpAndSettle();
    expect(find.text('Child 0'), findsOneWidget);
    expect(controller.isExpanded, isTrue);
    await tester.tap(find.widgetWithText(ExpansionTile, 'Title'));
    await tester.pumpAndSettle();
    expect(find.text('Child 0'), findsOneWidget);
    expect(controller.isExpanded, isTrue);
  });
1500
}