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

Adam Barth's avatar
Adam Barth committed
5
import 'package:flutter_test/flutter_test.dart';
6 7
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
8
import 'package:flutter/rendering.dart';
9
import 'package:flutter/physics.dart';
10

11
import '../rendering/mock_canvas.dart';
12
import '../rendering/recording_canvas.dart';
13
import '../widgets/semantics_tester.dart';
14

15
Widget boilerplate({ Widget child, TextDirection textDirection = TextDirection.ltr }) {
16
  return Localizations(
17
    locale: const Locale('en', 'US'),
18
    delegates: const <LocalizationsDelegate<dynamic>>[
19 20 21
      DefaultMaterialLocalizations.delegate,
      DefaultWidgetsLocalizations.delegate,
    ],
22
    child: Directionality(
23
      textDirection: textDirection,
24
      child: Material(
25 26
        child: child,
      ),
27 28 29 30
    ),
  );
}

Adam Barth's avatar
Adam Barth committed
31
class StateMarker extends StatefulWidget {
32
  const StateMarker({ Key key, this.child }) : super(key: key);
Adam Barth's avatar
Adam Barth committed
33 34 35 36

  final Widget child;

  @override
37
  StateMarkerState createState() => StateMarkerState();
Adam Barth's avatar
Adam Barth committed
38 39 40 41 42 43 44
}

class StateMarkerState extends State<StateMarker> {
  String marker;

  @override
  Widget build(BuildContext context) {
45 46
    if (widget.child != null)
      return widget.child;
47
    return Container();
Adam Barth's avatar
Adam Barth committed
48 49 50
  }
}

51
class AlwaysKeepAliveWidget extends StatefulWidget {
52
  const AlwaysKeepAliveWidget({ Key key}) : super(key: key);
53 54
  static String text = 'AlwaysKeepAlive';
  @override
55
  AlwaysKeepAliveState createState() => AlwaysKeepAliveState();
56 57 58 59 60 61 62 63 64
}

class AlwaysKeepAliveState extends State<AlwaysKeepAliveWidget>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
65
    super.build(context);
66
    return Text(AlwaysKeepAliveWidget.text);
67 68 69
  }
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
class _NestedTabBarContainer extends StatelessWidget {
  const _NestedTabBarContainer({
    this.tabController,
  });

  final TabController tabController;

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.blue,
      child: Column(
        children: <Widget>[
          TabBar(
            controller: tabController,
            tabs: const <Tab>[
              Tab(text: 'Yellow'),
              Tab(text: 'Grey'),
            ],
          ),
          Expanded(
            flex: 1,
            child: TabBarView(
              controller: tabController,
              children: <Widget>[
                Container(color: Colors.yellow),
                Container(color: Colors.grey),
              ],
            ),
99
          ),
100 101 102 103 104 105
        ],
      ),
    );
  }
}

106
Widget buildFrame({
107 108 109 110 111 112
  Key tabBarKey,
  List<String> tabs,
  String value,
  bool isScrollable = false,
  Color indicatorColor,
}) {
113
  return boilerplate(
114
    child: DefaultTabController(
Hans Muller's avatar
Hans Muller committed
115 116
      initialIndex: tabs.indexOf(value),
      length: tabs.length,
117
      child: TabBar(
Hans Muller's avatar
Hans Muller committed
118
        key: tabBarKey,
119
        tabs: tabs.map<Widget>((String tab) => Tab(text: tab)).toList(),
Hans Muller's avatar
Hans Muller committed
120
        isScrollable: isScrollable,
121
        indicatorColor: indicatorColor,
Hans Muller's avatar
Hans Muller committed
122 123
      ),
    ),
124 125 126
  );
}

127
typedef TabControllerFrameBuilder = Widget Function(BuildContext context, TabController controller);
Hans Muller's avatar
Hans Muller committed
128 129

class TabControllerFrame extends StatefulWidget {
130 131 132 133 134 135
  const TabControllerFrame({
    Key key,
    this.length,
    this.initialIndex = 0,
    this.builder,
  }) : super(key: key);
Hans Muller's avatar
Hans Muller committed
136 137 138 139 140 141

  final int length;
  final int initialIndex;
  final TabControllerFrameBuilder builder;

  @override
142
  TabControllerFrameState createState() => TabControllerFrameState();
Hans Muller's avatar
Hans Muller committed
143 144 145 146 147 148 149 150
}

class TabControllerFrameState extends State<TabControllerFrame> with SingleTickerProviderStateMixin {
  TabController _controller;

  @override
  void initState() {
    super.initState();
151
    _controller = TabController(
Hans Muller's avatar
Hans Muller committed
152
      vsync: this,
153 154
      length: widget.length,
      initialIndex: widget.initialIndex,
Hans Muller's avatar
Hans Muller committed
155 156 157 158 159 160 161 162 163 164 165
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
166
    return widget.builder(context, _controller);
Hans Muller's avatar
Hans Muller committed
167 168
  }
}
169 170

Widget buildLeftRightApp({ List<String> tabs, String value }) {
171 172 173
  return MaterialApp(
    theme: ThemeData(platform: TargetPlatform.android),
    home: DefaultTabController(
Hans Muller's avatar
Hans Muller committed
174 175
      initialIndex: tabs.indexOf(value),
      length: tabs.length,
176 177
      child: Scaffold(
        appBar: AppBar(
178
          title: const Text('tabs'),
179
          bottom: TabBar(
180
            tabs: tabs.map<Widget>((String tab) => Tab(text: tab)).toList(),
Hans Muller's avatar
Hans Muller committed
181
          ),
182
        ),
183
        body: const TabBarView(
184 185
          children: <Widget>[
            Center(child: Text('LEFT CHILD')),
186 187 188 189 190
            Center(child: Text('RIGHT CHILD')),
          ],
        ),
      ),
    ),
191 192 193
  );
}

194 195 196 197 198 199 200
class TabIndicatorRecordingCanvas extends TestRecordingCanvas {
  TabIndicatorRecordingCanvas(this.indicatorColor);

  final Color indicatorColor;
  Rect indicatorRect;

  @override
201 202 203
  void drawLine(Offset p1, Offset p2, Paint paint) {
    // Assuming that the indicatorWeight is 2.0, the default.
    const double indicatorWeight = 2.0;
204
    if (paint.color == indicatorColor)
205
      indicatorRect = Rect.fromPoints(p1, p2).inflate(indicatorWeight / 2.0);
206 207 208
  }
}

209 210
class TestScrollPhysics extends ScrollPhysics {
  const TestScrollPhysics({ ScrollPhysics parent }) : super(parent: parent);
211

212 213
  @override
  TestScrollPhysics applyTo(ScrollPhysics ancestor) {
214
    return TestScrollPhysics(parent: buildParent(ancestor));
215
  }
216

217
  static final SpringDescription _kDefaultSpring = SpringDescription.withDampingRatio(
218
    mass: 0.5,
219
    stiffness: 500.0,
220 221
    ratio: 1.1,
  );
222

223 224 225 226
  @override
  SpringDescription get spring => _kDefaultSpring;
}

227
void main() {
228 229 230 231
  setUp(() {
    debugResetSemanticsIdCounter();
  });

Ian Hickson's avatar
Ian Hickson committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
  testWidgets('Tab sizing - icon', (WidgetTester tester) async {
    await tester.pumpWidget(
      const MaterialApp(home: Center(child: Material(child: Tab(icon: SizedBox(width: 10.0, height: 10.0))))),
    );
    expect(tester.getSize(find.byType(Tab)), const Size(10.0, 46.0));
  });

  testWidgets('Tab sizing - child', (WidgetTester tester) async {
    await tester.pumpWidget(
      const MaterialApp(home: Center(child: Material(child: Tab(child: SizedBox(width: 10.0, height: 10.0))))),
    );
    expect(tester.getSize(find.byType(Tab)), const Size(10.0, 46.0));
  });

  testWidgets('Tab sizing - text', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(theme: ThemeData(fontFamily: 'Ahem'), home: const Center(child: Material(child: Tab(text: 'x')))),
    );
    expect(tester.renderObject<RenderParagraph>(find.byType(RichText)).text.style.fontFamily, 'Ahem');
    expect(tester.getSize(find.byType(Tab)), const Size(14.0, 46.0));
252
  }, skip: isBrowser);
Ian Hickson's avatar
Ian Hickson committed
253 254 255 256 257 258 259

  testWidgets('Tab sizing - icon and text', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(theme: ThemeData(fontFamily: 'Ahem'), home: const Center(child: Material(child: Tab(icon: SizedBox(width: 10.0, height: 10.0), text: 'x')))),
    );
    expect(tester.renderObject<RenderParagraph>(find.byType(RichText)).text.style.fontFamily, 'Ahem');
    expect(tester.getSize(find.byType(Tab)), const Size(14.0, 72.0));
260
  }, skip: isBrowser);
Ian Hickson's avatar
Ian Hickson committed
261

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
  testWidgets('Tab sizing - icon, iconMargin and text', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(fontFamily: 'Ahem'),
        home: const Center(
          child: Material(
            child: Tab(
              icon: SizedBox(
                width: 10.0,
                height: 10.0,
              ),
              iconMargin: EdgeInsets.symmetric(
                horizontal: 100.0,
              ),
              text: 'x',
            ),
          ),
        ),
      ),
    );
    expect(tester.renderObject<RenderParagraph>(find.byType(RichText)).text.style.fontFamily, 'Ahem');
    expect(tester.getSize(find.byType(Tab)), const Size(210.0, 72.0));
  }, skip: isBrowser);

Ian Hickson's avatar
Ian Hickson committed
286 287 288 289 290 291
  testWidgets('Tab sizing - icon and child', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(theme: ThemeData(fontFamily: 'Ahem'), home: const Center(child: Material(child: Tab(icon: SizedBox(width: 10.0, height: 10.0), child: Text('x'))))),
    );
    expect(tester.renderObject<RenderParagraph>(find.byType(RichText)).text.style.fontFamily, 'Ahem');
    expect(tester.getSize(find.byType(Tab)), const Size(14.0, 72.0));
292
  }, skip: isBrowser);
Ian Hickson's avatar
Ian Hickson committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

  testWidgets('Tab color - normal', (WidgetTester tester) async {
    final Widget tabBar = TabBar(tabs: const <Widget>[SizedBox.shrink()], controller: TabController(length: 1, vsync: tester));
    await tester.pumpWidget(
      MaterialApp(home: Material(child: tabBar)),
    );
    expect(find.byType(TabBar), paints..line(color: Colors.blue[500]));
  });

  testWidgets('Tab color - match', (WidgetTester tester) async {
    final Widget tabBar = TabBar(tabs: const <Widget>[SizedBox.shrink()], controller: TabController(length: 1, vsync: tester));
    await tester.pumpWidget(
      MaterialApp(home: Material(color: const Color(0xff2196f3), child: tabBar)),
    );
    expect(find.byType(TabBar), paints..line(color: Colors.white));
  });

  testWidgets('Tab color - transparency', (WidgetTester tester) async {
    final Widget tabBar = TabBar(tabs: const <Widget>[SizedBox.shrink()], controller: TabController(length: 1, vsync: tester));
    await tester.pumpWidget(
      MaterialApp(home: Material(type: MaterialType.transparency, child: tabBar)),
    );
    expect(find.byType(TabBar), paints..line(color: Colors.blue[500]));
  });

318
  testWidgets('TabBar tap selects tab', (WidgetTester tester) async {
319
    final List<String> tabs = <String>['A', 'B', 'C'];
320

321
    await tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: false));
322 323 324
    expect(find.text('A'), findsOneWidget);
    expect(find.text('B'), findsOneWidget);
    expect(find.text('C'), findsOneWidget);
325
    final TabController controller = DefaultTabController.of(tester.element(find.text('A')));
Hans Muller's avatar
Hans Muller committed
326 327 328
    expect(controller, isNotNull);
    expect(controller.index, 2);
    expect(controller.previousIndex, 2);
