dropdown_test.dart 45.1 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:math' as math;
6
import 'dart:ui' show window;
7

8 9
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
10
import 'package:flutter/rendering.dart';
11

12 13
import '../widgets/semantics_tester.dart';

14
const List<String> menuItems = <String>['one', 'two', 'three', 'four'];
15
final ValueChanged<String> onChanged = (_) { };
16

17
final Type dropdownButtonType = DropdownButton<String>(
18
  onChanged: (_) { },
19
  items: const <DropdownMenuItem<String>>[],
20
).runtimeType;
21

22 23 24 25 26 27 28
Finder _iconRichText(Key iconKey) {
  return find.descendant(
    of: find.byKey(iconKey),
    matching: find.byType(RichText),
  );
}

29
Widget buildFrame({
30
  Key buttonKey,
31
  String value = 'two',
32
  ValueChanged<String> onChanged,
33 34 35 36
  Widget icon,
  Color iconDisabledColor,
  Color iconEnabledColor,
  double iconSize = 24.0,
37
  bool isDense = false,
38
  bool isExpanded = false,
39
  Widget hint,
40
  Widget disabledHint,
41
  Widget underline,
42 43 44
  List<String> items = menuItems,
  Alignment alignment = Alignment.center,
  TextDirection textDirection = TextDirection.ltr,
45
}) {
46
  return TestApp(
47
    textDirection: textDirection,
48 49
    child: Material(
      child: Align(
50
        alignment: alignment,
51 52
        child: RepaintBoundary(
          child: DropdownButton<String>(
53 54 55
            key: buttonKey,
            value: value,
            hint: hint,
56
            disabledHint: disabledHint,
57
            onChanged: onChanged,
58 59 60 61
            icon: icon,
            iconSize: iconSize,
            iconDisabledColor: iconDisabledColor,
            iconEnabledColor: iconEnabledColor,
62 63
            isDense: isDense,
            isExpanded: isExpanded,
64
            underline: underline,
65
            items: items == null ? null : items.map<DropdownMenuItem<String>>((String item) {
66 67
              return DropdownMenuItem<String>(
                key: ValueKey<String>(item),
68
                value: item,
69
                child: Text(item, key: ValueKey<String>(item + 'Text')),
70 71
              );
            }).toList(),
72
          ),
73 74 75 76 77 78
        ),
      ),
    ),
  );
}

79 80 81 82 83
class TestApp extends StatefulWidget {
  const TestApp({ this.textDirection, this.child });
  final TextDirection textDirection;
  final Widget child;
  @override
84
  _TestAppState createState() => _TestAppState();
85 86 87 88 89
}

class _TestAppState extends State<TestApp> {
  @override
  Widget build(BuildContext context) {
90
    return Localizations(
91
      locale: const Locale('en', 'US'),
92
      delegates: const <LocalizationsDelegate<dynamic>>[
93 94 95
        DefaultWidgetsLocalizations.delegate,
        DefaultMaterialLocalizations.delegate,
      ],
96 97 98
      child: MediaQuery(
        data: MediaQueryData.fromWindow(window),
        child: Directionality(
99
          textDirection: widget.textDirection,
100
          child: Navigator(
101 102
            onGenerateRoute: (RouteSettings settings) {
              assert(settings.name == '/');
103
              return MaterialPageRoute<void>(
104 105 106 107 108
                settings: settings,
                builder: (BuildContext context) => widget.child,
              );
            },
          ),
109 110 111 112 113 114
        ),
      ),
    );
  }
}

115 116 117 118 119
// When the dropdown's menu is popped up, a RenderParagraph for the selected
// menu's text item will appear both in the dropdown button and in the menu.
// The RenderParagraphs should be aligned, i.e. they should have the same
// size and location.
void checkSelectedItemTextGeometry(WidgetTester tester, String value) {
120
  final List<RenderBox> boxes = tester.renderObjectList<RenderBox>(find.byKey(ValueKey<String>(value + 'Text'))).toList();
121 122 123
  expect(boxes.length, equals(2));
  final RenderBox box0 = boxes[0];
  final RenderBox box1 = boxes[1];
124
  expect(box0.localToGlobal(Offset.zero), equals(box1.localToGlobal(Offset.zero)));
125 126 127 128
  expect(box0.size, equals(box1.size));
}

bool sameGeometry(RenderBox box1, RenderBox box2) {
129
  expect(box1.localToGlobal(Offset.zero), equals(box2.localToGlobal(Offset.zero)));
130 131 132 133
  expect(box1.size.height, equals(box2.size.height));
  return true;
}

