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

5
import 'package:flutter/gestures.dart' show DragStartBehavior;
6
import 'package:flutter/material.dart';
7 8
import 'package:flutter/services.dart' show LogicalKeyboardKey;
import 'package:flutter_test/flutter_test.dart';
9

10
import 'states.dart';
11

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
class MaterialLocalizationsDelegate extends LocalizationsDelegate<MaterialLocalizations> {
  @override
  bool isSupported(Locale locale) => true;

  @override
  Future<MaterialLocalizations> load(Locale locale) => DefaultMaterialLocalizations.load(locale);

  @override
  bool shouldReload(MaterialLocalizationsDelegate old) => false;
}

class WidgetsLocalizationsDelegate extends LocalizationsDelegate<WidgetsLocalizations> {
  @override
  bool isSupported(Locale locale) => true;

  @override
  Future<WidgetsLocalizations> load(Locale locale) => DefaultWidgetsLocalizations.load(locale);

  @override
  bool shouldReload(WidgetsLocalizationsDelegate old) => false;
}

34
Widget textFieldBoilerplate({ required Widget child }) {
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  return MaterialApp(
    home: Localizations(
      locale: const Locale('en', 'US'),
      delegates: <LocalizationsDelegate<dynamic>>[
        WidgetsLocalizationsDelegate(),
        MaterialLocalizationsDelegate(),
      ],
      child: Directionality(
        textDirection: TextDirection.ltr,
        child: MediaQuery(
          data: const MediaQueryData(size: Size(800.0, 600.0)),
          child: Center(
            child: Material(
              child: child,
            ),
          ),
        ),
      ),
    ),
  );
}

57
void main() {
58
  testWidgets('ListView control test', (WidgetTester tester) async {
59
    final List<String> log = <String>[];
60

61
    await tester.pumpWidget(
62
      Directionality(
63
        textDirection: TextDirection.ltr,
64
        child: ListView(
65
          dragStartBehavior: DragStartBehavior.down,
66
          children: kStates.map<Widget>((String state) {
67
            return GestureDetector(
68 69 70
              onTap: () {
                log.add(state);
              },
71
              dragStartBehavior: DragStartBehavior.down,
72
              child: Container(
73 74
                height: 200.0,
                color: const Color(0xFF0000FF),
75
                child: Text(state),
76 77 78 79 80 81
              ),
            );
          }).toList(),
        ),
      ),
    );
82 83 84 85 86 87 88

    await tester.tap(find.text('Alabama'));
    expect(log, equals(<String>['Alabama']));
    log.clear();

    expect(find.text('Nevada'), findsNothing);

89
    await tester.drag(find.text('Alabama'), const Offset(0.0, -4000.0));
90 91 92
    await tester.pump();

    expect(find.text('Alabama'), findsNothing);
93
    expect(tester.getCenter(find.text('Massachusetts')), equals(const Offset(400.0, 100.0)));
94 95 96 97 98

    await tester.tap(find.text('Massachusetts'));
    expect(log, equals(<String>['Massachusetts']));
    log.clear();
  });
99

100 101 102 103
  testWidgets('ListView dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
104
      child: ListView(
105
        padding: EdgeInsets.zero,
106 107 108 109 110 111
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        children: focusNodes.map((FocusNode focusNode) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
112 113 114 115
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
116
              ),
117 118 119 120 121 122 123 124 125
            ),
          );
        }).toList(),
      ),
    ));

    final Finder finder = find.byType(TextField).first;
    final TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
126
    expect(textField.focusNode!.hasFocus, isTrue);
127 128 129

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
130
    expect(textField.focusNode!.hasFocus, isFalse);
131 132 133 134 135 136 137
  });

  testWidgets('ListView.builder dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: ListView.builder(
138
        padding: EdgeInsets.zero,
139 140 141 142 143 144 145 146 147 148 149
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        itemCount: focusNodes.length,
        itemBuilder: (BuildContext context,int index) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNodes[index],
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
150
              ),
151 152 153 154 155 156 157 158 159
            ),
          );
        },
      ),
    ));

    final Finder finder = find.byType(TextField).first;
    final TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
160
    expect(textField.focusNode!.hasFocus, isTrue);
161 162 163

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
164
    expect(textField.focusNode!.hasFocus, isFalse);
165 166 167 168 169 170 171
  });

  testWidgets('ListView.custom dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: ListView.custom(
172
        padding: EdgeInsets.zero,
173 174 175 176 177 178 179 180 181 182 183
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        childrenDelegate: SliverChildBuilderDelegate(
          (BuildContext context,int index) {
            return Container(
              height: 50,
              color: Colors.green,
              child: TextField(
                focusNode: focusNodes[index],
                style: const TextStyle(
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
184
                ),
185 186 187 188 189 190 191 192 193 194 195
              ),
            );
          },
          childCount: focusNodes.length,
        ),
      ),
    ));

    final Finder finder = find.byType(TextField).first;
    final TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
196
    expect(textField.focusNode!.hasFocus, isTrue);