329

Hans Muller's avatar
Hans Muller committed
330
    await tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: false));
331 332
    await tester.tap(find.text('B'));
    await tester.pump();
Hans Muller's avatar
Hans Muller committed
333
    expect(controller.indexIsChanging, true);
334
    await tester.pump(const Duration(seconds: 1)); // finish the animation
Hans Muller's avatar
Hans Muller committed
335 336 337
    expect(controller.index, 1);
    expect(controller.previousIndex, 2);
    expect(controller.indexIsChanging, false);
338

339 340 341 342
    await tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: false));
    await tester.tap(find.text('C'));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
Hans Muller's avatar
Hans Muller committed
343 344
    expect(controller.index, 2);
    expect(controller.previousIndex, 1);
345

346 347 348 349
    await tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: false));
    await tester.tap(find.text('A'));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
Hans Muller's avatar
Hans Muller committed
350 351
    expect(controller.index, 0);
    expect(controller.previousIndex, 2);
352 353
  });

354
  testWidgets('Scrollable TabBar tap selects tab', (WidgetTester tester) async {
355
    final List<String> tabs = <String>['A', 'B', 'C'];
356

357
    await tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: true));
358 359 360
    expect(find.text('A'), findsOneWidget);
    expect(find.text('B'), findsOneWidget);
    expect(find.text('C'), findsOneWidget);
361
    final TabController controller = DefaultTabController.of(tester.element(find.text('A')));
Hans Muller's avatar
Hans Muller committed
362 363
    expect(controller.index, 2);
    expect(controller.previousIndex, 2);
364

Hans Muller's avatar
Hans Muller committed
365
    await tester.tap(find.text('C'));
366
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
367
    expect(controller.index, 2);
368

Hans Muller's avatar
Hans Muller committed
369
    await tester.tap(find.text('B'));
370
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
371
    expect(controller.index, 1);
372

373
    await tester.tap(find.text('A'));
374
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
375
    expect(controller.index, 0);
376
  });
Hans Muller's avatar
Hans Muller committed
377

378
  testWidgets('Scrollable TabBar tap centers selected tab', (WidgetTester tester) async {
379
    final List<String> tabs = <String>['AAAAAA', 'BBBBBB', 'CCCCCC', 'DDDDDD', 'EEEEEE', 'FFFFFF', 'GGGGGG', 'HHHHHH', 'IIIIII', 'JJJJJJ', 'KKKKKK', 'LLLLLL'];
380
    const Key tabBarKey = Key('TabBar');
381
    await tester.pumpWidget(buildFrame(tabs: tabs, value: 'AAAAAA', isScrollable: true, tabBarKey: tabBarKey));
382
    final TabController controller = DefaultTabController.of(tester.element(find.text('AAAAAA')));
Hans Muller's avatar
Hans Muller committed
383 384
    expect(controller, isNotNull);
    expect(controller.index, 0);
385 386 387

    expect(tester.getSize(find.byKey(tabBarKey)).width, equals(800.0));
    // The center of the FFFFFF item is to the right of the TabBar's center
388
    expect(tester.getCenter(find.text('FFFFFF')).dx, greaterThan(401.0));
389

390
    await tester.tap(find.text('FFFFFF'));
391
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
392
    expect(controller.index, 5);
393
    // The center of the FFFFFF item is now at the TabBar's center
394
    expect(tester.getCenter(find.text('FFFFFF')).dx, closeTo(400.0, 1.0));
Hans Muller's avatar
Hans Muller committed
395 396 397
  });


398
  testWidgets('TabBar can be scrolled independent of the selection', (WidgetTester tester) async {
399
    final List<String> tabs = <String>['AAAA', 'BBBB', 'CCCC', 'DDDD', 'EEEE', 'FFFF', 'GGGG', 'HHHH', 'IIII', 'JJJJ', 'KKKK', 'LLLL'];
400
    const Key tabBarKey = Key('TabBar');
401
    await tester.pumpWidget(buildFrame(tabs: tabs, value: 'AAAA', isScrollable: true, tabBarKey: tabBarKey));
402
    final TabController controller = DefaultTabController.of(tester.element(find.text('AAAA')));
Hans Muller's avatar
Hans Muller committed
403 404
    expect(controller, isNotNull);
    expect(controller.index, 0);
405 406

    // Fling-scroll the TabBar to the left
407
    expect(tester.getCenter(find.text('HHHH')).dx, lessThan(700.0));
408
    await tester.fling(find.byKey(tabBarKey), const Offset(-200.0, 0.0), 10000.0);
409 410
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the scroll animation
411
    expect(tester.getCenter(find.text('HHHH')).dx, lessThan(500.0));
412 413

    // Scrolling the TabBar doesn't change the selection
Hans Muller's avatar
Hans Muller committed
414
    expect(controller.index, 0);
415
  }, skip: isBrowser);
Adam Barth's avatar
Adam Barth committed
416

Hans Muller's avatar
Hans Muller committed
417
  testWidgets('TabBarView maintains state', (WidgetTester tester) async {
418
    final List<String> tabs = <String>['AAAAAA', 'BBBBBB', 'CCCCCC', 'DDDDDD', 'EEEEEE'];
419 420 421
    String value = tabs[0];

    Widget builder() {
422
      return boilerplate(
423
        child: DefaultTabController(
Hans Muller's avatar
Hans Muller committed
424 425
          initialIndex: tabs.indexOf(value),
          length: tabs.length,
426
          child: TabBarView(
427
            children: tabs.map<Widget>((String name) {
428
              return StateMarker(
429
                child: Text(name),
430
              );
431
            }).toList(),
Hans Muller's avatar
Hans Muller committed
432 433
          ),
        ),
434 435 436 437
      );
    }

    StateMarkerState findStateMarkerState(String name) {
438
      return tester.state(find.widgetWithText(StateMarker, name, skipOffstage: false));
439 440
    }

441
    await tester.pumpWidget(builder());
442
    final TabController controller = DefaultTabController.of(tester.element(find.text('AAAAAA')));
Hans Muller's avatar
Hans Muller committed
443

444
    TestGesture gesture = await tester.startGesture(tester.getCenter(find.text(tabs[0])));
445
    await gesture.moveBy(const Offset(-600.0, 0.0));
446
    await tester.pump();
447 448
    expect(value, equals(tabs[0]));
    findStateMarkerState(tabs[1]).marker = 'marked';
449 450 451
    await gesture.up();
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
Hans Muller's avatar
Hans Muller committed
452
    value = tabs[controller.index];
453
    expect(value, equals(tabs[1]));
454
    await tester.pumpWidget(builder());
455 456 457 458
    expect(findStateMarkerState(tabs[1]).marker, equals('marked'));

    // Move to the third tab.

459
    gesture = await tester.startGesture(tester.getCenter(find.text(tabs[1])));
460
    await gesture.moveBy(const Offset(-600.0, 0.0));
461 462
    await gesture.up();
    await tester.pump();
463
    expect(findStateMarkerState(tabs[1]).marker, equals('marked'));
464
    await tester.pump(const Duration(seconds: 1));
Hans Muller's avatar
Hans Muller committed
465
    value = tabs[controller.index];
466
    expect(value, equals(tabs[2]));
467
    await tester.pumpWidget(builder());
468 469 470 471 472 473 474

    // The state is now gone.

    expect(find.text(tabs[1]), findsNothing);

    // Move back to the second tab.

475
    gesture = await tester.startGesture(tester.getCenter(find.text(tabs[2])));
476
    await gesture.moveBy(const Offset(600.0, 0.0));
477
    await tester.pump();
478
    final StateMarkerState markerState = findStateMarkerState(tabs[1]);
479 480
    expect(markerState.marker, isNull);
    markerState.marker = 'marked';
481 482 483
    await gesture.up();
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
Hans Muller's avatar
Hans Muller committed
484
    value = tabs[controller.index];
485
    expect(value, equals(tabs[1]));
486
    await tester.pumpWidget(builder());
487
    expect(findStateMarkerState(tabs[1]).marker, equals('marked'));
Adam Barth's avatar
Adam Barth committed
488
  });
489 490

  testWidgets('TabBar left/right fling', (WidgetTester tester) async {
491
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
492 493 494 495 496 497 498

    await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT'));
    expect(find.text('LEFT'), findsOneWidget);
    expect(find.text('RIGHT'), findsOneWidget);
    expect(find.text('LEFT CHILD'), findsOneWidget);
    expect(find.text('RIGHT CHILD'), findsNothing);

499
    final TabController controller = DefaultTabController.of(tester.element(find.text('LEFT')));
Hans Muller's avatar
Hans Muller committed
500
    expect(controller.index, 0);
501 502

    // Fling to the left, switch from the 'LEFT' tab to the 'RIGHT'
503
    Offset flingStart = tester.getCenter(find.text('LEFT CHILD'));
504
    await tester.flingFrom(flingStart, const Offset(-200.0, 0.0), 10000.0);
505
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
506
    expect(controller.index, 1);
507 508 509 510 511
    expect(find.text('LEFT CHILD'), findsNothing);
    expect(find.text('RIGHT CHILD'), findsOneWidget);

    // Fling to the right, switch back to the 'LEFT' tab
    flingStart = tester.getCenter(find.text('RIGHT CHILD'));
512
    await tester.flingFrom(flingStart, const Offset(200.0, 0.0), 10000.0);
513
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
514
    expect(controller.index, 0);
515 516 517 518
    expect(find.text('LEFT CHILD'), findsOneWidget);
    expect(find.text('RIGHT CHILD'), findsNothing);
  });

519
  testWidgets('TabBar left/right fling reverse (1)', (WidgetTester tester) async {
520
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
521 522 523 524 525 526 527

    await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT'));
    expect(find.text('LEFT'), findsOneWidget);
    expect(find.text('RIGHT'), findsOneWidget);
    expect(find.text('LEFT CHILD'), findsOneWidget);
    expect(find.text('RIGHT CHILD'), findsNothing);

528
    final TabController controller = DefaultTabController.of(tester.element(find.text('LEFT')));
529 530
    expect(controller.index, 0);

531
    final Offset flingStart = tester.getCenter(find.text('LEFT CHILD'));
532 533 534 535 536 537 538 539 540
    await tester.flingFrom(flingStart, const Offset(200.0, 0.0), 10000.0);
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the scroll animation
    expect(controller.index, 0);
    expect(find.text('LEFT CHILD'), findsOneWidget);
    expect(find.text('RIGHT CHILD'), findsNothing);
  });

  testWidgets('TabBar left/right fling reverse (2)', (WidgetTester tester) async {
541
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
542 543 544 545 546 547 548

    await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT'));
    expect(find.text('LEFT'), findsOneWidget);
    expect(find.text('RIGHT'), findsOneWidget);
    expect(find.text('LEFT CHILD'), findsOneWidget);
    expect(find.text('RIGHT CHILD'), findsNothing);

549
    final TabController controller = DefaultTabController.of(tester.element(find.text('LEFT')));
550 551
    expect(controller.index, 0);

552
    final Offset flingStart = tester.getCenter(find.text('LEFT CHILD'));
553 554 555 556 557 558 559 560 561
    await tester.flingFrom(flingStart, const Offset(-200.0, 0.0), 10000.0);
    await tester.pump();
    // this is similar to a test above, but that one does many more pumps
    await tester.pump(const Duration(seconds: 1)); // finish the scroll animation
    expect(controller.index, 1);
    expect(find.text('LEFT CHILD'), findsNothing);
    expect(find.text('RIGHT CHILD'), findsOneWidget);
  });

562
  // A regression test for https://github.com/flutter/flutter/issues/5095