134
void main() {
135
  testWidgets('Default dropdown golden', (WidgetTester tester) async {
136
    final Key buttonKey = UniqueKey();
137
    Widget build() => buildFrame(buttonKey: buttonKey, value: 'two', onChanged: onChanged);
138 139 140 141 142
    await tester.pumpWidget(build());
    final Finder buttonFinder = find.byKey(buttonKey);
    assert(tester.renderObject(buttonFinder).attached);
    await expectLater(
      find.ancestor(of: buttonFinder, matching: find.byType(RepaintBoundary)).first,
143 144 145 146
      matchesGoldenFile(
        'dropdown_test.default.png',
        version: 0,
      ),
147
    );
148
  }, skip: isBrowser);
149 150

  testWidgets('Expanded dropdown golden', (WidgetTester tester) async {
151
    final Key buttonKey = UniqueKey();
152
    Widget build() => buildFrame(buttonKey: buttonKey, value: 'two', isExpanded: true, onChanged: onChanged);
153 154 155 156 157
    await tester.pumpWidget(build());
    final Finder buttonFinder = find.byKey(buttonKey);
    assert(tester.renderObject(buttonFinder).attached);
    await expectLater(
      find.ancestor(of: buttonFinder, matching: find.byType(RepaintBoundary)).first,
158 159 160 161
      matchesGoldenFile(
        'dropdown_test.expanded.png',
        version: 0,
      ),
162
    );
163
  }, skip: isBrowser);
164

165
  testWidgets('Dropdown button control test', (WidgetTester tester) async {
166
    String value = 'one';
167 168 169 170
    void didChangeValue(String newValue) {
      value = newValue;
    }

171
    Widget build() => buildFrame(value: value, onChanged: didChangeValue);
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

    await tester.pumpWidget(build());

    await tester.tap(find.text('one'));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    expect(value, equals('one'));

    await tester.tap(find.text('three').last);

    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    expect(value, equals('three'));

    await tester.tap(find.text('three'));
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    expect(value, equals('three'));

    await tester.pumpWidget(build());

    await tester.tap(find.text('two').last);

    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    expect(value, equals('two'));
  });

204
  testWidgets('Dropdown button with no app', (WidgetTester tester) async {
205
    String value = 'one';
206 207 208 209 210
    void didChangeValue(String newValue) {
      value = newValue;
    }

    Widget build() {
211
      return Directionality(
212
        textDirection: TextDirection.ltr,
213
        child: Navigator(
214 215
          initialRoute: '/',
          onGenerateRoute: (RouteSettings settings) {
216
            return MaterialPageRoute<void>(
217 218
              settings: settings,
              builder: (BuildContext context) {
219
                return Material(
220 221 222 223 224 225
                  child: buildFrame(value: 'one', onChanged: didChangeValue),
                );
              },
            );
          },
        ),
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
      );
    }

    await tester.pumpWidget(build());

    await tester.tap(find.text('one'));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    expect(value, equals('one'));

    await tester.tap(find.text('three').last);

    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    expect(value, equals('three'));

    await tester.tap(find.text('three'));
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    expect(value, equals('three'));

    await tester.pumpWidget(build());

    await tester.tap(find.text('two').last);

    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    expect(value, equals('two'));
  });

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
  testWidgets('Dropdown form field', (WidgetTester tester) async {
    String value = 'one';

    await tester.pumpWidget(
      StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
          return MaterialApp(
            home: Material(
              child: DropdownButtonFormField<String>(
                value: value,
                hint: const Text('Select Value'),
                decoration: const InputDecoration(
                  prefixIcon: Icon(Icons.fastfood)
                ),
                items: menuItems.map((String val) {
                  return DropdownMenuItem<String>(
                    value: val,
277
                    child: Text(val),
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
                  );
                }).toList(),
                onChanged: (String v) {
                  setState(() {
                    value = v;
                  });
                },
                validator: (String v) => v == null ? 'Must select value' : null,
              ),
            ),
          );
        }
      )
    );

    expect(value, equals('one'));
    await tester.tap(find.text('one'));
    await tester.pumpAndSettle();
    await tester.tap(find.text('three').last);
    await tester.pump();
    await tester.pumpAndSettle();
    expect(value, equals('three'));
  });

302 303 304 305
  testWidgets('Dropdown in ListView', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/12053
    // Positions a DropdownButton at the left and right edges of the screen,
    // forcing it to be sized down to the viewport width
306
    const String value = 'foo';
307
    final UniqueKey itemKey = UniqueKey();
308
    await tester.pumpWidget(
309 310 311
      MaterialApp(
        home: Material(
          child: ListView(
312
            children: <Widget>[
313
              DropdownButton<String>(
314 315
                value: value,
                items: <DropdownMenuItem<String>>[
316
                  DropdownMenuItem<String>(
317 318
                    key: itemKey,
                    value: value,
319
                    child: const Text(value),
320 321
                  ),
                ],
322
                onChanged: (_) { },
323 324 325 326 327 328 329 330
              ),
            ],
          ),
        ),
      ),
    );
    await tester.tap(find.text(value));
    await tester.pump();
331
    final List<RenderBox> itemBoxes = tester.renderObjectList<RenderBox>(find.byKey(itemKey)).toList();
332 333 334 335 336
    expect(itemBoxes[0].localToGlobal(Offset.zero).dx, equals(0.0));
    expect(itemBoxes[1].localToGlobal(Offset.zero).dx, equals(16.0));
    expect(itemBoxes[1].size.width, equals(800.0 - 16.0 * 2));
  });

337
  testWidgets('Dropdown screen edges', (WidgetTester tester) async {
338
    int value = 4;
339
    final List<DropdownMenuItem<int>> items = <DropdownMenuItem<int>>[];
340
    for (int i = 0; i < 20; ++i)
341
      items.add(DropdownMenuItem<int>(value: i, child: Text('$i')));
342 343 344 345 346

    void handleChanged(int newValue) {
      value = newValue;
    }

347
    final DropdownButton<int> button = DropdownButton<int>(
348 349
      value: value,
      onChanged: handleChanged,
350
      items: items,
351 352
    );

353
    await tester.pumpWidget(
354 355 356
      MaterialApp(
        home: Material(
          child: Align(
357
            alignment: Alignment.topCenter,
358 359 360 361
            child: button,
          ),
        ),
      ),
362 363
    );

364 365 366
    await tester.tap(find.text('4'));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation
367

368 369
    // We should have two copies of item 5, one in the menu and one in the
    // button itself.
370
    expect(tester.elementList(find.text('5')), hasLength(2));
371 372 373

    // We should only have one copy of item 19, which is in the button itself.
    // The copy in the menu shouldn't be in the tree because it's off-screen.
374
    expect(tester.elementList(find.text('19')), hasLength(1));
375

376
    expect(value, 4);
377
    await tester.tap(find.byWidget(button));
378
    expect(value, 4);
379 380
    // this waits for the route's completer to complete, which calls handleChanged
    await tester.idle();
381
    expect(value, 4);
382 383 384

    // TODO(abarth): Remove these calls to pump once navigator cleans up its
    // pop transitions.
385 386
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation
387
  });
388