197 198 199

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
200
    expect(textField.focusNode!.hasFocus, isFalse);
201 202 203 204 205 206 207
  });

  testWidgets('ListView.separated dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: ListView.separated(
208
        padding: EdgeInsets.zero,
209 210 211 212 213 214 215 216 217 218 219 220
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        itemCount: focusNodes.length,
        separatorBuilder: (BuildContext context, int index) => const Divider(),
        itemBuilder: (BuildContext context,int index) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNodes[index],
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
221
              ),
222 223 224 225 226 227 228 229 230
            ),
          );
        },
      ),
    ));

    final Finder finder = find.byType(TextField).first;
    final TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
231
    expect(textField.focusNode!.hasFocus, isTrue);
232 233 234

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
235
    expect(textField.focusNode!.hasFocus, isFalse);
236 237 238 239 240 241 242
  });

  testWidgets('GridView dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView(
243
        padding: EdgeInsets.zero,
244 245 246 247 248 249 250 251 252 253 254
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        children: focusNodes.map((FocusNode focusNode) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
255
              ),
256 257 258 259 260 261 262 263 264
            ),
          );
        }).toList(),
      ),
    ));

    final Finder finder = find.byType(TextField).first;
    final TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
265
    expect(textField.focusNode!.hasFocus, isTrue);
266 267 268

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
269
    expect(textField.focusNode!.hasFocus, isFalse);
270 271 272 273 274 275 276
  });

  testWidgets('GridView.builder dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView.builder(
277
        padding: EdgeInsets.zero,
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        itemCount: focusNodes.length,
        itemBuilder: (BuildContext context, int index){
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNodes[index],
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
              ),
            ),
          );
        },
      ),
    ));

    final Finder finder = find.byType(TextField).first;
    final TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
300
    expect(textField.focusNode!.hasFocus, isTrue);
301 302 303

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
304
    expect(textField.focusNode!.hasFocus, isFalse);
305 306 307 308 309 310 311
  });

  testWidgets('GridView.count dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView.count(
312
        padding: EdgeInsets.zero,
313 314 315 316 317 318 319 320 321 322 323
        crossAxisCount: 2,
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        children: focusNodes.map((FocusNode focusNode) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
324
              ),
325 326 327 328 329 330 331 332 333
            ),
          );
        }).toList(),
      ),
    ));

    final Finder finder = find.byType(TextField).first;
    final TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
334
    expect(textField.focusNode!.hasFocus, isTrue);
335 336 337

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
338
    expect(textField.focusNode!.hasFocus, isFalse);
339 340 341 342 343 344 345
  });

  testWidgets('GridView.extent dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView.extent(
346
        padding: EdgeInsets.zero,
347 348 349 350 351 352 353 354 355 356 357
        maxCrossAxisExtent: 300,
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        children: focusNodes.map((FocusNode focusNode) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
358
              ),
359 360 361 362 363 364 365 366 367
            ),
          );
        }).toList(),
      ),
    ));

    final Finder finder = find.byType(TextField).first;
    final TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
368
    expect(textField.focusNode!.hasFocus, isTrue);
369 370 371

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
372
    expect(textField.focusNode!.hasFocus, isFalse);
373 374 375 376 377 378 379
  });

  testWidgets('GridView.custom dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView.custom(
380
        padding: EdgeInsets.zero,
381 382 383 384 385 386 387 388 389 390 391 392
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        childrenDelegate: SliverChildBuilderDelegate(
          (BuildContext context,int index) {
            return Container(
              height: 50,
              color: Colors.green,
              child: TextField(
                focusNode: focusNodes[index],
                style: const TextStyle(
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
393
                ),
394 395 396 397 398 399 400
              ),
            );
          },
          childCount: focusNodes.length,
        ),
      ),
    ));
401 402 403 404

    final Finder finder = find.byType(TextField).first;
    final TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
405
    expect(textField.focusNode!.hasFocus, isTrue);
406 407 408

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
409
    expect(textField.focusNode!.hasFocus, isFalse);
410 411 412 413 414 415
  });

  testWidgets('ListView dismiss keyboard manual test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
416 417 418 419 420 421 422 423
      child: ListView(
        padding: EdgeInsets.zero,
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.manual,
        children: focusNodes.map((FocusNode focusNode) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
424 425 426 427
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
428 429 430 431 432 433
              ),
            ),
          );
        }).toList(),
      ),
    ));
434 435 436 437

    final Finder finder = find.byType(TextField).first;
    final TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
438
    expect(textField.focusNode!.hasFocus, isTrue);
439 440

    await tester.drag(finder, const Offset(0.0, -40.0));
441
    await tester.pumpAndSettle();
442
    expect(textField.focusNode!.hasFocus, isTrue);
443 444 445 446 447 448 449
  });

  testWidgets('ListView.builder dismiss keyboard manual test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: ListView.builder(
450
        padding: EdgeInsets.zero,
451 452 453 454 455 456 457 458 459 460 461
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.manual,
        itemCount: focusNodes.length,
        itemBuilder: (BuildContext context,int index) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNodes[index],
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
462
              ),