563
  testWidgets('TabBar left/right fling reverse (2)', (WidgetTester tester) async {
564
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
565 566 567 568 569 570 571

    await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT'));
    expect(find.text('LEFT'), findsOneWidget);
    expect(find.text('RIGHT'), findsOneWidget);
    expect(find.text('LEFT CHILD'), findsOneWidget);
    expect(find.text('RIGHT CHILD'), findsNothing);

572
    final TabController controller = DefaultTabController.of(tester.element(find.text('LEFT')));
Hans Muller's avatar
Hans Muller committed
573
    expect(controller.index, 0);
574

575
    final Offset flingStart = tester.getCenter(find.text('LEFT CHILD'));
576
    final TestGesture gesture = await tester.startGesture(flingStart);
577 578 579 580
    for (int index = 0; index > 50; index += 1) {
      await gesture.moveBy(const Offset(-10.0, 0.0));
      await tester.pump(const Duration(milliseconds: 1));
    }
581 582 583
    // End the fling by reversing direction. This should cause not cause
    // a change to the selected tab, everything should just settle back to
    // to where it started.
584 585 586 587 588
    for (int index = 0; index > 50; index += 1) {
      await gesture.moveBy(const Offset(10.0, 0.0));
      await tester.pump(const Duration(milliseconds: 1));
    }
    await gesture.up();
589 590
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the scroll animation
Hans Muller's avatar
Hans Muller committed
591
    expect(controller.index, 0);
592 593 594 595
    expect(find.text('LEFT CHILD'), findsOneWidget);
    expect(find.text('RIGHT CHILD'), findsNothing);
  });

596 597
  // A regression test for https://github.com/flutter/flutter/issues/7133
  testWidgets('TabBar fling velocity', (WidgetTester tester) async {
598
    final List<String> tabs = <String>['AAAAAA', 'BBBBBB', 'CCCCCC', 'DDDDDD', 'EEEEEE', 'FFFFFF', 'GGGGGG', 'HHHHHH', 'IIIIII', 'JJJJJJ', 'KKKKKK', 'LLLLLL'];
599 600 601
    int index = 0;

    await tester.pumpWidget(
602 603
      MaterialApp(
        home: Align(
604
          alignment: Alignment.topLeft,
605
          child: SizedBox(
606 607
            width: 300.0,
            height: 200.0,
608
            child: DefaultTabController(
Hans Muller's avatar
Hans Muller committed
609
              length: tabs.length,
610 611
              child: Scaffold(
                appBar: AppBar(
612
                  title: const Text('tabs'),
613
                  bottom: TabBar(
614
                    isScrollable: true,
615
                    tabs: tabs.map<Widget>((String tab) => Tab(text: tab)).toList(),
616 617
                  ),
                ),
618
                body: TabBarView(
619
                  children: tabs.map<Widget>((String name) => Text('${index++}')).toList(),
620 621 622 623 624 625 626 627 628
                ),
              ),
            ),
          ),
        ),
      ),
    );

    // After a small slow fling to the left, we expect the second item to still be visible.
629
    await tester.fling(find.text('AAAAAA'), const Offset(-25.0, 0.0), 100.0);
630 631 632
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the scroll animation
    final RenderBox box = tester.renderObject(find.text('BBBBBB'));
633
    expect(box.localToGlobal(Offset.zero).dx, greaterThan(0.0));
634
  });
Hans Muller's avatar
Hans Muller committed
635 636

  testWidgets('TabController change notification', (WidgetTester tester) async {
637
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
Hans Muller's avatar
Hans Muller committed
638 639

    await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT'));
640
    final TabController controller = DefaultTabController.of(tester.element(find.text('LEFT')));
Hans Muller's avatar
Hans Muller committed
641 642 643 644 645 646 647 648 649 650

    expect(controller, isNotNull);
    expect(controller.index, 0);

    String value;
    controller.addListener(() {
      value = tabs[controller.index];
    });

    await tester.tap(find.text('RIGHT'));
651
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
652 653 654
    expect(value, 'RIGHT');

    await tester.tap(find.text('LEFT'));
655
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
656 657
    expect(value, 'LEFT');

658
    final Offset leftFlingStart = tester.getCenter(find.text('LEFT CHILD'));
Hans Muller's avatar
Hans Muller committed
659
    await tester.flingFrom(leftFlingStart, const Offset(-200.0, 0.0), 10000.0);
660
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
661 662
    expect(value, 'RIGHT');

663
    final Offset rightFlingStart = tester.getCenter(find.text('RIGHT CHILD'));
Hans Muller's avatar
Hans Muller committed
664
    await tester.flingFrom(rightFlingStart, const Offset(200.0, 0.0), 10000.0);
665
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
666 667 668 669
    expect(value, 'LEFT');
  });

  testWidgets('Explicit TabController', (WidgetTester tester) async {
670
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
Hans Muller's avatar
Hans Muller committed
671 672 673 674
    TabController tabController;

    Widget buildTabControllerFrame(BuildContext context, TabController controller) {
      tabController = controller;
675 676 677 678
      return MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
        home: Scaffold(
          appBar: AppBar(
679
            title: const Text('tabs'),
680
            bottom: TabBar(
Hans Muller's avatar
Hans Muller committed
681
              controller: controller,
682
              tabs: tabs.map<Widget>((String tab) => Tab(text: tab)).toList(),
Hans Muller's avatar
Hans Muller committed
683 684
            ),
          ),
685
          body: TabBarView(
Hans Muller's avatar
Hans Muller committed
686
            controller: controller,
687
            children: const <Widget>[
688
              Center(child: Text('LEFT CHILD')),
689 690
              Center(child: Text('RIGHT CHILD')),
            ],
Hans Muller's avatar
Hans Muller committed
691 692 693 694 695
          ),
        ),
      );
    }

696
    await tester.pumpWidget(TabControllerFrame(
Hans Muller's avatar
Hans Muller committed
697 698 699 700 701 702 703 704 705 706 707 708 709
      builder: buildTabControllerFrame,
      length: tabs.length,
      initialIndex: 1,
    ));

    expect(find.text('LEFT'), findsOneWidget);
    expect(find.text('RIGHT'), findsOneWidget);
    expect(find.text('LEFT CHILD'), findsNothing);
    expect(find.text('RIGHT CHILD'), findsOneWidget);
    expect(tabController.index, 1);
    expect(tabController.previousIndex, 1);
    expect(tabController.indexIsChanging, false);
    expect(tabController.animation.value, 1.0);
710
    expect(tabController.animation.status, AnimationStatus.forward);
Hans Muller's avatar
Hans Muller committed
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728

    tabController.index = 0;
    await tester.pump(const Duration(milliseconds: 500));
    await tester.pump(const Duration(milliseconds: 500));
    expect(find.text('LEFT CHILD'), findsOneWidget);
    expect(find.text('RIGHT CHILD'), findsNothing);

    tabController.index = 1;
    await tester.pump(const Duration(milliseconds: 500));
    await tester.pump(const Duration(milliseconds: 500));
    expect(find.text('LEFT CHILD'), findsNothing);
    expect(find.text('RIGHT CHILD'), findsOneWidget);
  });

  testWidgets('TabController listener resets index', (WidgetTester tester) async {
    // This is a regression test for the scenario brought up here
    // https://github.com/flutter/flutter/pull/7387#pullrequestreview-15630946

729
    final List<String> tabs = <String>['A', 'B', 'C'];
Hans Muller's avatar
Hans Muller committed
730 731 732 733
    TabController tabController;

    Widget buildTabControllerFrame(BuildContext context, TabController controller) {
      tabController = controller;
734 735 736 737
      return MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
        home: Scaffold(
          appBar: AppBar(
738
            title: const Text('tabs'),
739
            bottom: TabBar(
Hans Muller's avatar
Hans Muller committed
740
              controller: controller,
741
              tabs: tabs.map<Widget>((String tab) => Tab(text: tab)).toList(),
Hans Muller's avatar
Hans Muller committed
742 743
            ),
          ),
744
          body: TabBarView(
Hans Muller's avatar
Hans Muller committed
745
            controller: controller,
746
            children: const <Widget>[
747 748 749
              Center(child: Text('CHILD A')),
              Center(child: Text('CHILD B')),
              Center(child: Text('CHILD C')),
750
            ],
Hans Muller's avatar
Hans Muller committed
751 752 753 754 755
          ),
        ),
      );
    }

756
    await tester.pumpWidget(TabControllerFrame(
Hans Muller's avatar
Hans Muller committed
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
      builder: buildTabControllerFrame,
      length: tabs.length,
    ));

    tabController.animation.addListener(() {
      if (tabController.animation.status == AnimationStatus.forward)
        tabController.index = 2;
      expect(tabController.indexIsChanging, true);
    });

    expect(tabController.index, 0);
    expect(tabController.indexIsChanging, false);

    tabController.animateTo(1, duration: const Duration(milliseconds: 200), curve: Curves.linear);
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 300));

    expect(tabController.index, 2);
    expect(tabController.indexIsChanging, false);
  });

  testWidgets('TabBarView child disposed during animation', (WidgetTester tester) async {
    // This is a regression test for the scenario brought up here
    // https://github.com/flutter/flutter/pull/7387#discussion_r95089191x

782
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
Hans Muller's avatar
Hans Muller committed
783 784 785
    await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT'));

    // Fling to the left, switch from the 'LEFT' tab to the 'RIGHT'
786
    final Offset flingStart = tester.getCenter(find.text('LEFT CHILD'));
Hans Muller's avatar
Hans Muller committed
787 788 789 790 791
    await tester.flingFrom(flingStart, const Offset(-200.0, 0.0), 10000.0);
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the scroll animation
  });

792
  testWidgets('TabBar unselectedLabelColor control test', (WidgetTester tester) async {
793
    final TabController controller = TabController(
794 795 796 797 798 799 800 801
      vsync: const TestVSync(),
      length: 2,
    );

    Color firstColor;
    Color secondColor;

    await tester.pumpWidget(
802
      boilerplate(
803
        child: TabBar(
804 805 806 807
          controller: controller,
          labelColor: Colors.green[500],
          unselectedLabelColor: Colors.blue[500],
          tabs: <Widget>[
808
            Builder(
809 810
              builder: (BuildContext context) {
                firstColor = IconTheme.of(context).color;
811
                return const Text('First');
812 813
              }
            ),
814
            Builder(
815 816
              builder: (BuildContext context) {
                secondColor = IconTheme.of(context).color;
817
                return const Text('Second');
818 819 820 821 822 823 824 825 826 827 828
              }
            ),
          ],
        ),
      ),
    );

    expect(firstColor, equals(Colors.green[500]));
    expect(secondColor, equals(Colors.blue[500]));
  });

829
  testWidgets('TabBarView page left and right test', (WidgetTester tester) async {
830
    final TabController controller = TabController(
831 832 833 834 835
      vsync: const TestVSync(),
      length: 2,
    );

    await tester.pumpWidget(
836
      boilerplate(
837
        child: TabBarView(
838
          controller: controller,
839
          children: const <Widget>[ Text('First'), Text('Second') ],
840 841 842 843 844 845
        ),
      ),
    );

    expect(controller.index, equals(0));

846
    TestGesture gesture = await tester.startGesture(const Offset(100.0, 100.0));
847 848
    expect(controller.index, equals(0));

849 850 851 852
    // Drag to the left and right, by less than the TabBarView's width.
    // The selected index (controller.index) should not change.
    await gesture.moveBy(const Offset(-100.0, 0.0));
    await gesture.moveBy(const Offset(100.0, 0.0));
853
    expect(controller.index, equals(0));
854 855
    expect(find.text('First'), findsOneWidget);
    expect(find.text('Second'), findsNothing);
856

857 858 859 860 861 862
    // Drag more than the TabBarView's width to the right. This forces
    // the selected index to change to 1.
    await gesture.moveBy(const Offset(-500.0, 0.0));
    await gesture.up();
    await tester.pump(); // start the scroll animation
    await tester.pump(const Duration(seconds: 1)); // finish the scroll animation
863
    expect(controller.index, equals(1));
864 865
    expect(find.text('First'), findsNothing);
    expect(find.text('Second'), findsOneWidget);
866

867
    gesture = await tester.startGesture(const Offset(100.0, 100.0));
868 869
    expect(controller.index, equals(1));

870 871 872 873
    // Drag to the left and right, by less than the TabBarView's width.
    // The selected index (controller.index) should not change.
    await gesture.moveBy(const Offset(-100.0, 0.0));
    await gesture.moveBy(const Offset(100.0, 0.0));
874 875 876 877
    expect(controller.index, equals(1));
    expect(find.text('First'), findsNothing);
    expect(find.text('Second'), findsOneWidget);

878 879 880 881 882 883 884 885 886 887
    // Drag more than the TabBarView's width to the left. This forces
    // the selected index to change back to 0.
    await gesture.moveBy(const Offset(500.0, 0.0));
    await gesture.up();
    await tester.pump(); // start the scroll animation
    await tester.pump(const Duration(seconds: 1)); // finish the scroll animation
    expect(controller.index, equals(0));
    expect(find.text('First'), findsOneWidget);
    expect(find.text('Second'), findsNothing);
  });