389 390
  for (TextDirection textDirection in TextDirection.values) {
    testWidgets('Dropdown button aligns selected menu item ($textDirection)', (WidgetTester tester) async {
391
      final Key buttonKey = UniqueKey();
392
      const String value = 'two';
393

394
      Widget build() => buildFrame(buttonKey: buttonKey, value: value, textDirection: textDirection, onChanged: onChanged);
395 396

      await tester.pumpWidget(build());
397
      final RenderBox buttonBox = tester.renderObject<RenderBox>(find.byKey(buttonKey));
398 399 400 401 402 403 404 405 406 407 408 409 410
      assert(buttonBox.attached);
      final Offset buttonOriginBeforeTap = buttonBox.localToGlobal(Offset.zero);

      await tester.tap(find.text('two'));
      await tester.pump();
      await tester.pump(const Duration(seconds: 1)); // finish the menu animation

      // Tapping the dropdown button should not cause it to move.
      expect(buttonBox.localToGlobal(Offset.zero), equals(buttonOriginBeforeTap));

      // The selected dropdown item is both in menu we just popped up, and in
      // the IndexedStack contained by the dropdown button. Both of them should
      // have the same origin and height as the dropdown button.
411
      final List<RenderObject> itemBoxes = tester.renderObjectList<RenderBox>(find.byKey(const ValueKey<String>('two'))).toList();
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
      expect(itemBoxes.length, equals(2));
      for (RenderBox itemBox in itemBoxes) {
        assert(itemBox.attached);
        assert(textDirection != null);
        switch (textDirection) {
          case TextDirection.rtl:
            expect(buttonBox.localToGlobal(buttonBox.size.bottomRight(Offset.zero)),
                   equals(itemBox.localToGlobal(itemBox.size.bottomRight(Offset.zero))));
            break;
          case TextDirection.ltr:
            expect(buttonBox.localToGlobal(Offset.zero), equals(itemBox.localToGlobal(Offset.zero)));
            break;
        }
        expect(buttonBox.size.height, equals(itemBox.size.height));
      }
427

428 429 430
      // The two RenderParagraph objects, for the 'two' items' Text children,
      // should have the same size and location.
      checkSelectedItemTextGeometry(tester, 'two');
431

432
      await tester.pumpWidget(Container()); // reset test
433 434
    });
  }
435

436
  testWidgets('Arrow icon aligns with the edge of button when expanded', (WidgetTester tester) async {
437
    final Key buttonKey = UniqueKey();
438

439
    Widget build() => buildFrame(buttonKey: buttonKey, value: 'two', isExpanded: true, onChanged: onChanged);
440 441

    await tester.pumpWidget(build());
442
    final RenderBox buttonBox = tester.renderObject<RenderBox>(find.byKey(buttonKey));
443 444
    assert(buttonBox.attached);

445
    final RenderBox arrowIcon = tester.renderObject<RenderBox>(find.byIcon(Icons.arrow_drop_down));
446 447 448 449
    assert(arrowIcon.attached);

    // Arrow icon should be aligned with far right of button when expanded
    expect(arrowIcon.localToGlobal(Offset.zero).dx,
450
        buttonBox.size.centerRight(Offset(-arrowIcon.size.width, 0.0)).dx);
451 452
  });

453 454 455 456 457 458 459 460 461 462 463 464 465 466 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 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
  testWidgets('Dropdown button icon will accept widgets as icons', (WidgetTester tester) async {
    final Widget customWidget = Container(
      decoration: ShapeDecoration(
        shape: CircleBorder(
          side: BorderSide(
            width: 5.0,
            color: Colors.grey.shade700,
          ),
        ),
      ),
    );

    await tester.pumpWidget(buildFrame(
      icon: customWidget,
      onChanged: onChanged,
    ));

    expect(find.byWidget(customWidget), findsOneWidget);
    expect(find.byIcon(Icons.arrow_drop_down), findsNothing);

    await tester.pumpWidget(buildFrame(
      icon: const Icon(Icons.assessment),
      onChanged: onChanged,
    ));

    expect(find.byIcon(Icons.assessment), findsOneWidget);
    expect(find.byIcon(Icons.arrow_drop_down), findsNothing);
  });

  testWidgets('Dropdown button icon should have default size and colors when not defined', (WidgetTester tester) async {
    final Key iconKey = UniqueKey();
    final Icon customIcon = Icon(Icons.assessment, key: iconKey);

    await tester.pumpWidget(buildFrame(
      icon: customIcon,
      onChanged: onChanged,
    ));

    // test for size
    final RenderBox icon = tester.renderObject(find.byKey(iconKey));
    expect(icon.size, const Size(24.0, 24.0));

    // test for enabled color
    final RichText enabledRichText = tester.widget<RichText>(_iconRichText(iconKey));
    expect(enabledRichText.text.style.color, Colors.grey.shade700);

    // test for disabled color
    await tester.pumpWidget(buildFrame(
      icon: customIcon,
      onChanged: null,
    ));

    final RichText disabledRichText = tester.widget<RichText>(_iconRichText(iconKey));
    expect(disabledRichText.text.style.color, Colors.grey.shade400);
  });

  testWidgets('Dropdown button icon should have the passed in size and color instead of defaults', (WidgetTester tester) async {
    final Key iconKey = UniqueKey();
    final Icon customIcon = Icon(Icons.assessment, key: iconKey);

    await tester.pumpWidget(buildFrame(
      icon: customIcon,
      iconSize: 30.0,
      iconEnabledColor: Colors.pink,
      iconDisabledColor: Colors.orange,
      onChanged: onChanged,
    ));

    // test for size
    final RenderBox icon = tester.renderObject(find.byKey(iconKey));
    expect(icon.size, const Size(30.0, 30.0));

    // test for enabled color
    final RichText enabledRichText = tester.widget<RichText>(_iconRichText(iconKey));
    expect(enabledRichText.text.style.color, Colors.pink);

    // test for disabled color
    await tester.pumpWidget(buildFrame(
      icon: customIcon,
      iconSize: 30.0,
      iconEnabledColor: Colors.pink,
      iconDisabledColor: Colors.orange,
      onChanged: null,
    ));

    final RichText disabledRichText = tester.widget<RichText>(_iconRichText(iconKey));
    expect(disabledRichText.text.style.color, Colors.orange);
  });

  testWidgets('Dropdown button should use its own size and color properties over those defined by the theme', (WidgetTester tester) async {
    final Key iconKey = UniqueKey();

    final Icon customIcon = Icon(
      Icons.assessment,
      key: iconKey,
      size: 40.0,
      color: Colors.yellow,
    );

    await tester.pumpWidget(buildFrame(
      icon: customIcon,
      iconSize: 30.0,
      iconEnabledColor: Colors.pink,
      iconDisabledColor: Colors.orange,
      onChanged: onChanged,
    ));

    // test for size
    final RenderBox icon = tester.renderObject(find.byKey(iconKey));
    expect(icon.size, const Size(40.0, 40.0));

    // test for enabled color
    final RichText enabledRichText = tester.widget<RichText>(_iconRichText(iconKey));
    expect(enabledRichText.text.style.color, Colors.yellow);

    // test for disabled color
    await tester.pumpWidget(buildFrame(
      icon: customIcon,
      iconSize: 30.0,
      iconEnabledColor: Colors.pink,
      iconDisabledColor: Colors.orange,
      onChanged: null,
    ));

    final RichText disabledRichText = tester.widget<RichText>(_iconRichText(iconKey));
    expect(disabledRichText.text.style.color, Colors.yellow);
  });