463 464 465 466 467 468 469 470 471
            ),
          );
        },
      ),
    ));

    final Finder finder = find.byType(TextField).first;
    final TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
472
    expect(textField.focusNode!.hasFocus, isTrue);
473 474 475

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
476
    expect(textField.focusNode!.hasFocus, isTrue);
477 478 479 480 481 482 483
  });

  testWidgets('ListView.custom dismiss keyboard manual test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: ListView.custom(
484
        padding: EdgeInsets.zero,
485 486 487 488 489 490 491 492 493 494 495
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.manual,
        childrenDelegate: SliverChildBuilderDelegate(
          (BuildContext context,int index) {
            return Container(
              height: 50,
              color: Colors.green,
              child: TextField(
                focusNode: focusNodes[index],
                style: const TextStyle(
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
496
                ),
497 498 499 500 501 502 503 504 505 506 507
              ),
            );
          },
          childCount: focusNodes.length,
        ),
      ),
    ));

    final Finder finder = find.byType(TextField).first;
    final TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
508
    expect(textField.focusNode!.hasFocus, isTrue);
509 510 511

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
512
    expect(textField.focusNode!.hasFocus, isTrue);
513 514 515 516 517 518 519
  });

  testWidgets('ListView.separated dismiss keyboard manual test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: ListView.separated(
520
        padding: EdgeInsets.zero,
521 522 523 524 525 526 527 528 529 530 531 532
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.manual,
        itemCount: focusNodes.length,
        separatorBuilder: (BuildContext context, int index) => const Divider(),
        itemBuilder: (BuildContext context,int index) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNodes[index],
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
533
              ),
534 535 536 537 538 539 540 541 542
            ),
          );
        },
      ),
    ));

    final Finder finder = find.byType(TextField).first;
    final TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
543
    expect(textField.focusNode!.hasFocus, isTrue);
544 545 546

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
547
    expect(textField.focusNode!.hasFocus, isTrue);
548 549 550 551 552 553 554
  });

  testWidgets('GridView dismiss keyboard manual test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView(
555
        padding: EdgeInsets.zero,
556 557 558 559 560 561 562 563 564 565 566
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.manual,
        children: focusNodes.map((FocusNode focusNode) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
567
              ),
568 569 570 571 572 573 574 575 576
            ),
          );
        }).toList(),
      ),
    ));

    final Finder finder = find.byType(TextField).first;
    final TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
577
    expect(textField.focusNode!.hasFocus, isTrue);
578 579 580

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
581
    expect(textField.focusNode!.hasFocus, isTrue);
582 583 584 585 586 587 588
  });

  testWidgets('GridView.builder dismiss keyboard manual test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView.builder(
589
        padding: EdgeInsets.zero,
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.manual,
        itemCount: focusNodes.length,
        itemBuilder: (BuildContext context, int index){
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNodes[index],
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
              ),
            ),
          );
        },
      ),
    ));

    final Finder finder = find.byType(TextField).first;
    final TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
612
    expect(textField.focusNode!.hasFocus, isTrue);
613 614 615

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
616
    expect(textField.focusNode!.hasFocus, isTrue);
617 618 619 620 621 622 623
  });

  testWidgets('GridView.count dismiss keyboard manual test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView.count(
624
        padding: EdgeInsets.zero,
625 626 627 628 629 630 631 632 633 634 635
        crossAxisCount: 2,
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.manual,
        children: focusNodes.map((FocusNode focusNode) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
636
              ),
637 638 639 640 641 642 643 644 645
            ),
          );
        }).toList(),
      ),
    ));

    final Finder finder = find.byType(TextField).first;
    final TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
646
    expect(textField.focusNode!.hasFocus, isTrue);
647 648 649

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
650
    expect(textField.focusNode!.hasFocus, isTrue);
651 652 653 654 655 656 657
  });

  testWidgets('GridView.extent dismiss keyboard manual test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView.extent(
658
        padding: EdgeInsets.zero,
659 660 661 662 663 664 665 666 667 668 669
        maxCrossAxisExtent: 300,
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.manual,
        children: focusNodes.map((FocusNode focusNode) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
670
              ),
671 672 673 674 675 676 677 678 679
            ),
          );
        }).toList(),
      ),
    ));

    final Finder finder = find.byType(TextField).first;
    final TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
680
    expect(textField.focusNode!.hasFocus, isTrue);
681 682 683

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
684
    expect(textField.focusNode!.hasFocus, isTrue);
685 686 687 688 689 690 691
  });

  testWidgets('GridView.custom dismiss keyboard manual test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView.custom(
692
        padding: EdgeInsets.zero,
693 694 695 696 697 698 699 700 701 702 703 704
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.manual,
        childrenDelegate: SliverChildBuilderDelegate(
          (BuildContext context,int index) {
            return Container(
              height: 50,
              color: Colors.green,
              child: TextField(
                focusNode: focusNodes[index],
                style: const TextStyle(
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
705
                ),
706 707 708 709 710 711 712 713 714 715 716
              ),
            );
          },
          childCount: focusNodes.length,
        ),
      ),
    ));

    final Finder finder = find.byType(TextField).first;
    final TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