888 889 890 891 892 893

  testWidgets('TabBar tap animates the selection indicator', (WidgetTester tester) async {
    // This is a regression test for https://github.com/flutter/flutter/issues/7479

    final List<String> tabs = <String>['A', 'B'];

894
    const Color indicatorColor = Color(0xFFFF0000);
895 896 897
    await tester.pumpWidget(buildFrame(tabs: tabs, value: 'A', indicatorColor: indicatorColor));

    final RenderBox box = tester.renderObject(find.byType(TabBar));
898 899
    final TabIndicatorRecordingCanvas canvas = TabIndicatorRecordingCanvas(indicatorColor);
    final TestRecordingPaintingContext context = TestRecordingPaintingContext(canvas);
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922

    box.paint(context, Offset.zero);
    final Rect indicatorRect0 = canvas.indicatorRect;
    expect(indicatorRect0.left, 0.0);
    expect(indicatorRect0.width, 400.0);
    expect(indicatorRect0.height, 2.0);

    await tester.tap(find.text('B'));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
    box.paint(context, Offset.zero);
    final Rect indicatorRect1 = canvas.indicatorRect;
    expect(indicatorRect1.left, greaterThan(indicatorRect0.left));
    expect(indicatorRect1.right, lessThan(800.0));
    expect(indicatorRect1.height, 2.0);

    await tester.pump(const Duration(milliseconds: 300));
    box.paint(context, Offset.zero);
    final Rect indicatorRect2 = canvas.indicatorRect;
    expect(indicatorRect2.left, 400.0);
    expect(indicatorRect2.width, 400.0);
    expect(indicatorRect2.height, 2.0);
  });
923 924 925 926 927

  testWidgets('TabBarView child disposed during animation', (WidgetTester tester) async {
    // This is a regression test for this patch:
    // https://github.com/flutter/flutter/pull/9015

928
    final TabController controller = TabController(
929 930 931 932 933
      vsync: const TestVSync(),
      length: 2,
    );

    Widget buildFrame() {
934
      return boilerplate(
935 936
        child: TabBar(
          key: UniqueKey(),
937
          controller: controller,
938
          tabs: const <Widget>[ Text('A'), Text('B') ],
939 940 941 942 943 944 945 946 947 948 949 950 951
        ),
      );
    }

    await tester.pumpWidget(buildFrame());

    // The original TabBar will be disposed. The controller should no
    // longer have any listeners from the original TabBar.
    await tester.pumpWidget(buildFrame());

    controller.index = 1;
    await tester.pump(const Duration(milliseconds: 300));
  });
952

953
  testWidgets('TabBarView scrolls end close to a new page', (WidgetTester tester) async {
954 955
    // This is a regression test for https://github.com/flutter/flutter/issues/9375

956
    final TabController tabController = TabController(
957 958 959 960 961
      vsync: const TestVSync(),
      initialIndex: 1,
      length: 3,
    );

962
    await tester.pumpWidget(Directionality(
963
      textDirection: TextDirection.ltr,
964 965 966
      child: SizedBox.expand(
        child: Center(
          child: SizedBox(
967 968
            width: 400.0,
            height: 400.0,
969
            child: TabBarView(
970
              controller: tabController,
971
              children: const <Widget>[
972 973 974
                Center(child: Text('0')),
                Center(child: Text('1')),
                Center(child: Text('2')),
975 976 977 978 979
              ],
            ),
          ),
        ),
      ),
980
    ));
981 982 983 984 985 986

    expect(tabController.index, 1);

    final PageView pageView = tester.widget(find.byType(PageView));
    final PageController pageController = pageView.controller;
    final ScrollPosition position = pageController.position;
987 988 989 990 991 992 993

    // The TabBarView's page width is 400, so page 0 is at scroll offset 0.0,
    // page 1 is at 400.0, page 2 is at 800.0.

    expect(position.pixels, 400.0);

    // Not close enough to switch to page 2
994
    pageController.jumpTo(500.0);
995 996 997
    expect(tabController.index, 1);

    // Close enough to switch to page 2
998
    pageController.jumpTo(700.0);
999
    expect(tabController.index, 2);
1000 1001 1002 1003 1004 1005 1006 1007

    // Same behavior going left: not left enough to get to page 0
    pageController.jumpTo(300.0);
    expect(tabController.index, 1);

    // Left enough to get to page 0
    pageController.jumpTo(100.0);
    expect(tabController.index, 0);
1008 1009
  });

1010
  testWidgets('Can switch to non-neighboring tab in nested TabBarView without crashing', (WidgetTester tester) async {
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
    // This is a regression test for https://github.com/flutter/flutter/issues/18756
    final TabController _mainTabController = TabController(length: 4, vsync: const TestVSync());
    final TabController _nestedTabController = TabController(length: 2, vsync: const TestVSync());

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text('Exception for Nested Tabs'),
            bottom: TabBar(
              controller: _mainTabController,
              tabs: const <Widget>[
                Tab(icon: Icon(Icons.add), text: 'A'),
                Tab(icon: Icon(Icons.add), text: 'B'),
                Tab(icon: Icon(Icons.add), text: 'C'),
                Tab(icon: Icon(Icons.add), text: 'D'),
              ],
            ),
          ),
          body: TabBarView(
            controller: _mainTabController,
            children: <Widget>[
              Container(color: Colors.red),
              _NestedTabBarContainer(tabController: _nestedTabController),
              Container(color: Colors.green),
              Container(color: Colors.indigo),
            ],
          ),
        ),
1040
      ),
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
    );

    // expect first tab to be selected
    expect(_mainTabController.index, 0);

    // tap on third tab
    await tester.tap(find.text('C'));
    await tester.pumpAndSettle();

    // expect third tab to be selected without exceptions
    expect(_mainTabController.index, 2);
  });

1054
  testWidgets('TabBarView scrolls end close to a new page with custom physics', (WidgetTester tester) async {
1055
    final TabController tabController = TabController(
1056 1057 1058 1059 1060
      vsync: const TestVSync(),
      initialIndex: 1,
      length: 3,
    );

1061
    await tester.pumpWidget(Directionality(
1062
      textDirection: TextDirection.ltr,
1063 1064 1065
      child: SizedBox.expand(
        child: Center(
          child: SizedBox(
1066 1067
            width: 400.0,
            height: 400.0,
1068
            child: TabBarView(
1069 1070
              controller: tabController,
              physics: const TestScrollPhysics(),
1071
              children: const <Widget>[
1072 1073 1074
                Center(child: Text('0')),
                Center(child: Text('1')),
                Center(child: Text('2')),
1075 1076 1077 1078 1079
              ],
            ),
          ),
        ),
      ),
1080
    ));
1081 1082 1083 1084 1085 1086

    expect(tabController.index, 1);

    final PageView pageView = tester.widget(find.byType(PageView));
    final PageController pageController = pageView.controller;
    final ScrollPosition position = pageController.position;
1087 1088 1089 1090 1091 1092 1093

    // The TabBarView's page width is 400, so page 0 is at scroll offset 0.0,
    // page 1 is at 400.0, page 2 is at 800.0.

    expect(position.pixels, 400.0);

    // Not close enough to switch to page 2
1094
    pageController.jumpTo(500.0);
1095 1096 1097
    expect(tabController.index, 1);

    // Close enough to switch to page 2
1098
    pageController.jumpTo(700.0);
1099
    expect(tabController.index, 2);
1100 1101 1102 1103 1104 1105 1106 1107

    // Same behavior going left: not left enough to get to page 0
    pageController.jumpTo(300.0);
    expect(tabController.index, 1);

    // Left enough to get to page 0
    pageController.jumpTo(100.0);
    expect(tabController.index, 0);
1108 1109 1110 1111 1112
  });

  testWidgets('Scrollable TabBar with a non-zero TabController initialIndex', (WidgetTester tester) async {
    // This is a regression test for https://github.com/flutter/flutter/issues/9374

1113 1114
    final List<Tab> tabs = List<Tab>.generate(20, (int index) {
      return Tab(text: 'TAB #$index');
1115 1116
    });

1117
    final TabController controller = TabController(
1118 1119 1120 1121 1122 1123
      vsync: const TestVSync(),
      length: tabs.length,
      initialIndex: tabs.length - 1,
    );

    await tester.pumpWidget(
1124
      boilerplate(
1125
        child: TabBar(
1126 1127 1128 1129 1130 1131
          isScrollable: true,
          controller: controller,
          tabs: tabs,
        ),
      ),
    );
1132

1133 1134
    // The initialIndex tab should be visible and right justified
    expect(find.text('TAB #19'), findsOneWidget);
1135 1136 1137 1138 1139 1140

    // Tabs have a minimum width of 72.0 and 'TAB #19' is wider than
    // that. Tabs are padded horizontally with kTabLabelPadding.
    final double tabRight = 800.0 - kTabLabelPadding.right;

    expect(tester.getTopRight(find.widgetWithText(Tab, 'TAB #19')).dx, tabRight);
1141
  });
1142

Ian Hickson's avatar
Ian Hickson committed
1143
  testWidgets('TabBar with indicatorWeight, indicatorPadding (LTR)', (WidgetTester tester) async {
1144
    const Color indicatorColor = Color(0xFF00FF00);
1145
    const double indicatorWeight = 8.0;
1146 1147 1148
    const double padLeft = 8.0;
    const double padRight = 4.0;

1149 1150
    final List<Widget> tabs = List<Widget>.generate(4, (int index) {
      return Tab(text: 'Tab $index');
1151 1152
    });

1153
    final TabController controller = TabController(
1154 1155 1156 1157 1158
      vsync: const TestVSync(),
      length: tabs.length,
    );

    await tester.pumpWidget(
1159
      boilerplate(
1160
        child: Container(
1161
          alignment: Alignment.topLeft,
1162
          child: TabBar(
1163 1164 1165 1166 1167 1168
            indicatorWeight: indicatorWeight,
            indicatorColor: indicatorColor,
            indicatorPadding: const EdgeInsets.only(left: padLeft, right: padRight),
            controller: controller,
            tabs: tabs,
          ),
1169 1170 1171 1172 1173
        ),
      ),
    );

    final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));
1174
    expect(tabBarBox.size.height, 54.0); // 54 = _kTabHeight(46) + indicatorWeight(8.0)
1175

1176
    const double indicatorY = 54.0 - indicatorWeight / 2.0;
1177 1178
    double indicatorLeft = padLeft + indicatorWeight / 2.0;
    double indicatorRight = 200.0 - (padRight + indicatorWeight / 2.0);
1179

1180 1181 1182
    expect(tabBarBox, paints..line(
      color: indicatorColor,
      strokeWidth: indicatorWeight,
1183 1184
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
1185 1186 1187 1188 1189 1190
    ));

    // Select tab 3
    controller.index = 3;
    await tester.pumpAndSettle();

1191 1192
    indicatorLeft = 600.0 + padLeft + indicatorWeight / 2.0;
    indicatorRight = 800.0 - (padRight + indicatorWeight / 2.0);