581
  testWidgets('Dropdown button with isDense:true aligns selected menu item', (WidgetTester tester) async {
582
    final Key buttonKey = UniqueKey();
583
    const String value = 'two';
584

585
    Widget build() => buildFrame(buttonKey: buttonKey, value: value, isDense: true, onChanged: onChanged);
586 587

    await tester.pumpWidget(build());
588
    final RenderBox buttonBox = tester.renderObject<RenderBox>(find.byKey(buttonKey));
589 590 591 592 593 594 595 596 597
    assert(buttonBox.attached);

    await tester.tap(find.text('two'));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    // The selected dropdown item is both in menu we just popped up, and in
    // the IndexedStack contained by the dropdown button. Both of them should
    // have the same vertical center as the button.
598
    final List<RenderBox> itemBoxes = tester.renderObjectList<RenderBox>(find.byKey(const ValueKey<String>('two'))).toList();
599 600 601 602
    expect(itemBoxes.length, equals(2));

    // When isDense is true, the button's height is reduced. The menu items'
    // heights are not.
603
    final double menuItemHeight = itemBoxes.map<double>((RenderBox box) => box.size.height).reduce(math.max);
604 605
    expect(menuItemHeight, greaterThan(buttonBox.size.height));

606
    for (RenderBox itemBox in itemBoxes) {
607
      assert(itemBox.attached);
608
      final Offset buttonBoxCenter = buttonBox.size.center(buttonBox.localToGlobal(Offset.zero));
609
      final Offset itemBoxCenter = itemBox.size.center(itemBox.localToGlobal(Offset.zero));
610
      expect(buttonBoxCenter.dy, equals(itemBoxCenter.dy));
611 612 613 614 615 616
    }

    // The two RenderParagraph objects, for the 'two' items' Text children,
    // should have the same size and location.
    checkSelectedItemTextGeometry(tester, 'two');
  });
617