717
    expect(textField.focusNode!.hasFocus, isTrue);
718 719

    await tester.drag(finder, const Offset(0.0, -40.0));
720
    await tester.pumpAndSettle();
721
    expect(textField.focusNode!.hasFocus, isTrue);
722 723
  });

724 725
  testWidgets('ListView restart ballistic activity out of range', (WidgetTester tester) async {
    Widget buildListView(int n) {
726
      return Directionality(
727
        textDirection: TextDirection.ltr,
728
        child: ListView(
729
          dragStartBehavior: DragStartBehavior.down,
730
          children: kStates.take(n).map<Widget>((String state) {
731
            return Container(
732 733
              height: 200.0,
              color: const Color(0xFF0000FF),
734
              child: Text(state),
735 736 737
            );
          }).toList(),
        ),
738 739 740
      );
    }

741 742 743
    await tester.pumpWidget(buildListView(30));
    await tester.fling(find.byType(ListView), const Offset(0.0, -4000.0), 4000.0);
    await tester.pumpWidget(buildListView(15));
744 745 746 747
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
748
    await tester.pumpAndSettle(const Duration(milliseconds: 100));
749

750
    final Viewport viewport = tester.widget(find.byType(Viewport));
751 752
    expect(viewport.offset.pixels, equals(2400.0));
  });
Adam Barth's avatar
Adam Barth committed
753 754

  testWidgets('CustomScrollView control test', (WidgetTester tester) async {
755
    final List<String> log = <String>[];
Adam Barth's avatar
Adam Barth committed
756

757
    await tester.pumpWidget(
758
      Directionality(
759
        textDirection: TextDirection.ltr,
760
        child: CustomScrollView(
761
          dragStartBehavior: DragStartBehavior.down,
762
          slivers: <Widget>[
763 764
            SliverList(
              delegate: SliverChildListDelegate(
765
                kStates.map<Widget>((String state) {
766
                  return GestureDetector(
767
                    dragStartBehavior: DragStartBehavior.down,
768 769 770
                    onTap: () {
                      log.add(state);
                    },
771
                    child: Container(
772 773
                      height: 200.0,
                      color: const Color(0xFF0000FF),
774
                      child: Text(state),
775 776 777 778 779 780
                    ),
                  );
                }).toList(),
              ),
            ),
          ],
Adam Barth's avatar
Adam Barth committed
781
        ),
782 783
      ),
    );
Adam Barth's avatar
Adam Barth committed
784 785 786 787 788 789 790

    await tester.tap(find.text('Alabama'));
    expect(log, equals(<String>['Alabama']));
    log.clear();

    expect(find.text('Nevada'), findsNothing);

791
    await tester.drag(find.text('Alabama'), const Offset(0.0, -4000.0));
Adam Barth's avatar
Adam Barth committed
792 793 794
    await tester.pump();

    expect(find.text('Alabama'), findsNothing);
795
    expect(tester.getCenter(find.text('Massachusetts')), equals(const Offset(400.0, 100.0)));
Adam Barth's avatar
Adam Barth committed
796 797 798 799 800

    await tester.tap(find.text('Massachusetts'));
    expect(log, equals(<String>['Massachusetts']));
    log.clear();
  });
801

802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
  testWidgets('CustomScrollView dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: CustomScrollView(
        dragStartBehavior: DragStartBehavior.down,
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        slivers: <Widget>[
          SliverList(
            delegate: SliverChildListDelegate(
              focusNodes.map((FocusNode focusNode) {
                return Container(
                  height: 50,
                  color: Colors.green,
                  child: TextField(
                    focusNode: focusNode,
                    style: const TextStyle(
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
821
                    ),
822 823 824 825 826 827 828 829 830 831 832 833
                  ),
                );
              }).toList(),
            ),
          ),
        ],
      ),
    ));

    final Finder finder = find.byType(TextField).first;
    final TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
834
    expect(textField.focusNode!.hasFocus, isTrue);
835 836 837

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
838
    expect(textField.focusNode!.hasFocus, isFalse);
839 840
  });