1193

1194 1195 1196
    expect(tabBarBox, paints..line(
      color: indicatorColor,
      strokeWidth: indicatorWeight,
1197 1198
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
1199 1200
    ));
  });
1201

Ian Hickson's avatar
Ian Hickson committed
1202
  testWidgets('TabBar with indicatorWeight, indicatorPadding (RTL)', (WidgetTester tester) async {
1203
    const Color indicatorColor = Color(0xFF00FF00);
1204
    const double indicatorWeight = 8.0;
Ian Hickson's avatar
Ian Hickson committed
1205 1206 1207
    const double padLeft = 8.0;
    const double padRight = 4.0;

1208 1209
    final List<Widget> tabs = List<Widget>.generate(4, (int index) {
      return Tab(text: 'Tab $index');
Ian Hickson's avatar
Ian Hickson committed
1210 1211
    });

1212
    final TabController controller = TabController(
Ian Hickson's avatar
Ian Hickson committed
1213 1214 1215 1216 1217 1218 1219
      vsync: const TestVSync(),
      length: tabs.length,
    );

    await tester.pumpWidget(
      boilerplate(
        textDirection: TextDirection.rtl,
1220
        child: Container(
1221
          alignment: Alignment.topLeft,
1222
          child: TabBar(
1223 1224 1225 1226 1227 1228
            indicatorWeight: indicatorWeight,
            indicatorColor: indicatorColor,
            indicatorPadding: const EdgeInsets.only(left: padLeft, right: padRight),
            controller: controller,
            tabs: tabs,
          ),
Ian Hickson's avatar
Ian Hickson committed
1229 1230 1231 1232 1233
        ),
      ),
    );

    final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));
1234 1235 1236
    expect(tabBarBox.size.height, 54.0); // 54 = _kTabHeight(46) + indicatorWeight(8.0)
    expect(tabBarBox.size.width, 800.0);

1237
    const double indicatorY = 54.0 - indicatorWeight / 2.0;
1238 1239 1240 1241 1242 1243
    double indicatorLeft = 600.0 + padLeft + indicatorWeight / 2.0;
    double indicatorRight = 800.0 - padRight - indicatorWeight / 2.0;

    expect(tabBarBox, paints..line(
      color: indicatorColor,
      strokeWidth: indicatorWeight,
1244 1245
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
Ian Hickson's avatar
Ian Hickson committed
1246 1247 1248 1249 1250 1251
    ));

    // Select tab 3
    controller.index = 3;
    await tester.pumpAndSettle();

1252 1253 1254 1255 1256 1257
    indicatorLeft = padLeft + indicatorWeight / 2.0;
    indicatorRight = 200.0 - padRight -  indicatorWeight / 2.0;

    expect(tabBarBox, paints..line(
      color: indicatorColor,
      strokeWidth: indicatorWeight,
1258 1259
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
1260 1261 1262 1263
    ));
  });

  testWidgets('TabBar changes indicator attributes', (WidgetTester tester) async {
1264 1265
    final List<Widget> tabs = List<Widget>.generate(4, (int index) {
      return Tab(text: 'Tab $index');
1266 1267
    });

1268
    final TabController controller = TabController(
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
      vsync: const TestVSync(),
      length: tabs.length,
    );

    Color indicatorColor = const Color(0xFF00FF00);
    double indicatorWeight = 8.0;
    double padLeft = 8.0;
    double padRight = 4.0;

    Widget buildFrame() {
      return boilerplate(
1280
        child: Container(
1281
          alignment: Alignment.topLeft,
1282
          child: TabBar(
1283 1284
            indicatorWeight: indicatorWeight,
            indicatorColor: indicatorColor,
1285
            indicatorPadding: EdgeInsets.only(left: padLeft, right: padRight),
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
            controller: controller,
            tabs: tabs,
          ),
        ),
      );
    }

    await tester.pumpWidget(buildFrame());

    final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));
    expect(tabBarBox.size.height, 54.0); // 54 = _kTabHeight(46) + indicatorWeight(8.0)
Ian Hickson's avatar
Ian Hickson committed
1297

1298 1299 1300 1301 1302 1303 1304
    double indicatorY = 54.0 - indicatorWeight / 2.0;
    double indicatorLeft = padLeft + indicatorWeight / 2.0;
    double indicatorRight = 200.0 - (padRight + indicatorWeight / 2.0);

    expect(tabBarBox, paints..line(
      color: indicatorColor,
      strokeWidth: indicatorWeight,
1305 1306
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
    ));

    indicatorColor = const Color(0xFF0000FF);
    indicatorWeight = 4.0;
    padLeft = 4.0;
    padRight = 8.0;

    await tester.pumpWidget(buildFrame());

    expect(tabBarBox.size.height, 50.0); // 54 = _kTabHeight(46) + indicatorWeight(4.0)

    indicatorY = 50.0 - indicatorWeight / 2.0;
    indicatorLeft = padLeft + indicatorWeight / 2.0;
    indicatorRight = 200.0 - (padRight + indicatorWeight / 2.0);

    expect(tabBarBox, paints..line(
      color: indicatorColor,
      strokeWidth: indicatorWeight,
1325 1326
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
Ian Hickson's avatar
Ian Hickson committed
1327 1328 1329 1330 1331
    ));
  });

  testWidgets('TabBar with directional indicatorPadding (LTR)', (WidgetTester tester) async {
    final List<Widget> tabs = <Widget>[
1332 1333 1334
      SizedBox(key: UniqueKey(), width: 130.0, height: 30.0),
      SizedBox(key: UniqueKey(), width: 140.0, height: 40.0),
      SizedBox(key: UniqueKey(), width: 150.0, height: 50.0),
Ian Hickson's avatar
Ian Hickson committed
1335 1336
    ];

1337 1338
    const double indicatorWeight = 2.0; // the default

1339
    final TabController controller = TabController(
Ian Hickson's avatar
Ian Hickson committed
1340 1341 1342 1343 1344 1345
      vsync: const TestVSync(),
      length: tabs.length,
    );

    await tester.pumpWidget(
      boilerplate(
1346
        child: Container(
1347
          alignment: Alignment.topLeft,
1348
          child: TabBar(
1349 1350 1351 1352
            indicatorPadding: const EdgeInsetsDirectional.only(start: 100.0),
            isScrollable: true,
            controller: controller,
            tabs: tabs,
Ian Hickson's avatar
Ian Hickson committed
1353 1354 1355 1356 1357
          ),
        ),
      ),
    );

1358 1359 1360 1361 1362 1363 1364 1365 1366
    final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));
    const double tabBarHeight = 50.0 + indicatorWeight;  // 50 = max tab height
    expect(tabBarBox.size.height, tabBarHeight);

    // Tab0 width = 130, height = 30
    double tabLeft = kTabLabelPadding.left;
    double tabRight = tabLeft + 130.0;
    double tabTop = (tabBarHeight - indicatorWeight - 30.0) / 2.0;
    double tabBottom = tabTop + 30.0;
1367
    Rect tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
1368 1369 1370 1371 1372 1373 1374 1375
    expect(tester.getRect(find.byKey(tabs[0].key)), tabRect);


    // Tab1 width = 140, height = 40
    tabLeft = tabRight + kTabLabelPadding.right + kTabLabelPadding.left;
    tabRight = tabLeft + 140.0;
    tabTop = (tabBarHeight - indicatorWeight - 40.0) / 2.0;
    tabBottom = tabTop + 40.0;
1376
    tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
1377 1378 1379 1380 1381 1382 1383 1384
    expect(tester.getRect(find.byKey(tabs[1].key)), tabRect);


    // Tab2 width = 150, height = 50
    tabLeft = tabRight + kTabLabelPadding.right + kTabLabelPadding.left;
    tabRight = tabLeft + 150.0;
    tabTop = (tabBarHeight - indicatorWeight - 50.0) / 2.0;
    tabBottom = tabTop + 50.0;
1385
    tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
1386 1387 1388
    expect(tester.getRect(find.byKey(tabs[2].key)), tabRect);

    // Tab 0 selected, indicator padding resolves to left: 100.0
1389
    const double indicatorLeft = 100.0 + indicatorWeight / 2.0;
1390 1391 1392 1393
    final double indicatorRight = 130.0 + kTabLabelPadding.horizontal - indicatorWeight / 2.0;
    final double indicatorY = tabBottom + indicatorWeight / 2.0;
    expect(tabBarBox, paints..line(
      strokeWidth: indicatorWeight,
1394 1395
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
Ian Hickson's avatar
Ian Hickson committed
1396 1397 1398 1399 1400
    ));
  });

  testWidgets('TabBar with directional indicatorPadding (RTL)', (WidgetTester tester) async {
    final List<Widget> tabs = <Widget>[
1401 1402 1403
      SizedBox(key: UniqueKey(), width: 130.0, height: 30.0),
      SizedBox(key: UniqueKey(), width: 140.0, height: 40.0),
      SizedBox(key: UniqueKey(), width: 150.0, height: 50.0),
Ian Hickson's avatar
Ian Hickson committed
1404 1405
    ];

1406 1407
    const double indicatorWeight = 2.0; // the default

1408
    final TabController controller = TabController(
Ian Hickson's avatar
Ian Hickson committed
1409 1410 1411 1412 1413 1414 1415
      vsync: const TestVSync(),
      length: tabs.length,
    );

    await tester.pumpWidget(
      boilerplate(
        textDirection: TextDirection.rtl,
1416
        child: Container(
1417
          alignment: Alignment.topLeft,
1418
          child: TabBar(
1419 1420 1421 1422
            indicatorPadding: const EdgeInsetsDirectional.only(start: 100.0),
            isScrollable: true,
            controller: controller,
            tabs: tabs,
Ian Hickson's avatar
Ian Hickson committed
1423 1424 1425 1426 1427
          ),
        ),
      ),
    );

1428 1429 1430 1431 1432 1433 1434 1435 1436
    final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));
    const double tabBarHeight = 50.0 + indicatorWeight;  // 50 = max tab height
    expect(tabBarBox.size.height, tabBarHeight);

    // Tab2 width = 150, height = 50
    double tabLeft = kTabLabelPadding.left;
    double tabRight = tabLeft + 150.0;
    double tabTop = (tabBarHeight - indicatorWeight - 50.0) / 2.0;
    double tabBottom = tabTop + 50.0;
1437
    Rect tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
1438 1439 1440 1441 1442 1443 1444
    expect(tester.getRect(find.byKey(tabs[2].key)), tabRect);

    // Tab1 width = 140, height = 40
    tabLeft = tabRight + kTabLabelPadding.right + kTabLabelPadding.left;
    tabRight = tabLeft + 140.0;
    tabTop = (tabBarHeight - indicatorWeight - 40.0) / 2.0;
    tabBottom = tabTop + 40.0;
1445
    tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
1446 1447 1448 1449 1450 1451 1452
    expect(tester.getRect(find.byKey(tabs[1].key)), tabRect);

    // Tab0 width = 130, height = 30
    tabLeft = tabRight + kTabLabelPadding.right + kTabLabelPadding.left;
    tabRight = tabLeft + 130.0;
    tabTop = (tabBarHeight - indicatorWeight - 30.0) / 2.0;
    tabBottom = tabTop + 30.0;
1453
    tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
1454 1455 1456 1457 1458
    expect(tester.getRect(find.byKey(tabs[0].key)), tabRect);

    // Tab 0 selected, indicator padding resolves to right: 100.0
    final double indicatorLeft = tabLeft - kTabLabelPadding.left + indicatorWeight / 2.0;
    final double indicatorRight = tabRight + kTabLabelPadding.left - indicatorWeight / 2.0 - 100.0;
1459
    const double indicatorY = 50.0 + indicatorWeight / 2.0;