618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
  testWidgets('Dropdown button can have a text style with no fontSize specified', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/33425
    const String value = 'foo';
    final UniqueKey itemKey = UniqueKey();

    await tester.pumpWidget(TestApp(
      textDirection: TextDirection.ltr,
      child: Material(
        child: DropdownButton<String>(
          value: value,
          items: <DropdownMenuItem<String>>[
            DropdownMenuItem<String>(
              key: itemKey,
              value: 'foo',
              child: const Text(value),
            ),
          ],
          isDense: true,
          onChanged: (_) { },
          style: const TextStyle(color: Colors.blue),
        ),
      ),
    ));

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

645 646 647 648 649 650
  testWidgets('Dropdown menu scrolls to first item in long lists', (WidgetTester tester) async {
    // Open the dropdown menu
    final Key buttonKey = UniqueKey();
    await tester.pumpWidget(buildFrame(
      buttonKey: buttonKey,
      value: null, // nothing selected
651 652
      items: List<String>.generate(/*length=*/ 100, (int index) => index.toString()),
      onChanged: onChanged,
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
    ));
    await tester.tap(find.byKey(buttonKey));
    await tester.pump();
    await tester.pumpAndSettle(); // finish the menu animation

    // Find the first item in the scrollable dropdown list
    final Finder menuItemFinder = find.byType(Scrollable);
    final RenderBox menuItemContainer = tester.renderObject<RenderBox>(menuItemFinder);
    final RenderBox firstItem = tester.renderObject<RenderBox>(
      find.descendant(of: menuItemFinder, matching: find.byKey(const ValueKey<String>('0'))));

    // List should be scrolled so that the first item is at the top. Menu items
    // are offset 8.0 from the top edge of the scrollable menu.
    const Offset selectedItemOffset = Offset(0.0, -8.0);
    expect(
      firstItem.size.topCenter(firstItem.localToGlobal(selectedItemOffset)).dy,
669
      equals(menuItemContainer.size.topCenter(menuItemContainer.localToGlobal(Offset.zero)).dy),
670 671 672 673 674 675 676 677 678
    );
  });

  testWidgets('Dropdown menu aligns selected item with button in long lists', (WidgetTester tester) async {
    // Open the dropdown menu
    final Key buttonKey = UniqueKey();
    await tester.pumpWidget(buildFrame(
      buttonKey: buttonKey,
      value: '50',
679 680
      items: List<String>.generate(/*length=*/ 100, (int index) => index.toString()),
      onChanged: onChanged,
681
    ));
682
    final RenderBox buttonBox = tester.renderObject<RenderBox>(find.byKey(buttonKey));
683 684 685 686 687 688 689 690 691 692
    await tester.tap(find.byKey(buttonKey));
    await tester.pumpAndSettle(); // finish the menu animation

    // Find the selected item in the scrollable dropdown list
    final RenderBox selectedItem = tester.renderObject<RenderBox>(
      find.descendant(of: find.byType(Scrollable), matching: find.byKey(const ValueKey<String>('50'))));

    // List should be scrolled so that the selected item is in line with the button
    expect(
      selectedItem.size.center(selectedItem.localToGlobal(Offset.zero)).dy,
693
      equals(buttonBox.size.center(buttonBox.localToGlobal(Offset.zero)).dy),
694 695 696
    );
  });

697
  testWidgets('Size of DropdownButton with null value', (WidgetTester tester) async {
698
    final Key buttonKey = UniqueKey();
699 700
    String value;

701
    Widget build() => buildFrame(buttonKey: buttonKey, value: value, onChanged: onChanged);
702 703

    await tester.pumpWidget(build());
704
    final RenderBox buttonBoxNullValue = tester.renderObject<RenderBox>(find.byKey(buttonKey));
705 706 707 708
    assert(buttonBoxNullValue.attached);

    value = 'three';
    await tester.pumpWidget(build());
709
    final RenderBox buttonBox = tester.renderObject<RenderBox>(find.byKey(buttonKey));
710 711
    assert(buttonBox.attached);

712
    // A Dropdown button with a null value should be the same size as a
713
    // one with a non-null value.
714
    expect(buttonBox.localToGlobal(Offset.zero), equals(buttonBoxNullValue.localToGlobal(Offset.zero)));
715 716 717
    expect(buttonBox.size, equals(buttonBoxNullValue.size));
  });

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
  testWidgets('Size of DropdownButton with no items', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/26419
    final Key buttonKey = UniqueKey();
    List<String> items;

    Widget build() => buildFrame(buttonKey: buttonKey, items: items, onChanged: onChanged);

    await tester.pumpWidget(build());
    final RenderBox buttonBoxNullItems = tester.renderObject<RenderBox>(find.byKey(buttonKey));
    assert(buttonBoxNullItems.attached);

    items = <String>[];
    await tester.pumpWidget(build());
    final RenderBox buttonBoxEmptyItems = tester.renderObject<RenderBox>(find.byKey(buttonKey));
    assert(buttonBoxEmptyItems.attached);

    items = <String>['one', 'two', 'three', 'four'];
    await tester.pumpWidget(build());
    final RenderBox buttonBox = tester.renderObject<RenderBox>(find.byKey(buttonKey));
    assert(buttonBox.attached);

    // A Dropdown button with a null value should be the same size as a
    // one with a non-null value.
    expect(buttonBox.localToGlobal(Offset.zero), equals(buttonBoxNullItems.localToGlobal(Offset.zero)));
    expect(buttonBox.size, equals(buttonBoxNullItems.size));
  });

745
  testWidgets('Layout of a DropdownButton with null value', (WidgetTester tester) async {
746
    final Key buttonKey = UniqueKey();
747 748 749 750 751 752 753 754 755
    String value;

    void onChanged(String newValue) {
      value = newValue;
    }

    Widget build() => buildFrame(buttonKey: buttonKey, value: value, onChanged: onChanged);

    await tester.pumpWidget(build());
756
    final RenderBox buttonBox = tester.renderObject<RenderBox>(find.byKey(buttonKey));
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
    assert(buttonBox.attached);

    // Show the menu.
    await tester.tap(find.byKey(buttonKey));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    // Tap on item 'one', which must appear over the button.
    await tester.tap(find.byKey(buttonKey));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    await tester.pumpWidget(build());
    expect(value, equals('one'));
  });

773
  testWidgets('Size of DropdownButton with null value and a hint', (WidgetTester tester) async {
774
    final Key buttonKey = UniqueKey();
775 776 777
    String value;

    // The hint will define the dropdown's width
778
    Widget build() => buildFrame(buttonKey: buttonKey, value: value, hint: const Text('onetwothree'));
779 780 781

    await tester.pumpWidget(build());
    expect(find.text('onetwothree'), findsOneWidget);
782
    final RenderBox buttonBoxHintValue = tester.renderObject<RenderBox>(find.byKey(buttonKey));
783 784 785 786
    assert(buttonBoxHintValue.attached);

    value = 'three';
    await tester.pumpWidget(build());
787
    final RenderBox buttonBox = tester.renderObject<RenderBox>(find.byKey(buttonKey));
788 789
    assert(buttonBox.attached);

790
    // A Dropdown button with a null value and a hint should be the same size as a
791
    // one with a non-null value.
792
    expect(buttonBox.localToGlobal(Offset.zero), equals(buttonBoxHintValue.localToGlobal(Offset.zero)));
793 794 795
    expect(buttonBox.size, equals(buttonBoxHintValue.size));
  });