841 842
  testWidgets('Can jumpTo during drag', (WidgetTester tester) async {
    final List<Type> log = <Type>[];
843
    final ScrollController controller = ScrollController();
844

845
    await tester.pumpWidget(
846
      Directionality(
847
        textDirection: TextDirection.ltr,
848
        child: NotificationListener<ScrollNotification>(
849 850 851 852
          onNotification: (ScrollNotification notification) {
            log.add(notification.runtimeType);
            return false;
          },
853
          child: ListView(
854 855
            controller: controller,
            children: kStates.map<Widget>((String state) {
856
              return SizedBox(
857
                height: 200.0,
858
                child: Text(state),
859 860 861 862
              );
            }).toList(),
          ),
        ),
863
      ),
864
    );
865 866 867

    expect(log, isEmpty);

868
    final TestGesture gesture = await tester.startGesture(const Offset(100.0, 100.0));
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
    await gesture.moveBy(const Offset(0.0, -100.0));

    expect(log, equals(<Type>[
      ScrollStartNotification,
      UserScrollNotification,
      ScrollUpdateNotification,
    ]));
    log.clear();

    await tester.pump();

    controller.jumpTo(550.0);

    expect(controller.offset, equals(550.0));
    expect(log, equals(<Type>[
      ScrollEndNotification,
      UserScrollNotification,
      ScrollStartNotification,
      ScrollUpdateNotification,
      ScrollEndNotification,
    ]));
    log.clear();

    await tester.pump();
    await gesture.moveBy(const Offset(0.0, -100.0));

    expect(controller.offset, equals(550.0));
    expect(log, isEmpty);
  });
898

899
  testWidgets('Vertical CustomScrollViews are primary by default', (WidgetTester tester) async {
900
    const CustomScrollView view = CustomScrollView(scrollDirection: Axis.vertical);
901 902 903 904
    expect(view.primary, isTrue);
  });

  testWidgets('Vertical ListViews are primary by default', (WidgetTester tester) async {
905
    final ListView view = ListView(scrollDirection: Axis.vertical);
906 907 908 909
    expect(view.primary, isTrue);
  });

  testWidgets('Vertical GridViews are primary by default', (WidgetTester tester) async {
910
    final GridView view = GridView.count(
911 912 913 914 915 916 917
      scrollDirection: Axis.vertical,
      crossAxisCount: 1,
    );
    expect(view.primary, isTrue);
  });

  testWidgets('Horizontal CustomScrollViews are non-primary by default', (WidgetTester tester) async {
918
    const CustomScrollView view = CustomScrollView(scrollDirection: Axis.horizontal);
919 920 921 922
    expect(view.primary, isFalse);
  });

  testWidgets('Horizontal ListViews are non-primary by default', (WidgetTester tester) async {
923
    final ListView view = ListView(scrollDirection: Axis.horizontal);
924 925 926 927
    expect(view.primary, isFalse);
  });

  testWidgets('Horizontal GridViews are non-primary by default', (WidgetTester tester) async {
928
    final GridView view = GridView.count(
929 930 931 932 933 934 935
      scrollDirection: Axis.horizontal,
      crossAxisCount: 1,
    );
    expect(view.primary, isFalse);
  });

  testWidgets('CustomScrollViews with controllers are non-primary by default', (WidgetTester tester) async {
936 937
    final CustomScrollView view = CustomScrollView(
      controller: ScrollController(),
938 939 940 941 942 943
      scrollDirection: Axis.vertical,
    );
    expect(view.primary, isFalse);
  });

  testWidgets('ListViews with controllers are non-primary by default', (WidgetTester tester) async {
944 945
    final ListView view = ListView(
      controller: ScrollController(),
946 947 948 949 950 951
      scrollDirection: Axis.vertical,
    );
    expect(view.primary, isFalse);
  });

  testWidgets('GridViews with controllers are non-primary by default', (WidgetTester tester) async {
952 953
    final GridView view = GridView.count(
      controller: ScrollController(),
954 955 956 957 958 959
      scrollDirection: Axis.vertical,
      crossAxisCount: 1,
    );
    expect(view.primary, isFalse);
  });

960
  testWidgets('CustomScrollView sets PrimaryScrollController when primary', (WidgetTester tester) async {
961
    final ScrollController primaryScrollController = ScrollController();
962
    await tester.pumpWidget(
963
      Directionality(
964
        textDirection: TextDirection.ltr,
965
        child: PrimaryScrollController(
966
          controller: primaryScrollController,
967
          child: const CustomScrollView(primary: true),
968 969 970
        ),
      ),
    );
971
    final Scrollable scrollable = tester.widget(find.byType(Scrollable));
972 973 974 975
    expect(scrollable.controller, primaryScrollController);
  });

  testWidgets('ListView sets PrimaryScrollController when primary', (WidgetTester tester) async {
976
    final ScrollController primaryScrollController = ScrollController();
977
    await tester.pumpWidget(
978
      Directionality(
979
        textDirection: TextDirection.ltr,
980
        child: PrimaryScrollController(
981
          controller: primaryScrollController,
982
          child: ListView(primary: true),
983 984 985
        ),
      ),
    );
986
    final Scrollable scrollable = tester.widget(find.byType(Scrollable));
987 988 989 990
    expect(scrollable.controller, primaryScrollController);
  });

  testWidgets('GridView sets PrimaryScrollController when primary', (WidgetTester tester) async {
991
    final ScrollController primaryScrollController = ScrollController();
992
    await tester.pumpWidget(
993
      Directionality(
994
        textDirection: TextDirection.ltr,
995
        child: PrimaryScrollController(
996
          controller: primaryScrollController,
997
          child: GridView.count(primary: true, crossAxisCount: 1),
998 999 1000
        ),
      ),
    );
1001
    final Scrollable scrollable = tester.widget(find.byType(Scrollable));
1002 1003
    expect(scrollable.controller, primaryScrollController);
  });