1460 1461
    expect(tabBarBox, paints..line(
      strokeWidth: indicatorWeight,
1462
      p1: Offset(indicatorLeft, indicatorY),
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
      p2: Offset(indicatorRight, indicatorY),
    ));
  });

  testWidgets('TabBar with labelPadding', (WidgetTester tester) async {
    const double indicatorWeight = 2.0; // default indicator weight
    const EdgeInsets labelPadding = EdgeInsets.only(left: 3.0, right: 7.0);
    const EdgeInsets indicatorPadding = labelPadding;

    final List<Widget> tabs = <Widget>[
      SizedBox(key: UniqueKey(), width: 130.0, height: 30.0),
      SizedBox(key: UniqueKey(), width: 140.0, height: 40.0),
      SizedBox(key: UniqueKey(), width: 150.0, height: 50.0),
    ];

    final TabController controller = TabController(
      vsync: const TestVSync(),
      length: tabs.length,
    );

    await tester.pumpWidget(
      boilerplate(
        child: Container(
          alignment: Alignment.topLeft,
          child: TabBar(
            labelPadding: labelPadding,
            indicatorPadding: labelPadding,
            isScrollable: true,
            controller: controller,
            tabs: tabs,
          ),
        ),
      ),
    );

    final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));
    const double tabBarHeight = 50.0 + indicatorWeight;  // 50 = max tab height
    expect(tabBarBox.size.height, tabBarHeight);

    // Tab0 width = 130, height = 30
    double tabLeft = labelPadding.left;
    double tabRight = tabLeft + 130.0;
    double tabTop = (tabBarHeight - indicatorWeight - 30.0) / 2.0;
    double tabBottom = tabTop + 30.0;
    Rect tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
    expect(tester.getRect(find.byKey(tabs[0].key)), tabRect);

    // Tab1 width = 140, height = 40
    tabLeft = tabRight + labelPadding.right + labelPadding.left;
    tabRight = tabLeft + 140.0;
    tabTop = (tabBarHeight - indicatorWeight - 40.0) / 2.0;
    tabBottom = tabTop + 40.0;
    tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
    expect(tester.getRect(find.byKey(tabs[1].key)), tabRect);

    // Tab2 width = 150, height = 50
    tabLeft = tabRight + labelPadding.right + labelPadding.left;
    tabRight = tabLeft + 150.0;
    tabTop = (tabBarHeight - indicatorWeight - 50.0) / 2.0;
    tabBottom = tabTop + 50.0;
    tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
    expect(tester.getRect(find.byKey(tabs[2].key)), tabRect);

    // Tab 0 selected, indicatorPadding == labelPadding
    final double indicatorLeft = indicatorPadding.left + indicatorWeight / 2.0;
    final double indicatorRight = 130.0 + labelPadding.horizontal - indicatorPadding.right - indicatorWeight / 2.0;
    final double indicatorY = tabBottom + indicatorWeight / 2.0;
    expect(tabBarBox, paints..line(
      strokeWidth: indicatorWeight,
      p1: Offset(indicatorLeft, indicatorY),
1533
      p2: Offset(indicatorRight, indicatorY),
Ian Hickson's avatar
Ian Hickson committed
1534 1535 1536 1537
    ));
  });

  testWidgets('Overflowing RTL tab bar', (WidgetTester tester) async {
1538
    final List<Widget> tabs = List<Widget>.filled(100,
1539
      // For convenience padded width of each tab will equal 100:
1540
      // 68 + kTabLabelPadding.horizontal(32)
1541
      SizedBox(key: UniqueKey(), width: 68.0, height: 40.0),
Ian Hickson's avatar
Ian Hickson committed
1542 1543
    );

1544
    final TabController controller = TabController(
Ian Hickson's avatar
Ian Hickson committed
1545 1546 1547 1548
      vsync: const TestVSync(),
      length: tabs.length,
    );

1549 1550
    const double indicatorWeight = 2.0; // the default

Ian Hickson's avatar
Ian Hickson committed
1551 1552 1553
    await tester.pumpWidget(
      boilerplate(
        textDirection: TextDirection.rtl,
1554
        child: Container(
1555
          alignment: Alignment.topLeft,
1556
          child: TabBar(
Ian Hickson's avatar
Ian Hickson committed
1557 1558 1559 1560 1561 1562 1563 1564
            isScrollable: true,
            controller: controller,
            tabs: tabs,
          ),
        ),
      ),
    );

1565 1566 1567 1568 1569 1570 1571
    final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));
    const double tabBarHeight = 40.0 + indicatorWeight;  // 40 = tab height
    expect(tabBarBox.size.height, tabBarHeight);

    // Tab 0 out of 100 selected
    double indicatorLeft = 99.0 * 100.0 + indicatorWeight / 2.0;
    double indicatorRight = 100.0 * 100.0 - indicatorWeight / 2.0;
1572
    const double indicatorY = 40.0 + indicatorWeight / 2.0;
1573 1574
    expect(tabBarBox, paints..line(
      strokeWidth: indicatorWeight,
1575 1576
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
Ian Hickson's avatar
Ian Hickson committed
1577 1578 1579 1580 1581 1582
    ));

    controller.animateTo(tabs.length - 1, duration: const Duration(seconds: 1), curve: Curves.linear);
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 500));

1583 1584 1585
    // The x coordinates of p1 and p2 were derived empirically, not analytically.
    expect(tabBarBox, paints..line(
      strokeWidth: indicatorWeight,
1586 1587
      p1: const Offset(2476.0, indicatorY),
      p2: const Offset(2574.0, indicatorY),
Ian Hickson's avatar
Ian Hickson committed
1588 1589 1590 1591
    ));

    await tester.pump(const Duration(milliseconds: 501));

1592 1593 1594 1595 1596
    // Tab 99 out of 100 selected, appears on the far left because RTL
    indicatorLeft = indicatorWeight / 2.0;
    indicatorRight = 100.0 - indicatorWeight / 2.0;
    expect(tabBarBox, paints..line(
      strokeWidth: indicatorWeight,
1597 1598
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
Ian Hickson's avatar
Ian Hickson committed
1599 1600 1601
    ));
  });

1602
  testWidgets('correct semantics', (WidgetTester tester) async {
1603
    final SemanticsTester semantics = SemanticsTester(tester);
1604

1605 1606
    final List<Tab> tabs = List<Tab>.generate(2, (int index) {
      return Tab(text: 'TAB #$index');
1607 1608
    });

1609
    final TabController controller = TabController(
1610 1611 1612 1613 1614 1615
      vsync: const TestVSync(),
      length: tabs.length,
      initialIndex: 0,
    );

    await tester.pumpWidget(
1616
      boilerplate(
1617
        child: Semantics(
1618
          container: true,
1619
          child: TabBar(
1620 1621 1622 1623 1624 1625 1626 1627
            isScrollable: true,
            controller: controller,
            tabs: tabs,
          ),
        ),
      ),
    );

1628
    final TestSemantics expectedSemantics = TestSemantics.root(
1629
      children: <TestSemantics>[
1630
        TestSemantics.rootChild(
1631
          id: 1,
1632 1633
          rect: TestSemantics.fullScreen,
          children: <TestSemantics>[
1634
            TestSemantics(
1635
              id: 2,
1636 1637
              rect: TestSemantics.fullScreen,
              children: <TestSemantics>[
1638
                TestSemantics(
1639 1640
                    id: 3,
                    rect: TestSemantics.fullScreen,
1641
                    flags: <SemanticsFlag>[SemanticsFlag.hasImplicitScrolling],
1642
                    children: <TestSemantics>[
1643
                      TestSemantics(
1644
                        id: 4,
1645 1646 1647 1648 1649
                        actions: <SemanticsAction>[SemanticsAction.tap],
                        flags: <SemanticsFlag>[
                          SemanticsFlag.isSelected,
                          SemanticsFlag.isFocusable,
                        ],
1650
                        label: 'TAB #0\nTab 1 of 2',
Dan Field's avatar
Dan Field committed
1651
                        rect: const Rect.fromLTRB(0.0, 0.0, 116.0, kTextTabBarHeight),
1652
                        transform: Matrix4.translationValues(0.0, 276.0, 0.0),
1653
                      ),
1654
                      TestSemantics(
1655
                        id: 5,
1656 1657
                        flags: <SemanticsFlag>[SemanticsFlag.isFocusable],
                        actions: <SemanticsAction>[SemanticsAction.tap],
1658
                        label: 'TAB #1\nTab 2 of 2',
Dan Field's avatar
Dan Field committed
1659
                        rect: const Rect.fromLTRB(0.0, 0.0, 116.0, kTextTabBarHeight),
1660
                        transform: Matrix4.translationValues(116.0, 276.0, 0.0),
1661
                      ),
1662 1663
                    ],
                ),
1664 1665
              ],
            ),
1666 1667
          ],
        ),
1668 1669 1670 1671 1672 1673
      ],
    );

    expect(semantics, hasSemantics(expectedSemantics));

    semantics.dispose();
1674
  }, skip: isBrowser);
1675

1676
  testWidgets('correct scrolling semantics', (WidgetTester tester) async {
1677
    final SemanticsTester semantics = SemanticsTester(tester);
1678

1679 1680
    final List<Tab> tabs = List<Tab>.generate(20, (int index) {
      return Tab(text: 'This is a very wide tab #$index');
1681 1682
    });

1683
    final TabController controller = TabController(
1684 1685 1686 1687 1688 1689 1690
      vsync: const TestVSync(),
      length: tabs.length,
      initialIndex: 0,
    );

    await tester.pumpWidget(
      boilerplate(
1691
        child: Semantics(
1692
          container: true,
1693
          child: TabBar(
1694 1695 1696 1697 1698 1699 1700 1701
            isScrollable: true,
            controller: controller,
            tabs: tabs,
          ),
        ),
      ),
    );

1702 1703 1704
    const String tab0title = 'This is a very wide tab #0\nTab 1 of 20';
    const String tab10title = 'This is a very wide tab #10\nTab 11 of 20';

1705
    expect(semantics, includesNodeWith(actions: <SemanticsAction>[SemanticsAction.scrollLeft]));
1706 1707
    expect(semantics, includesNodeWith(label: tab0title));
    expect(semantics, isNot(includesNodeWith(label: tab10title)));
1708 1709 1710 1711

    controller.index = 10;
    await tester.pumpAndSettle();

1712
    expect(semantics, isNot(includesNodeWith(label: tab0title)));
1713
    expect(semantics, includesNodeWith(actions: <SemanticsAction>[SemanticsAction.scrollLeft, SemanticsAction.scrollRight]));
1714
    expect(semantics, includesNodeWith(label: tab10title));
1715 1716 1717 1718 1719 1720 1721 1722 1723 1724

    controller.index = 19;
    await tester.pumpAndSettle();

    expect(semantics, includesNodeWith(actions: <SemanticsAction>[SemanticsAction.scrollRight]));

    controller.index = 0;
    await tester.pumpAndSettle();

    expect(semantics, includesNodeWith(actions: <SemanticsAction>[SemanticsAction.scrollLeft]));
1725 1726
    expect(semantics, includesNodeWith(label: tab0title));
    expect(semantics, isNot(includesNodeWith(label: tab10title)));
1727 1728 1729 1730

    semantics.dispose();
  });