796 797
  testWidgets('Dropdown menus must fit within the screen', (WidgetTester tester) async {

Josh Soref's avatar
Josh Soref committed
798
    // The dropdown menu isn't readily accessible. To find it we're assuming that it
799 800 801 802
    // contains a ListView and that it's an instance of _DropdownMenu.
    Rect getMenuRect() {
      Rect menuRect;
      tester.element(find.byType(ListView)).visitAncestorElements((Element element) {
Ian Hickson's avatar
Ian Hickson committed
803
        if (element.toString().startsWith('_DropdownMenu')) {
804 805
          final RenderBox box = element.findRenderObject();
          assert(box != null);
806
          menuRect = box.localToGlobal(Offset.zero) & box.size;
807 808 809 810 811 812 813 814 815 816 817
          return false;
        }
        return true;
      });
      assert(menuRect != null);
      return menuRect;
    }

    // In all of the tests that follow we're assuming that the dropdown menu
    // is horizontally aligned with the center of the dropdown button and padded
    // on the top, left, and right.
818
    const EdgeInsets buttonPadding = EdgeInsets.only(top: 8.0, left: 16.0, right: 24.0);
819 820 821 822 823 824 825 826 827 828

    Rect getExpandedButtonRect() {
      final RenderBox box = tester.renderObject<RenderBox>(find.byType(dropdownButtonType));
      final Rect buttonRect = box.localToGlobal(Offset.zero) & box.size;
      return buttonPadding.inflateRect(buttonRect);
    }

    Rect buttonRect;
    Rect menuRect;

829
    Future<void> popUpAndDown(Widget frame) async {
830 831 832 833 834 835 836 837 838 839 840 841 842
      await tester.pumpWidget(frame);
      await tester.tap(find.byType(dropdownButtonType));
      await tester.pumpAndSettle();
      menuRect = getMenuRect();
      buttonRect = getExpandedButtonRect();
      await tester.tap(find.byType(dropdownButtonType));
    }

    // Dropdown button is along the top of the app. The top of the menu is
    // aligned with the top of the expanded button and shifted horizontally
    // so that it fits within the frame.

    await popUpAndDown(
843
      buildFrame(alignment: Alignment.topLeft, value: menuItems.last, onChanged: onChanged)
844 845
    );
    expect(menuRect.topLeft, Offset.zero);
846
    expect(menuRect.topRight, Offset(menuRect.width, 0.0));
847 848

    await popUpAndDown(
849
      buildFrame(alignment: Alignment.topCenter, value: menuItems.last, onChanged: onChanged)
850
    );
851 852
    expect(menuRect.topLeft, Offset(buttonRect.left, 0.0));
    expect(menuRect.topRight, Offset(buttonRect.right, 0.0));
853 854

    await popUpAndDown(
855
      buildFrame(alignment: Alignment.topRight, value: menuItems.last, onChanged: onChanged)
856
    );
857
    expect(menuRect.topLeft, Offset(800.0 - menuRect.width, 0.0));
858 859 860 861 862 863 864
    expect(menuRect.topRight, const Offset(800.0, 0.0));

    // Dropdown button is along the middle of the app. The top of the menu is
    // aligned with the top of the expanded button (because the 1st item
    // is selected) and shifted horizontally so that it fits within the frame.

    await popUpAndDown(
865
      buildFrame(alignment: Alignment.centerLeft, value: menuItems.first, onChanged: onChanged)
866
    );
867 868
    expect(menuRect.topLeft, Offset(0.0, buttonRect.top));
    expect(menuRect.topRight, Offset(menuRect.width, buttonRect.top));
869 870

    await popUpAndDown(
871
      buildFrame(alignment: Alignment.center, value: menuItems.first, onChanged: onChanged)
872 873 874 875 876
    );
    expect(menuRect.topLeft, buttonRect.topLeft);
    expect(menuRect.topRight, buttonRect.topRight);

    await popUpAndDown(
877
      buildFrame(alignment: Alignment.centerRight, value: menuItems.first, onChanged: onChanged)
878
    );
879 880
    expect(menuRect.topLeft, Offset(800.0 - menuRect.width, buttonRect.top));
    expect(menuRect.topRight, Offset(800.0, buttonRect.top));
881 882 883 884 885 886

    // Dropdown button is along the bottom of the app. The bottom of the menu is
    // aligned with the bottom of the expanded button and shifted horizontally
    // so that it fits within the frame.

    await popUpAndDown(
887
      buildFrame(alignment: Alignment.bottomLeft, value: menuItems.first, onChanged: onChanged)
888 889
    );
    expect(menuRect.bottomLeft, const Offset(0.0, 600.0));
890
    expect(menuRect.bottomRight, Offset(menuRect.width, 600.0));
891 892

    await popUpAndDown(
893
      buildFrame(alignment: Alignment.bottomCenter, value: menuItems.first, onChanged: onChanged)
894
    );
895 896
    expect(menuRect.bottomLeft, Offset(buttonRect.left, 600.0));
    expect(menuRect.bottomRight, Offset(buttonRect.right, 600.0));
897 898

    await popUpAndDown(
899
      buildFrame(alignment: Alignment.bottomRight, value: menuItems.first, onChanged: onChanged)
900
    );
901
    expect(menuRect.bottomLeft, Offset(800.0 - menuRect.width, 600.0));
902 903
    expect(menuRect.bottomRight, const Offset(800.0, 600.0));
  });
904 905

  testWidgets('Dropdown menus are dismissed on screen orientation changes', (WidgetTester tester) async {
906
    await tester.pumpWidget(buildFrame(onChanged: onChanged));
907 908 909 910 911 912 913 914 915
    await tester.tap(find.byType(dropdownButtonType));
    await tester.pumpAndSettle();
    expect(find.byType(ListView), findsOneWidget);

    window.onMetricsChanged();
    await tester.pump();
    expect(find.byType(ListView, skipOffstage: false), findsNothing);
  });

916
  testWidgets('Semantics Tree contains only selected element', (WidgetTester tester) async {
917
    final SemanticsTester semantics = SemanticsTester(tester);
918
    await tester.pumpWidget(buildFrame(items: menuItems, onChanged: onChanged));
919 920 921 922 923 924 925 926

    expect(semantics, isNot(includesNodeWith(label: menuItems[0])));
    expect(semantics, includesNodeWith(label: menuItems[1]));
    expect(semantics, isNot(includesNodeWith(label: menuItems[2])));
    expect(semantics, isNot(includesNodeWith(label: menuItems[3])));

    semantics.dispose();
  });