1004 1005

  testWidgets('Nested scrollables have a null PrimaryScrollController', (WidgetTester tester) async {
1006
    const Key innerKey = Key('inner');
1007
    final ScrollController primaryScrollController = ScrollController();
1008
    await tester.pumpWidget(
1009
      Directionality(
1010
        textDirection: TextDirection.ltr,
1011
        child: PrimaryScrollController(
1012
          controller: primaryScrollController,
1013
          child: ListView(
1014 1015
            primary: true,
            children: <Widget>[
1016
              Container(
1017
                constraints: const BoxConstraints(maxHeight: 200.0),
1018
                child: ListView(key: innerKey, primary: true),
1019 1020
              ),
            ],
1021
          ),
1022
        ),
1023
      ),
1024
    );
1025

1026
    final Scrollable innerScrollable = tester.widget(
1027 1028 1029 1030 1031 1032 1033
      find.descendant(
        of: find.byKey(innerKey),
        matching: find.byType(Scrollable),
      ),
    );
    expect(innerScrollable.controller, isNull);
  });
1034 1035

  testWidgets('Primary ListViews are always scrollable', (WidgetTester tester) async {
1036
    final ListView view = ListView(primary: true);
Dan Field's avatar
Dan Field committed
1037
    expect(view.physics, isA<AlwaysScrollableScrollPhysics>());
1038 1039 1040
  });

  testWidgets('Non-primary ListViews are not always scrollable', (WidgetTester tester) async {
1041
    final ListView view = ListView(primary: false);
Dan Field's avatar
Dan Field committed
1042
    expect(view.physics, isNot(isA<AlwaysScrollableScrollPhysics>()));
1043 1044 1045
  });

  testWidgets('Defaulting-to-primary ListViews are always scrollable', (WidgetTester tester) async {
1046
    final ListView view = ListView(scrollDirection: Axis.vertical);
Dan Field's avatar
Dan Field committed
1047
    expect(view.physics, isA<AlwaysScrollableScrollPhysics>());
1048 1049 1050
  });

  testWidgets('Defaulting-to-not-primary ListViews are not always scrollable', (WidgetTester tester) async {
1051
    final ListView view = ListView(scrollDirection: Axis.horizontal);
Dan Field's avatar
Dan Field committed
1052
    expect(view.physics, isNot(isA<AlwaysScrollableScrollPhysics>()));
1053 1054 1055 1056 1057
  });

  testWidgets('primary:true leads to scrolling', (WidgetTester tester) async {
    bool scrolled = false;
    await tester.pumpWidget(
1058
      Directionality(
1059
        textDirection: TextDirection.ltr,
1060
        child: NotificationListener<OverscrollNotification>(
1061 1062 1063 1064
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
1065
          child: ListView(
1066
            primary: true,
1067
            children: const <Widget>[],
1068
          ),
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
        ),
      ),
    );
    await tester.dragFrom(const Offset(100.0, 100.0), const Offset(0.0, 100.0));
    expect(scrolled, isTrue);
  });

  testWidgets('primary:false leads to no scrolling', (WidgetTester tester) async {
    bool scrolled = false;
    await tester.pumpWidget(
1079
      Directionality(
1080
        textDirection: TextDirection.ltr,
1081
        child: NotificationListener<OverscrollNotification>(
1082 1083 1084 1085
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
1086
          child: ListView(
1087
            primary: false,
1088
            children: const <Widget>[],
1089
          ),
1090 1091 1092 1093 1094 1095 1096
        ),
      ),
    );
    await tester.dragFrom(const Offset(100.0, 100.0), const Offset(0.0, 100.0));
    expect(scrolled, isFalse);
  });

Ian Hickson's avatar
Ian Hickson committed
1097
  testWidgets('physics:AlwaysScrollableScrollPhysics actually overrides primary:false default behavior', (WidgetTester tester) async {
1098 1099
    bool scrolled = false;
    await tester.pumpWidget(
1100
      Directionality(
1101
        textDirection: TextDirection.ltr,
1102
        child: NotificationListener<OverscrollNotification>(
1103 1104 1105 1106
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
1107
          child: ListView(
1108 1109
            primary: false,
            physics: const AlwaysScrollableScrollPhysics(),
1110
            children: const <Widget>[],
1111
          ),
1112 1113 1114 1115 1116 1117 1118
        ),
      ),
    );
    await tester.dragFrom(const Offset(100.0, 100.0), const Offset(0.0, 100.0));
    expect(scrolled, isTrue);
  });