1731
  testWidgets('TabBar etc with zero tabs', (WidgetTester tester) async {
1732
    final TabController controller = TabController(
1733 1734 1735 1736 1737
      vsync: const TestVSync(),
      length: 0,
    );

    await tester.pumpWidget(
1738
      boilerplate(
1739
        child: Column(
1740
          children: <Widget>[
1741
            TabBar(
1742 1743 1744
              controller: controller,
              tabs: const <Widget>[],
            ),
1745 1746
            Flexible(
              child: TabBarView(
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
                controller: controller,
                children: const <Widget>[],
              ),
            ),
          ],
        ),
      ),
    );

    expect(controller.index, 0);
    expect(tester.getSize(find.byType(TabBar)), const Size(800.0, 48.0));
    expect(tester.getSize(find.byType(TabBarView)), const Size(800.0, 600.0 - 48.0));

    // A fling in the TabBar or TabBarView, shouldn't do anything.

1762 1763
    await tester.fling(find.byType(TabBar), const Offset(-100.0, 0.0), 5000.0);
    await tester.pumpAndSettle();
1764

1765 1766
    await tester.fling(find.byType(TabBarView), const Offset(100.0, 0.0), 5000.0);
    await tester.pumpAndSettle();
1767 1768 1769 1770 1771

    expect(controller.index, 0);
  });

  testWidgets('TabBar etc with one tab', (WidgetTester tester) async {
1772
    final TabController controller = TabController(
1773 1774 1775 1776 1777
      vsync: const TestVSync(),
      length: 1,
    );

    await tester.pumpWidget(
1778
      boilerplate(
1779
        child: Column(
1780
          children: <Widget>[
1781
            TabBar(
1782
              controller: controller,
1783
              tabs: const <Widget>[Tab(text: 'TAB')],
1784
            ),
1785 1786
            Flexible(
              child: TabBarView(
1787
                controller: controller,
1788
                children: const <Widget>[Text('PAGE')],
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
              ),
            ),
          ],
        ),
      ),
    );

    expect(controller.index, 0);
    expect(find.text('TAB'), findsOneWidget);
    expect(find.text('PAGE'), findsOneWidget);
    expect(tester.getSize(find.byType(TabBar)), const Size(800.0, 48.0));
    expect(tester.getSize(find.byType(TabBarView)), const Size(800.0, 600.0 - 48.0));

1802 1803 1804 1805
    // The one tab should be center vis the app's width (800).
    final double tabLeft = tester.getTopLeft(find.widgetWithText(Tab, 'TAB')).dx;
    final double tabRight = tester.getTopRight(find.widgetWithText(Tab, 'TAB')).dx;
    expect(tabLeft + (tabRight - tabLeft) / 2.0, 400.0);
1806 1807 1808

    // A fling in the TabBar or TabBarView, shouldn't move the tab.

1809 1810
    await tester.fling(find.byType(TabBar), const Offset(-100.0, 0.0), 5000.0);
    await tester.pump(const Duration(milliseconds: 50));
1811 1812
    expect(tester.getTopLeft(find.widgetWithText(Tab, 'TAB')).dx, tabLeft);
    expect(tester.getTopRight(find.widgetWithText(Tab, 'TAB')).dx, tabRight);
1813
    await tester.pumpAndSettle();
1814

1815 1816
    await tester.fling(find.byType(TabBarView), const Offset(100.0, 0.0), 5000.0);
    await tester.pump(const Duration(milliseconds: 50));
1817 1818
    expect(tester.getTopLeft(find.widgetWithText(Tab, 'TAB')).dx, tabLeft);
    expect(tester.getTopRight(find.widgetWithText(Tab, 'TAB')).dx, tabRight);
1819
    await tester.pumpAndSettle();
1820 1821 1822 1823 1824 1825

    expect(controller.index, 0);
    expect(find.text('TAB'), findsOneWidget);
    expect(find.text('PAGE'), findsOneWidget);
  });

1826
  testWidgets('can tap on indicator at very bottom of TabBar to switch tabs', (WidgetTester tester) async {
1827
    final TabController controller = TabController(
1828 1829 1830 1831 1832 1833
      vsync: const TestVSync(),
      length: 2,
      initialIndex: 0,
    );

    await tester.pumpWidget(
1834
      boilerplate(
1835
        child: Column(
1836
          children: <Widget>[
1837
            TabBar(
1838 1839
              controller: controller,
              indicatorWeight: 30.0,
1840
              tabs: const <Widget>[Tab(text: 'TAB1'), Tab(text: 'TAB2')],
1841
            ),
1842 1843
            Flexible(
              child: TabBarView(
1844
                controller: controller,
1845
                children: const <Widget>[Text('PAGE1'), Text('PAGE2')],
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
              ),
            ),
          ],
        ),
      ),
    );

    expect(controller.index, 0);

    final Offset bottomRight = tester.getBottomRight(find.byType(TabBar)) - const Offset(1.0, 1.0);
    final TestGesture gesture = await tester.startGesture(bottomRight);
    await gesture.up();
    await tester.pumpAndSettle();

    expect(controller.index, 1);
  });
1862 1863

  testWidgets('can override semantics of tabs', (WidgetTester tester) async {
1864
    final SemanticsTester semantics = SemanticsTester(tester);
1865

1866 1867 1868
    final List<Tab> tabs = List<Tab>.generate(2, (int index) {
      return Tab(
        child: Semantics(
1869
          label: 'Semantics override $index',
1870 1871
          child: ExcludeSemantics(
            child: Text('TAB #$index'),
1872 1873 1874 1875 1876
          ),
        ),
      );
    });

1877
    final TabController controller = TabController(
1878 1879 1880 1881 1882 1883 1884
      vsync: const TestVSync(),
      length: tabs.length,
      initialIndex: 0,
    );

    await tester.pumpWidget(
      boilerplate(
1885
        child: Semantics(
1886
          container: true,
1887
          child: TabBar(
1888 1889 1890 1891 1892 1893 1894 1895
            isScrollable: true,
            controller: controller,
            tabs: tabs,
          ),
        ),
      ),
    );

1896
    final TestSemantics expectedSemantics = TestSemantics.root(
1897
      children: <TestSemantics>[
1898
        TestSemantics.rootChild(
1899
          id: 1,
1900 1901
          rect: TestSemantics.fullScreen,
          children: <TestSemantics>[
1902
            TestSemantics(
1903 1904 1905
              id: 2,
              rect: TestSemantics.fullScreen,
              children: <TestSemantics>[
1906
                TestSemantics(
1907 1908
                    id: 3,
                    rect: TestSemantics.fullScreen,
1909
                    flags: <SemanticsFlag>[SemanticsFlag.hasImplicitScrolling],
1910
                    children: <TestSemantics>[
1911
                      TestSemantics(
1912
                        id: 4,
1913 1914 1915 1916 1917
                        flags: <SemanticsFlag>[
                          SemanticsFlag.isSelected,
                          SemanticsFlag.isFocusable,
                        ],
                        actions: <SemanticsAction>[SemanticsAction.tap],
1918
                        label: 'Semantics override 0\nTab 1 of 2',
Dan Field's avatar
Dan Field committed
1919
                        rect: const Rect.fromLTRB(0.0, 0.0, 116.0, kTextTabBarHeight),
1920
                        transform: Matrix4.translationValues(0.0, 276.0, 0.0),
1921
                      ),
1922
                      TestSemantics(
1923
                        id: 5,
1924 1925
                        flags: <SemanticsFlag>[SemanticsFlag.isFocusable],
                        actions: <SemanticsAction>[SemanticsAction.tap],
1926
                        label: 'Semantics override 1\nTab 2 of 2',
Dan Field's avatar
Dan Field committed
1927
                        rect: const Rect.fromLTRB(0.0, 0.0, 116.0, kTextTabBarHeight),
1928
                        transform: Matrix4.translationValues(116.0, 276.0, 0.0),
1929
                      ),
1930 1931
                    ],
                ),
1932 1933
              ],
            ),
1934 1935 1936 1937 1938 1939 1940 1941
          ],
        ),
      ],
    );

    expect(semantics, hasSemantics(expectedSemantics));

    semantics.dispose();
1942
  }, skip: isBrowser);
1943

1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024
  testWidgets('can be notified of TabBar onTap behavior', (WidgetTester tester) async {
    int tabIndex = -1;

    Widget buildFrame({
      TabController controller,
      List<String> tabs,
    }) {
      return boilerplate(
        child: Container(
          child: TabBar(
            controller: controller,
            tabs: tabs.map<Widget>((String tab) => Tab(text: tab)).toList(),
            onTap: (int index) {
              tabIndex = index;
            },
          ),
        ),
      );
    }

    final List<String> tabs = <String>['A', 'B', 'C'];
    final TabController controller = TabController(
      vsync: const TestVSync(),
      length: tabs.length,
      initialIndex: tabs.indexOf('C'),
    );

    await tester.pumpWidget(buildFrame(tabs: tabs, controller: controller));
    expect(find.text('A'), findsOneWidget);
    expect(find.text('B'), findsOneWidget);
    expect(find.text('C'), findsOneWidget);
    expect(controller, isNotNull);
    expect(controller.index, 2);
    expect(tabIndex, -1); // no tap so far so tabIndex should reflect that

    // Verify whether the [onTap] notification works when the [TabBar] animates.

    await tester.pumpWidget(buildFrame(tabs: tabs, controller: controller));
    await tester.tap(find.text('B'));
    await tester.pump();
    expect(controller.indexIsChanging, true);
    await tester.pumpAndSettle();
    expect(controller.index, 1);
    expect(controller.previousIndex, 2);
    expect(controller.indexIsChanging, false);
    expect(tabIndex, controller.index);

    tabIndex = -1;

    await tester.pumpWidget(buildFrame(tabs: tabs, controller: controller));
    await tester.tap(find.text('C'));
    await tester.pump();
    await tester.pumpAndSettle();
    expect(controller.index, 2);
    expect(controller.previousIndex, 1);
    expect(tabIndex, controller.index);

    tabIndex = -1;

    await tester.pumpWidget(buildFrame(tabs: tabs, controller: controller));
    await tester.tap(find.text('A'));
    await tester.pump();
    await tester.pumpAndSettle();
    expect(controller.index, 0);
    expect(controller.previousIndex, 2);
    expect(tabIndex, controller.index);

    tabIndex = -1;

    // Verify whether [onTap] is called even when the [TabController] does
    // not change.

    final int currentControllerIndex = controller.index;
    await tester.pumpWidget(buildFrame(tabs: tabs, controller: controller));
    await tester.tap(find.text('A'));
    await tester.pump();
    await tester.pumpAndSettle();
    expect(controller.index, currentControllerIndex); // controller has not changed
    expect(tabIndex, 0);
  });

2025
  test('illegal constructor combinations', () {
2026 2027 2028
    expect(() => Tab(icon: nonconst(null)), throwsAssertionError);
    expect(() => Tab(icon: Container(), text: 'foo', child: Container()), throwsAssertionError);
    expect(() => Tab(text: 'foo', child: Container()), throwsAssertionError);
2029
  });
2030 2031 2032 2033 2034 2035 2036


  testWidgets('TabController changes', (WidgetTester tester) async {
    // This is a regression test for https://github.com/flutter/flutter/issues/14812

    Widget buildFrame(TabController controller) {
      return boilerplate(
2037
        child: Container(
2038
          alignment: Alignment.topLeft,
2039
          child: TabBar(
2040
            controller: controller,
2041
            tabs: const <Tab>[
2042 2043
              Tab(text: 'LEFT'),
              Tab(text: 'RIGHT'),
2044 2045 2046 2047 2048 2049
            ],
          ),
        ),
      );
    }

2050
    final TabController controller1 = TabController(
2051 2052 2053 2054 2055
      vsync: const TestVSync(),
      length: 2,
      initialIndex: 0,
    );

2056
    final TabController controller2 = TabController(
2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070
      vsync: const TestVSync(),
      length: 2,
      initialIndex: 0,
    );

    await tester.pumpWidget(buildFrame(controller1));
    await tester.pumpWidget(buildFrame(controller2));
    expect(controller1.index, 0);
    expect(controller2.index, 0);

    const double indicatorWeight = 2.0;
    final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));
    expect(tabBarBox.size.height, 48.0); // 48 = _kTabHeight(46) + indicatorWeight(2.0)

2071
    const double indicatorY = 48.0 - indicatorWeight / 2.0;