927 928 929

  testWidgets('Dropdown button includes semantics', (WidgetTester tester) async {
    final SemanticsHandle handle = tester.ensureSemantics();
930
    const Key key = Key('test');
931 932 933 934
    await tester.pumpWidget(buildFrame(
      buttonKey: key,
      value: null,
      items: menuItems,
935
      onChanged: (String _) { },
936 937 938 939
      hint: const Text('test'),
    ));

    // By default the hint contributes the label.
940
    expect(tester.getSemantics(find.byKey(key)), matchesSemantics(
941 942 943 944 945 946 947 948 949
      isButton: true,
      label: 'test',
      hasTapAction: true,
    ));

    await tester.pumpWidget(buildFrame(
      buttonKey: key,
      value: 'three',
      items: menuItems,
950
      onChanged: onChanged,
951 952 953 954
      hint: const Text('test'),
    ));

    // Displays label of select item and is no longer tappable.
955
    expect(tester.getSemantics(find.byKey(key)), matchesSemantics(
956 957 958 959 960 961 962 963
      isButton: true,
      label: 'three',
      hasTapAction: true,
    ));
    handle.dispose();
  });

  testWidgets('Dropdown menu includes semantics', (WidgetTester tester) async {
964
    final SemanticsTester semantics = SemanticsTester(tester);
965
    const Key key = Key('test');
966 967 968 969
    await tester.pumpWidget(buildFrame(
      buttonKey: key,
      value: null,
      items: menuItems,
970
      onChanged: onChanged,
971 972 973 974
    ));
    await tester.tap(find.byKey(key));
    await tester.pumpAndSettle();

975
    expect(semantics, hasSemantics(TestSemantics.root(
976
      children: <TestSemantics>[
977
        TestSemantics.rootChild(
978
          children: <TestSemantics>[
979
            TestSemantics(
980 981 982 983 984 985
              flags: <SemanticsFlag>[
                SemanticsFlag.scopesRoute,
                SemanticsFlag.namesRoute,
              ],
              label: 'Popup menu',
              children: <TestSemantics>[
986
                TestSemantics(
987
                  children: <TestSemantics>[
988
                    TestSemantics(
989 990 991
                      flags: <SemanticsFlag>[
                        SemanticsFlag.hasImplicitScrolling,
                      ],
992
                      children: <TestSemantics>[
993
                        TestSemantics(
994 995 996 997 998
                          label: 'one',
                          textDirection: TextDirection.ltr,
                          tags: <SemanticsTag>[const SemanticsTag('RenderViewport.twoPane')],
                          actions: <SemanticsAction>[SemanticsAction.tap],
                        ),
999
                        TestSemantics(
1000 1001 1002 1003 1004
                          label: 'two',
                          textDirection: TextDirection.ltr,
                          tags: <SemanticsTag>[const SemanticsTag('RenderViewport.twoPane')],
                          actions: <SemanticsAction>[SemanticsAction.tap],
                        ),
1005
                        TestSemantics(
1006 1007 1008 1009 1010
                          label: 'three',
                          textDirection: TextDirection.ltr,
                          tags: <SemanticsTag>[const SemanticsTag('RenderViewport.twoPane')],
                          actions: <SemanticsAction>[SemanticsAction.tap],
                        ),
1011
                        TestSemantics(
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
                          label: 'four',
                          textDirection: TextDirection.ltr,
                          tags: <SemanticsTag>[const SemanticsTag('RenderViewport.twoPane')],
                          actions: <SemanticsAction>[SemanticsAction.tap],
                        ),
                      ],
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
      ],
    ), ignoreId: true, ignoreRect: true, ignoreTransform: true));
    semantics.dispose();
  });
1029

1030 1031 1032
  testWidgets('disabledHint displays on empty items or onChanged', (WidgetTester tester) async {
    final Key buttonKey = UniqueKey();

1033
    Widget build({ List<String> items, ValueChanged<String> onChanged }) => buildFrame(
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
      items: items,
      onChanged: onChanged,
      buttonKey: buttonKey, value: null,
      hint: const Text('enabled'),
      disabledHint: const Text('disabled'));

    // [disabledHint] should display when [items] is null
    await tester.pumpWidget(build(items: null, onChanged: onChanged));
    expect(find.text('enabled'), findsNothing);
    expect(find.text('disabled'), findsOneWidget);

    // [disabledHint] should display when [items] is an empty list.
    await tester.pumpWidget(build(items: <String>[], onChanged: onChanged));
    expect(find.text('enabled'), findsNothing);
    expect(find.text('disabled'), findsOneWidget);

    // [disabledHint] should display when [onChanged] is null
    await tester.pumpWidget(build(items: menuItems, onChanged: null));
    expect(find.text('enabled'), findsNothing);
    expect(find.text('disabled'), findsOneWidget);
1054
    final RenderBox disabledHintBox = tester.renderObject<RenderBox>(find.byKey(buttonKey));
1055 1056 1057 1058 1059 1060

    // A Dropdown button with a disabled hint should be the same size as a
    // one with a regular enabled hint.
    await tester.pumpWidget(build(items: menuItems, onChanged: onChanged));
    expect(find.text('disabled'), findsNothing);
    expect(find.text('enabled'), findsOneWidget);
1061
    final RenderBox enabledHintBox = tester.renderObject<RenderBox>(find.byKey(buttonKey));
1062 1063 1064 1065
    expect(enabledHintBox.localToGlobal(Offset.zero), equals(disabledHintBox.localToGlobal(Offset.zero)));
    expect(enabledHintBox.size, equals(disabledHintBox.size));
  });

1066 1067 1068 1069 1070 1071 1072
  testWidgets('Dropdown in middle showing middle item', (WidgetTester tester) async {
    final List<DropdownMenuItem<int>> items =
      List<DropdownMenuItem<int>>.generate(100, (int i) =>
        DropdownMenuItem<int>(value: i, child: Text('$i')));

    final DropdownButton<int> button = DropdownButton<int>(
      value: 50,
1073
      onChanged: (int newValue) { },
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
      items: items,
    );

    double getMenuScroll() {
      double scrollPosition;
      final ListView listView = tester.element(find.byType(ListView)).widget;
      final ScrollController scrollController = listView.controller;
      assert(scrollController != null);
      scrollPosition = scrollController.position.pixels;
      assert(scrollPosition != null);
      return scrollPosition;
    }

    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: Align(
            alignment: Alignment.center,
            child: button,
          ),
        ),
      ),
    );

    await tester.tap(find.text('50'));
    await tester.pumpAndSettle();
    expect(getMenuScroll(), 2180.0);
  });

  testWidgets('Dropdown in top showing bottom item', (WidgetTester tester) async {
    final List<DropdownMenuItem<int>> items =
      List<DropdownMenuItem<int>>.generate(100, (int i) =>
        DropdownMenuItem<int>(value: i, child: Text('$i')));

    final DropdownButton<int> button = DropdownButton<int>(
      value: 99,
1110
      onChanged: (int newValue) { },
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
      items: items,
    );

    double getMenuScroll() {
      double scrollPosition;
      final ListView listView = tester.element(find.byType(ListView)).widget;
      final ScrollController scrollController = listView.controller;
      assert(scrollController != null);
      scrollPosition = scrollController.position.pixels;
      assert(scrollPosition != null);
      return scrollPosition;
    }

    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: Align(
            alignment: Alignment.topCenter,
            child: button,
          ),
        ),
      ),
    );

    await tester.tap(find.text('99'));
    await tester.pumpAndSettle();
    expect(getMenuScroll(), 4312.0);
  });

  testWidgets('Dropdown in bottom showing top item', (WidgetTester tester) async {
    final List<DropdownMenuItem<int>> items =
      List<DropdownMenuItem<int>>.generate(100, (int i) =>
        DropdownMenuItem<int>(value: i, child: Text('$i')));

    final DropdownButton<int> button = DropdownButton<int>(
      value: 0,
1147
      onChanged: (int newValue) { },
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
      items: items,
    );

    double getMenuScroll() {
      double scrollPosition;
      final ListView listView = tester.element(find.byType(ListView)).widget;
      final ScrollController scrollController = listView.controller;
      assert(scrollController != null);
      scrollPosition = scrollController.position.pixels;
      assert(scrollPosition != null);
      return scrollPosition;
    }

    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: Align(
            alignment: Alignment.bottomCenter,
            child: button,
          ),
        ),
      ),
    );

    await tester.tap(find.text('0'));
    await tester.pumpAndSettle();
    expect(getMenuScroll(), 0.0);
  });

  testWidgets('Dropdown in center showing bottom item', (WidgetTester tester) async {
    final List<DropdownMenuItem<int>> items =
      List<DropdownMenuItem<int>>.generate(100, (int i) =>
        DropdownMenuItem<int>(value: i, child: Text('$i')));

    final DropdownButton<int> button = DropdownButton<int>(
      value: 99,
1184
      onChanged: (int newValue) { },
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
      items: items,
    );

    double getMenuScroll() {
      double scrollPosition;
      final ListView listView = tester.element(find.byType(ListView)).widget;
      final ScrollController scrollController = listView.controller;
      assert(scrollController != null);
      scrollPosition = scrollController.position.pixels;
      assert(scrollPosition != null);
      return scrollPosition;
    }

    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: Align(
            alignment: Alignment.center,
            child: button,
          ),
        ),
      ),
    );

    await tester.tap(find.text('99'));
    await tester.pumpAndSettle();
    expect(getMenuScroll(), 4312.0);
  });
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257

  testWidgets('Dropdown menu respects parent size limits', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/24417

    int selectedIndex;
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          bottomNavigationBar: const SizedBox(height: 200),
          body: Navigator(
            onGenerateRoute: (RouteSettings settings) {
              return MaterialPageRoute<void>(
                builder: (BuildContext context) {
                  return SafeArea(
                    child: Container(
                      alignment: Alignment.topLeft,
                      // From material/dropdown.dart (menus are unaligned by default):
                      //  _kUnalignedMenuMargin = EdgeInsetsDirectional.only(start: 16.0, end: 24.0)
                      // This padding ensures that the entire menu will be visible
                      padding: const EdgeInsetsDirectional.only(start: 16.0, end: 24.0),
                      child: DropdownButton<int>(
                        value: 12,
                        onChanged: (int i) { selectedIndex = i; },
                        items: List<DropdownMenuItem<int>>.generate(100, (int i) {
                          return DropdownMenuItem<int>(value: i, child: Text('$i'));
                        }),
                      ),
                    ),
                  );
                },
              );
            },
          ),
        ),
      ),
    );

    await tester.tap(find.text('12'));
    await tester.pumpAndSettle();
    expect(selectedIndex, null);

    await tester.tap(find.text('13').last);
    await tester.pumpAndSettle();
    expect(selectedIndex, 13);
  });
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283

  testWidgets('Dropdown button will accept widgets as its underline', (
      WidgetTester tester) async {

    const BoxDecoration decoration = BoxDecoration(
      border: Border(bottom: BorderSide(color: Color(0xFFCCBB00), width: 4.0)),
    );
    const BoxDecoration defaultDecoration = BoxDecoration(
      border: Border(bottom: BorderSide(color: Color(0xFFBDBDBD), width: 0.0)),
    );

    final Widget customUnderline = Container(height: 4.0, decoration: decoration);
    final Key buttonKey = UniqueKey();

    final Finder decoratedBox = find.descendant(
      of: find.byKey(buttonKey),
      matching: find.byType(DecoratedBox),
    );

    await tester.pumpWidget(buildFrame(buttonKey: buttonKey, underline: customUnderline,
        value: 'two', onChanged: onChanged));
    expect(tester.widget<DecoratedBox>(decoratedBox).decoration, decoration);

    await tester.pumpWidget(buildFrame(buttonKey: buttonKey, value: 'two', onChanged: onChanged));
    expect(tester.widget<DecoratedBox>(decoratedBox).decoration, defaultDecoration);
  });
1284
}