Ian Hickson's avatar
Ian Hickson committed
1119
  testWidgets('physics:ScrollPhysics actually overrides primary:true default behavior', (WidgetTester tester) async {
1120 1121
    bool scrolled = false;
    await tester.pumpWidget(
1122
      Directionality(
1123
        textDirection: TextDirection.ltr,
1124
        child: NotificationListener<OverscrollNotification>(
1125 1126 1127 1128
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
1129
          child: ListView(
1130 1131
            primary: true,
            physics: const ScrollPhysics(),
1132
            children: const <Widget>[],
1133
          ),
1134 1135 1136 1137 1138 1139
        ),
      ),
    );
    await tester.dragFrom(const Offset(100.0, 100.0), const Offset(0.0, 100.0));
    expect(scrolled, isFalse);
  });
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153

  testWidgets('separatorBuilder must return something', (WidgetTester tester) async {
    const List<String> listOfValues = <String>['ALPHA', 'BETA', 'GAMMA', 'DELTA'];

    Widget buildFrame(Widget firstSeparator) {
      return MaterialApp(
        home: Material(
          child: ListView.separated(
            itemBuilder: (BuildContext context, int index) {
              return Text(listOfValues[index]);
            },
            separatorBuilder: (BuildContext context, int index) {
              if (index == 0) {
                return firstSeparator;
1154
              } else {
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
                return const Divider();
              }
            },
            itemCount: listOfValues.length,
          ),
        ),
      );
    }

    // A separatorBuilder that always returns a Divider is fine
    await tester.pumpWidget(buildFrame(const Divider()));
    expect(tester.takeException(), isNull);
  });

  testWidgets('when itemBuilder throws, creates Error Widget', (WidgetTester tester) async {
    const List<String> listOfValues = <String>['ALPHA', 'BETA', 'GAMMA', 'DELTA'];

    Widget buildFrame(bool throwOnFirstItem) {
      return MaterialApp(
        home: Material(
          child: ListView.builder(
            itemBuilder: (BuildContext context, int index) {
              if (index == 0 && throwOnFirstItem) {
                throw Exception('itemBuilder fail');
              }
              return Text(listOfValues[index]);
            },
            itemCount: listOfValues.length,
          ),
        ),
      );
    }

    // When itemBuilder doesn't throw, no ErrorWidget
    await tester.pumpWidget(buildFrame(false));
    expect(tester.takeException(), isNull);
    final Finder finder = find.byType(ErrorWidget);
    expect(find.byType(ErrorWidget), findsNothing);

    // When it does throw, one error widget is rendered in the item's place
    await tester.pumpWidget(buildFrame(true));
Dan Field's avatar
Dan Field committed
1196
    expect(tester.takeException(), isA<Exception>());
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
    expect(finder, findsOneWidget);
  });

  testWidgets('when separatorBuilder throws, creates ErrorWidget', (WidgetTester tester) async {
    const List<String> listOfValues = <String>['ALPHA', 'BETA', 'GAMMA', 'DELTA'];
    const Key key = Key('list');

    Widget buildFrame(bool throwOnFirstSeparator) {
      return MaterialApp(
        home: Material(
          child: ListView.separated(
            key: key,
            itemBuilder: (BuildContext context, int index) {
              return Text(listOfValues[index]);
            },
            separatorBuilder: (BuildContext context, int index) {
              if (index == 0 && throwOnFirstSeparator) {
                throw Exception('separatorBuilder fail');
              }
              return const Divider();
            },
            itemCount: listOfValues.length,
          ),
        ),
      );
    }

    // When separatorBuilder doesn't throw, no ErrorWidget
    await tester.pumpWidget(buildFrame(false));
    expect(tester.takeException(), isNull);
    final Finder finder = find.byType(ErrorWidget);
    expect(find.byType(ErrorWidget), findsNothing);

    // When it does throw, one error widget is rendered in the separator's place
    await tester.pumpWidget(buildFrame(true));
Dan Field's avatar
Dan Field committed
1232
    expect(tester.takeException(), isA<Exception>());
1233 1234
    expect(finder, findsOneWidget);
  });
1235

1236
   testWidgets('ListView asserts on both non-null itemExtent and prototypeItem', (WidgetTester tester) async {
1237 1238 1239 1240 1241 1242
    expect(() => ListView(
      itemExtent: 100,
      prototypeItem: const SizedBox(),
    ), throwsAssertionError);
  });

1243 1244 1245 1246 1247 1248
  testWidgets('ListView.builder asserts on negative childCount', (WidgetTester tester) async {
    expect(() => ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        return const SizedBox();
      },
      itemCount: -1,
Dan Field's avatar
Dan Field committed
1249
    ), throwsAssertionError);
1250 1251 1252 1253 1254 1255 1256 1257 1258
  });

  testWidgets('ListView.builder asserts on negative semanticChildCount', (WidgetTester tester) async {
    expect(() => ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        return const SizedBox();
      },
      itemCount: 1,
      semanticChildCount: -1,
Dan Field's avatar
Dan Field committed
1259
    ), throwsAssertionError);