2072 2073 2074 2075
    double indicatorLeft = indicatorWeight / 2.0;
    double indicatorRight = 400.0 - indicatorWeight / 2.0; // 400 = screen_width / 2
    expect(tabBarBox, paints..line(
      strokeWidth: indicatorWeight,
2076 2077
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
    ));

    await tester.tap(find.text('RIGHT'));
    await tester.pumpAndSettle();
    expect(controller1.index, 0);
    expect(controller2.index, 1);

    // Verify that the TabBar's _IndicatorPainter is now listening to
    // tabController2.

    indicatorLeft = 400.0 + indicatorWeight / 2.0;
    indicatorRight = 800.0 - indicatorWeight / 2.0;
    expect(tabBarBox, paints..line(
      strokeWidth: indicatorWeight,
2092 2093
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
2094 2095 2096
    ));
  });

2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107
  testWidgets('Default tab indicator color is white', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/15958
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
    await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT'));
    final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));
    expect(tabBarBox, paints..line(
      color: Colors.white,
    ));

  });

2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157
  testWidgets('Skipping tabs with global key does not crash', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/24660
    final List<String> tabs = <String>[
      'Tab1',
      'Tab2',
      'Tab3',
      'Tab4',
    ];
    final TabController controller = TabController(
      vsync: const TestVSync(),
      length: tabs.length,
    );
    await tester.pumpWidget(
      MaterialApp(
        home: Align(
          alignment: Alignment.topLeft,
          child: SizedBox(
            width: 300.0,
            height: 200.0,
            child: Scaffold(
              appBar: AppBar(
                title: const Text('tabs'),
                bottom: TabBar(
                  controller: controller,
                  tabs: tabs.map<Widget>((String tab) => Tab(text: tab)).toList(),
                ),
              ),
              body: TabBarView(
                controller: controller,
                children: <Widget>[
                  Text('1', key: GlobalKey()),
                  Text('2', key: GlobalKey()),
                  Text('3', key: GlobalKey()),
                  Text('4', key: GlobalKey()),
                ],
              ),
            ),
          ),
        ),
      ),
    );
    expect(find.text('1'), findsOneWidget);
    expect(find.text('4'), findsNothing);
    await tester.tap(find.text('Tab4'));
    await tester.pumpAndSettle();
    expect(controller.index, 3);
    expect(find.text('4'), findsOneWidget);
    expect(find.text('1'), findsNothing);
  });

2158 2159 2160 2161 2162 2163 2164 2165 2166
  testWidgets('Skipping tabs with a KeepAlive child works', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/11895
    final List<String> tabs = <String>[
      'Tab1',
      'Tab2',
      'Tab3',
      'Tab4',
      'Tab5',
    ];
2167
    final TabController controller = TabController(
2168 2169 2170 2171
      vsync: const TestVSync(),
      length: tabs.length,
    );
    await tester.pumpWidget(
2172 2173
      MaterialApp(
        home: Align(
2174
          alignment: Alignment.topLeft,
2175
          child: SizedBox(
2176 2177
            width: 300.0,
            height: 200.0,
2178 2179
            child: Scaffold(
              appBar: AppBar(
2180
                title: const Text('tabs'),
2181
                bottom: TabBar(
2182
                  controller: controller,
2183
                  tabs: tabs.map<Widget>((String tab) => Tab(text: tab)).toList(),
2184 2185
                ),
              ),
2186
              body: TabBarView(
2187 2188
                controller: controller,
                children: <Widget>[
2189
                  AlwaysKeepAliveWidget(key: UniqueKey()),
2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209
                  const Text('2'),
                  const Text('3'),
                  const Text('4'),
                  const Text('5'),
                ],
              ),
            ),
          ),
        ),
      ),
    );
    expect(find.text(AlwaysKeepAliveWidget.text), findsOneWidget);
    expect(find.text('4'), findsNothing);
    await tester.tap(find.text('Tab4'));
    await tester.pumpAndSettle();
    await tester.pump();
    expect(controller.index, 3);
    expect(find.text(AlwaysKeepAliveWidget.text, skipOffstage: false), findsOneWidget);
    expect(find.text('4'), findsOneWidget);
  });
2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270

  testWidgets('tabbar does not scroll when viewport dimensions initially change from zero to non-zero', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/10531.

    const List<Widget> tabs = <Widget>[
      Tab(text: 'NEW MEXICO'),
      Tab(text: 'GABBA'),
      Tab(text: 'HEY'),
    ];
    final TabController controller = TabController(vsync: const TestVSync(), length: tabs.length);

    Widget buildTestWidget({double width, double height}) {
      return MaterialApp(
        home: Center(
          child: SizedBox(
            height: height,
            width: width,
            child: Scaffold(
              appBar: AppBar(
                title: const Text('AppBarBug'),
                bottom: PreferredSize(
                  preferredSize: const Size.fromHeight(30.0),
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 15.0),
                    child: Align(
                      alignment: FractionalOffset.center,
                      child: TabBar(
                        controller: controller,
                        isScrollable: true,
                        tabs: tabs,
                      ),
                    ),
                  ),
                ),
              ),
              body: const Center(
                child: Text('Hello World'),
              ),
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(
      buildTestWidget(
        width: 0.0,
        height: 0.0,
      ),
    );

    await tester.pumpWidget(
      buildTestWidget(
        width: 300.0,
        height: 400.0,
      ),
    );

    expect(tester.hasRunningAnimations, isFalse);
    expect(await tester.pumpAndSettle(), 1); // no more frames are scheduled.
  });
2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320

  // Regression test for https://github.com/flutter/flutter/issues/20292.
  testWidgets('Number of tabs can be updated dynamically', (WidgetTester tester) async {
    final List<String> threeTabs = <String>['A', 'B', 'C'];
    final List<String> twoTabs = <String>['A', 'B'];
    final List<String> oneTab = <String>['A'];
    final Key key = UniqueKey();
    Widget buildTabs(List<String> tabs) {
      return boilerplate(
        child: DefaultTabController(
          key: key,
          length: tabs.length,
          child: TabBar(
            tabs: tabs.map<Widget>((String tab) => Tab(text: tab)).toList(),
          ),
        ),
      );
    }
    TabController getController() => DefaultTabController.of(tester.element(find.text('A')));

    await tester.pumpWidget(buildTabs(threeTabs));
    await tester.tap(find.text('B'));
    await tester.pump();
    TabController controller = getController();
    expect(controller.previousIndex, 0);
    expect(controller.index, 1);
    expect(controller.length, 3);

    await tester.pumpWidget(buildTabs(twoTabs));
    controller = getController();
    expect(controller.previousIndex, 0);
    expect(controller.index, 1);
    expect(controller.length, 2);

    await tester.pumpWidget(buildTabs(oneTab));
    controller = getController();
    expect(controller.previousIndex, 1);
    expect(controller.index, 0);
    expect(controller.length, 1);

    await tester.pumpWidget(buildTabs(twoTabs));
    controller = getController();
    expect(controller.previousIndex, 1);
    expect(controller.index, 0);
    expect(controller.length, 2);
  });

  // Regression test for https://github.com/flutter/flutter/issues/15008.
  testWidgets('TabBar with one tab has correct color', (WidgetTester tester) async {
    const Tab tab = Tab(text: 'A');
2321 2322
    const Color selectedTabColor = Color(0x00000001);
    const Color unselectedTabColor = Color(0x00000002);
2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337

    await tester.pumpWidget(boilerplate(
      child: const DefaultTabController(
        length: 1,
        child: TabBar(
          tabs: <Tab>[tab],
          labelColor: selectedTabColor,
          unselectedLabelColor: unselectedTabColor,
        ),
      ),
    ));

    final IconThemeData iconTheme = IconTheme.of(tester.element(find.text('A')));
    expect(iconTheme.color, equals(selectedTabColor));
  });
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388

  testWidgets('Replacing the tabController after disposing the old one', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/32428

    TabController controller = TabController(vsync: const TestVSync(), length: 2);
    await tester.pumpWidget(
      MaterialApp(
        home: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
            return Scaffold(
              appBar: AppBar(
                bottom: TabBar(
                  controller: controller,
                  tabs: List<Widget>.generate(controller.length, (int index) => Tab(text: 'Tab$index')),
                ),
                actions: <Widget>[
                  FlatButton(
                    child: const Text('Change TabController length'),
                    onPressed: () {
                      setState(() {
                        controller.dispose();
                        controller = TabController(vsync: const TestVSync(), length: 3);
                      });
                    },
                  ),
                ],
              ),
              body: TabBarView(
                controller: controller,
                children: List<Widget>.generate(controller.length, (int index) => Center(child: Text('Tab $index'))),
              ),
            );
          },
        ),
      ),
    );

    expect(controller.index, 0);
    expect(controller.length, 2);
    expect(find.text('Tab0'), findsOneWidget);
    expect(find.text('Tab1'), findsOneWidget);
    expect(find.text('Tab2'), findsNothing);

    await tester.tap(find.text('Change TabController length'));
    await tester.pumpAndSettle();
    expect(controller.index, 0);
    expect(controller.length, 3);
    expect(find.text('Tab0'), findsOneWidget);
    expect(find.text('Tab1'), findsOneWidget);
    expect(find.text('Tab2'), findsOneWidget);
  });
2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411

  testWidgets('DefaultTabController should allow for a length of zero', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/20292.
    List<String> tabTextContent = <String>[];

    await tester.pumpWidget(
      MaterialApp(
        home: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
            return DefaultTabController(
              length: tabTextContent.length,
              child: Scaffold(
                appBar: AppBar(
                  title: const Text('Default TabBar Preview'),
                  bottom: tabTextContent.isNotEmpty
                    ? TabBar(
                       isScrollable: true,
                       tabs: tabTextContent.map((String textContent) => Tab(text: textContent)).toList(),
                     )
                    : null,
                ),
                body: tabTextContent.isNotEmpty
                  ? TabBarView(
2412
                      children: tabTextContent.map((String textContent) => Tab(text: "$textContent's view")).toList()
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454
                    )
                  : const Center(child: Text('No tabs')),
                bottomNavigationBar: BottomAppBar(
                  child: Row(
                    mainAxisSize: MainAxisSize.max,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      IconButton(
                        key: const Key('Add tab'),
                        icon: const Icon(Icons.add),
                        onPressed: () {
                          setState(() {
                            tabTextContent = List<String>.from(tabTextContent)
                              ..add('Tab ${tabTextContent.length + 1}');
                          });
                        },
                      ),
                      IconButton(
                        key: const Key('Delete tab'),
                        icon: const Icon(Icons.delete),
                        onPressed: () {
                          setState(() {
                            tabTextContent = List<String>.from(tabTextContent)
                              ..removeLast();
                          });
                        },
                      ),
                    ],
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );

    // Initializes with zero tabs properly
    expect(find.text('No tabs'), findsOneWidget);
    await tester.tap(find.byKey(const Key('Add tab')));
    await tester.pumpAndSettle();
    expect(find.text('Tab 1'), findsOneWidget);
2455
    expect(find.text("Tab 1's view"), findsOneWidget);
2456 2457 2458 2459 2460 2461

    // Dynamically updates to zero tabs properly
    await tester.tap(find.byKey(const Key('Delete tab')));
    await tester.pumpAndSettle();
    expect(find.text('No tabs'), findsOneWidget);
  });
2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477

   testWidgets('TabBar expands vertically to accommodate the Icon and child Text() pair the same amount it would expand for Icon and text pair.', (WidgetTester tester) async {
    const double indicatorWeight = 2.0;

    const List<Widget> tabListWithText = <Widget>[
      Tab(icon: Icon(Icons.notifications), text: 'Test'),
    ];
    const List<Widget> tabListWithTextChild = <Widget>[
      Tab(icon: Icon(Icons.notifications), child: Text('Test')),
    ];

    const TabBar tabBarWithText = TabBar(tabs: tabListWithText, indicatorWeight: indicatorWeight,);
    const TabBar tabBarWithTextChild = TabBar(tabs: tabListWithTextChild, indicatorWeight: indicatorWeight,);

    expect(tabBarWithText.preferredSize, tabBarWithTextChild.preferredSize);
   });
2478
}