1260 1261 1262 1263 1264 1265 1266 1267 1268
  });

  testWidgets('ListView.builder asserts on nonsensical childCount/semanticChildCount', (WidgetTester tester) async {
    expect(() => ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        return const SizedBox();
      },
      itemCount: 1,
      semanticChildCount: 4,
Dan Field's avatar
Dan Field committed
1269
    ), throwsAssertionError);
1270
  });
1271

1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
  testWidgets('ListView.builder asserts on both non-null itemExtent and prototypeItem', (WidgetTester tester) async {
    expect(() => ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        return const SizedBox();
      },
      itemExtent: 100,
      prototypeItem: const SizedBox(),
    ), throwsAssertionError);
  });

  testWidgets('ListView.custom asserts on both non-null itemExtent and prototypeItem', (WidgetTester tester) async {
    expect(() => ListView.custom(
      childrenDelegate: SliverChildBuilderDelegate(
        (BuildContext context, int index) {
          return const SizedBox();
        },
      ),
      itemExtent: 100,
      prototypeItem: const SizedBox(),
    ), throwsAssertionError);
  });

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
  testWidgets('PrimaryScrollController provides fallback ScrollActions', (WidgetTester tester)  async {
    await tester.pumpWidget(
      MaterialApp(
        home: CustomScrollView(
          primary: true,
          slivers: List<Widget>.generate(
            20,
            (int index) {
              return SliverToBoxAdapter(
                child: Focus(
                  autofocus: index == 0,
                  child: SizedBox(key: ValueKey<String>('Box $index'), height: 50.0),
                ),
              );
            },
          ),
        ),
      ),
    );
    final ScrollController controller = PrimaryScrollController.of(
1314
      tester.element(find.byType(CustomScrollView)),
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
    )!;
    await tester.pumpAndSettle();
    expect(controller.position.pixels, equals(0.0));
    expect(
      tester.getRect(find.byKey(const ValueKey<String>('Box 0'), skipOffstage: false)),
      equals(const Rect.fromLTRB(0.0, 0.0, 800.0, 50.0)),
    );
    await tester.sendKeyEvent(LogicalKeyboardKey.pageDown);
    await tester.pumpAndSettle();
    expect(controller.position.pixels, equals(400.0));
    expect(
      tester.getRect(find.byKey(const ValueKey<String>('Box 0'), skipOffstage: false)),
      equals(const Rect.fromLTRB(0.0, -400.0, 800.0, -350.0)),
    );
    await tester.sendKeyEvent(LogicalKeyboardKey.pageUp);
    await tester.pumpAndSettle();
    expect(controller.position.pixels, equals(0.0));
    expect(
      tester.getRect(find.byKey(const ValueKey<String>('Box 0'), skipOffstage: false)),
      equals(const Rect.fromLTRB(0.0, 0.0, 800.0, 50.0)),
    );
  });
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 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

  testWidgets('if itemExtent is non-null, children have same extent in the scroll direction', (WidgetTester tester) async {
    final List<int> numbers = <int>[0,1,2];

    await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: StatefulBuilder(
              builder: (BuildContext context, StateSetter setState) {
                return ListView.builder(
                  itemBuilder: (BuildContext context, int index) {
                    return SizedBox(
                        key: ValueKey<int>(numbers[index]),
                        // children with different heights
                        height: 20 + numbers[index] * 10,
                        child: ReorderableDragStartListener(
                          index: index,
                          child: Text(numbers[index].toString()),
                        )
                    );
                  },
                  itemCount: numbers.length,
                  itemExtent: 30,
                );
              },
            ),
          ),
        )
    );

    final double item0Height = tester.getSize(find.text('0').hitTestable()).height;
    final double item1Height = tester.getSize(find.text('1').hitTestable()).height;
    final double item2Height = tester.getSize(find.text('2').hitTestable()).height;

    expect(item0Height, 30.0);
    expect(item1Height, 30.0);
    expect(item2Height, 30.0);
  });
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415

  testWidgets('if prototypeItem is non-null, children have same extent in the scroll direction', (WidgetTester tester) async {
    final List<int> numbers = <int>[0,1,2];

    await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: StatefulBuilder(
              builder: (BuildContext context, StateSetter setState) {
                return ListView.builder(
                  itemBuilder: (BuildContext context, int index) {
                    return SizedBox(
                        key: ValueKey<int>(numbers[index]),
                        // children with different heights
                        height: 20 + numbers[index] * 10,
                        child: ReorderableDragStartListener(
                          index: index,
                          child: Text(numbers[index].toString()),
                        )
                    );
                  },
                  itemCount: numbers.length,
                  prototypeItem: const SizedBox(
                      height: 30,
                      child: Text('3'),
                  ),
                );
              },
            ),
          ),
        )
    );

    final double item0Height = tester.getSize(find.text('0').hitTestable()).height;
    final double item1Height = tester.getSize(find.text('1').hitTestable()).height;
    final double item2Height = tester.getSize(find.text('2').hitTestable()).height;

    expect(item0Height, 30.0);
    expect(item1Height, 30.0);
    expect(item2Height, 30.0);
  });
1416
}