tabs_test.dart 130 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.

5
import 'package:flutter/gestures.dart';
6
import 'package:flutter/material.dart';
7
import 'package:flutter/physics.dart';
8 9
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
10

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

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

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

35
  final Widget? child;
Adam Barth's avatar
Adam Barth committed
36 37

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

class StateMarkerState extends State<StateMarker> {
42
  String? marker;
Adam Barth's avatar
Adam Barth committed
43 44 45

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

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

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

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

71 72 73 74 75
class _NestedTabBarContainer extends StatelessWidget {
  const _NestedTabBarContainer({
    this.tabController,
  });

76
  final TabController? tabController;
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

  @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),
              ],
            ),
100
          ),
101 102 103 104 105 106
        ],
      ),
    );
  }
}

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

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

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

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

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

class TabControllerFrameState extends State<TabControllerFrame> with SingleTickerProviderStateMixin {
147
  late TabController _controller;
Hans Muller's avatar
Hans Muller committed
148 149 150 151

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

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

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

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

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

  final Color indicatorColor;
200
  late Rect indicatorRect;
201 202

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

211
class TestScrollPhysics extends ScrollPhysics {
212
  const TestScrollPhysics({ ScrollPhysics? parent }) : super(parent: parent);
213

214
  @override
215
  TestScrollPhysics applyTo(ScrollPhysics? ancestor) {
216
    return TestScrollPhysics(parent: buildParent(ancestor));
217
  }
218

219 220 221 222 223
  @override
  double applyPhysicsToUserOffset(ScrollMetrics position, double offset) {
    return offset == 10 ? 20 : offset;
  }

224
  static final SpringDescription _kDefaultSpring = SpringDescription.withDampingRatio(
225
    mass: 0.5,
226
    stiffness: 500.0,
227 228
    ratio: 1.1,
  );
229

230 231 232 233
  @override
  SpringDescription get spring => _kDefaultSpring;
}

234
void main() {
235 236 237 238
  setUp(() {
    debugResetSemanticsIdCounter();
  });

Ian Hickson's avatar
Ian Hickson committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
  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')))),
    );
257
    expect(tester.renderObject<RenderParagraph>(find.byType(RichText)).text.style!.fontFamily, 'Ahem');
Ian Hickson's avatar
Ian Hickson committed
258
    expect(tester.getSize(find.byType(Tab)), const Size(14.0, 46.0));
259
  });
Ian Hickson's avatar
Ian Hickson committed
260 261 262 263 264

  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')))),
    );
265
    expect(tester.renderObject<RenderParagraph>(find.byType(RichText)).text.style!.fontFamily, 'Ahem');
Ian Hickson's avatar
Ian Hickson committed
266
    expect(tester.getSize(find.byType(Tab)), const Size(14.0, 72.0));
267
  });
Ian Hickson's avatar
Ian Hickson committed
268

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
  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',
            ),
          ),
        ),
      ),
    );
289
    expect(tester.renderObject<RenderParagraph>(find.byType(RichText)).text.style!.fontFamily, 'Ahem');
290
    expect(tester.getSize(find.byType(Tab)), const Size(210.0, 72.0));
291
  });
292

Ian Hickson's avatar
Ian Hickson committed
293 294 295 296
  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'))))),
    );
297
    expect(tester.renderObject<RenderParagraph>(find.byType(RichText)).text.style!.fontFamily, 'Ahem');
Ian Hickson's avatar
Ian Hickson committed
298
    expect(tester.getSize(find.byType(Tab)), const Size(14.0, 72.0));
299
  });
Ian Hickson's avatar
Ian Hickson committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

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

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

328
    await tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: false));
329 330 331
    expect(find.text('A'), findsOneWidget);
    expect(find.text('B'), findsOneWidget);
    expect(find.text('C'), findsOneWidget);
332
    final TabController controller = DefaultTabController.of(tester.element(find.text('A')))!;
Hans Muller's avatar
Hans Muller committed
333 334 335
    expect(controller, isNotNull);
    expect(controller.index, 2);
    expect(controller.previousIndex, 2);
336

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

346 347 348 349
    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
350 351
    expect(controller.index, 2);
    expect(controller.previousIndex, 1);
352

353 354 355 356
    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
357 358
    expect(controller.index, 0);
    expect(controller.previousIndex, 2);
359 360
  });

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

364
    await tester.pumpWidget(buildFrame(tabs: tabs, value: 'C', isScrollable: true));
365 366 367
    expect(find.text('A'), findsOneWidget);
    expect(find.text('B'), findsOneWidget);
    expect(find.text('C'), findsOneWidget);
368
    final TabController controller = DefaultTabController.of(tester.element(find.text('A')))!;
Hans Muller's avatar
Hans Muller committed
369 370
    expect(controller.index, 2);
    expect(controller.previousIndex, 2);
371

Hans Muller's avatar
Hans Muller committed
372
    await tester.tap(find.text('C'));
373
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
374
    expect(controller.index, 2);
375

Hans Muller's avatar
Hans Muller committed
376
    await tester.tap(find.text('B'));
377
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
378
    expect(controller.index, 1);
379

380
    await tester.tap(find.text('A'));
381
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
382
    expect(controller.index, 0);
383
  });
Hans Muller's avatar
Hans Muller committed
384

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

    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
395
    expect(tester.getCenter(find.text('FFFFFF')).dx, greaterThan(401.0));
396

397
    await tester.tap(find.text('FFFFFF'));
398
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
399
    expect(controller.index, 5);
400
    // The center of the FFFFFF item is now at the TabBar's center
401
    expect(tester.getCenter(find.text('FFFFFF')).dx, moreOrLessEquals(400.0, epsilon: 1.0));
Hans Muller's avatar
Hans Muller committed
402 403 404
  });


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

    // Fling-scroll the TabBar to the left
414
    expect(tester.getCenter(find.text('HHHH')).dx, lessThan(700.0));
415
    await tester.fling(find.byKey(tabBarKey), const Offset(-200.0, 0.0), 10000.0);
416 417
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the scroll animation
418
    expect(tester.getCenter(find.text('HHHH')).dx, lessThan(500.0));
419 420

    // Scrolling the TabBar doesn't change the selection
Hans Muller's avatar
Hans Muller committed
421
    expect(controller.index, 0);
422
  });
Adam Barth's avatar
Adam Barth committed
423

Hans Muller's avatar
Hans Muller committed
424
  testWidgets('TabBarView maintains state', (WidgetTester tester) async {
425
    final List<String> tabs = <String>['AAAAAA', 'BBBBBB', 'CCCCCC', 'DDDDDD', 'EEEEEE'];
426 427 428
    String value = tabs[0];

    Widget builder() {
429
      return boilerplate(
430
        child: DefaultTabController(
Hans Muller's avatar
Hans Muller committed
431 432
          initialIndex: tabs.indexOf(value),
          length: tabs.length,
433
          child: TabBarView(
434
            children: tabs.map<Widget>((String name) {
435
              return StateMarker(
436
                child: Text(name),
437
              );
438
            }).toList(),
Hans Muller's avatar
Hans Muller committed
439 440
          ),
        ),
441 442 443 444
      );
    }

    StateMarkerState findStateMarkerState(String name) {
445
      return tester.state(find.widgetWithText(StateMarker, name, skipOffstage: false));
446 447
    }

448
    await tester.pumpWidget(builder());
449
    final TabController controller = DefaultTabController.of(tester.element(find.text('AAAAAA')))!;
Hans Muller's avatar
Hans Muller committed
450

451
    TestGesture gesture = await tester.startGesture(tester.getCenter(find.text(tabs[0])));
452
    await gesture.moveBy(const Offset(-600.0, 0.0));
453
    await tester.pump();
454 455
    expect(value, equals(tabs[0]));
    findStateMarkerState(tabs[1]).marker = 'marked';
456 457 458
    await gesture.up();
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
Hans Muller's avatar
Hans Muller committed
459
    value = tabs[controller.index];
460
    expect(value, equals(tabs[1]));
461
    await tester.pumpWidget(builder());
462 463 464 465
    expect(findStateMarkerState(tabs[1]).marker, equals('marked'));

    // Move to the third tab.

466
    gesture = await tester.startGesture(tester.getCenter(find.text(tabs[1])));
467
    await gesture.moveBy(const Offset(-600.0, 0.0));
468 469
    await gesture.up();
    await tester.pump();
470
    expect(findStateMarkerState(tabs[1]).marker, equals('marked'));
471
    await tester.pump(const Duration(seconds: 1));
Hans Muller's avatar
Hans Muller committed
472
    value = tabs[controller.index];
473
    expect(value, equals(tabs[2]));
474
    await tester.pumpWidget(builder());
475 476 477 478 479 480 481

    // The state is now gone.

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

    // Move back to the second tab.

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

  testWidgets('TabBar left/right fling', (WidgetTester tester) async {
498
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
499 500 501 502 503 504 505

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

506
    final TabController controller = DefaultTabController.of(tester.element(find.text('LEFT')))!;
Hans Muller's avatar
Hans Muller committed
507
    expect(controller.index, 0);
508 509

    // Fling to the left, switch from the 'LEFT' tab to the 'RIGHT'
510
    Offset flingStart = tester.getCenter(find.text('LEFT CHILD'));
511
    await tester.flingFrom(flingStart, const Offset(-200.0, 0.0), 10000.0);
512
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
513
    expect(controller.index, 1);
514 515 516 517 518
    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'));
519
    await tester.flingFrom(flingStart, const Offset(200.0, 0.0), 10000.0);
520
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
521
    expect(controller.index, 0);
522 523 524 525
    expect(find.text('LEFT CHILD'), findsOneWidget);
    expect(find.text('RIGHT CHILD'), findsNothing);
  });

526
  testWidgets('TabBar left/right fling reverse (1)', (WidgetTester tester) async {
527
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
528 529 530 531 532 533 534

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

535
    final TabController controller = DefaultTabController.of(tester.element(find.text('LEFT')))!;
536 537
    expect(controller.index, 0);

538
    final Offset flingStart = tester.getCenter(find.text('LEFT CHILD'));
539 540 541 542 543 544 545 546 547
    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 {
548
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
549 550 551 552 553 554 555

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

556
    final TabController controller = DefaultTabController.of(tester.element(find.text('LEFT')))!;
557 558
    expect(controller.index, 0);

559
    final Offset flingStart = tester.getCenter(find.text('LEFT CHILD'));
560 561 562 563 564 565 566 567 568
    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);
  });

569
  // A regression test for https://github.com/flutter/flutter/issues/5095
570
  testWidgets('TabBar left/right fling reverse (2)', (WidgetTester tester) async {
571
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
572 573 574 575 576 577 578

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

579
    final TabController controller = DefaultTabController.of(tester.element(find.text('LEFT')))!;
Hans Muller's avatar
Hans Muller committed
580
    expect(controller.index, 0);
581

582
    final Offset flingStart = tester.getCenter(find.text('LEFT CHILD'));
583
    final TestGesture gesture = await tester.startGesture(flingStart);
584 585 586 587
    for (int index = 0; index > 50; index += 1) {
      await gesture.moveBy(const Offset(-10.0, 0.0));
      await tester.pump(const Duration(milliseconds: 1));
    }
588 589 590
    // 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.
591 592 593 594 595
    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();
596 597
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the scroll animation
Hans Muller's avatar
Hans Muller committed
598
    expect(controller.index, 0);
599 600 601 602
    expect(find.text('LEFT CHILD'), findsOneWidget);
    expect(find.text('RIGHT CHILD'), findsNothing);
  });

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

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

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

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

    await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT'));
647
    final TabController controller = DefaultTabController.of(tester.element(find.text('LEFT')))!;
Hans Muller's avatar
Hans Muller committed
648 649 650 651

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

652
    late String value;
Hans Muller's avatar
Hans Muller committed
653 654 655 656 657
    controller.addListener(() {
      value = tabs[controller.index];
    });

    await tester.tap(find.text('RIGHT'));
658
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
659 660 661
    expect(value, 'RIGHT');

    await tester.tap(find.text('LEFT'));
662
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
663 664
    expect(value, 'LEFT');

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

670
    final Offset rightFlingStart = tester.getCenter(find.text('RIGHT CHILD'));
Hans Muller's avatar
Hans Muller committed
671
    await tester.flingFrom(rightFlingStart, const Offset(200.0, 0.0), 10000.0);
672
    await tester.pumpAndSettle();
Hans Muller's avatar
Hans Muller committed
673 674 675 676
    expect(value, 'LEFT');
  });

  testWidgets('Explicit TabController', (WidgetTester tester) async {
677
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
678
    late TabController tabController;
Hans Muller's avatar
Hans Muller committed
679 680 681

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

703
    await tester.pumpWidget(TabControllerFrame(
Hans Muller's avatar
Hans Muller committed
704 705 706 707 708 709 710 711 712 713 714 715
      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);
716 717
    expect(tabController.animation!.value, 1.0);
    expect(tabController.animation!.status, AnimationStatus.forward);
Hans Muller's avatar
Hans Muller committed
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735

    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

736
    final List<String> tabs = <String>['A', 'B', 'C'];
737
    late TabController tabController;
Hans Muller's avatar
Hans Muller committed
738 739 740

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

763
    await tester.pumpWidget(TabControllerFrame(
Hans Muller's avatar
Hans Muller committed
764 765 766 767
      builder: buildTabControllerFrame,
      length: tabs.length,
    ));

768 769
    tabController.animation!.addListener(() {
      if (tabController.animation!.status == AnimationStatus.forward)
Hans Muller's avatar
Hans Muller committed
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
        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

789
    final List<String> tabs = <String>['LEFT', 'RIGHT'];
Hans Muller's avatar
Hans Muller committed
790 791 792
    await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT'));

    // Fling to the left, switch from the 'LEFT' tab to the 'RIGHT'
793
    final Offset flingStart = tester.getCenter(find.text('LEFT CHILD'));
Hans Muller's avatar
Hans Muller committed
794 795 796 797 798
    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
  });

799
  testWidgets('TabBar unselectedLabelColor control test', (WidgetTester tester) async {
800
    final TabController controller = TabController(
801 802 803 804
      vsync: const TestVSync(),
      length: 2,
    );

805 806
    late Color firstColor;
    late Color secondColor;
807 808

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

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

836
  testWidgets('TabBarView page left and right test', (WidgetTester tester) async {
837
    final TabController controller = TabController(
838 839 840 841 842
      vsync: const TestVSync(),
      length: 2,
    );

    await tester.pumpWidget(
843
      boilerplate(
844
        child: TabBarView(
845
          controller: controller,
846
          children: const <Widget>[ Text('First'), Text('Second') ],
847 848 849 850 851 852
        ),
      ),
    );

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

853
    TestGesture gesture = await tester.startGesture(const Offset(100.0, 100.0));
854 855
    expect(controller.index, equals(0));

856 857 858 859
    // 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));
860
    expect(controller.index, equals(0));
861 862
    expect(find.text('First'), findsOneWidget);
    expect(find.text('Second'), findsNothing);
863

864 865 866 867 868 869
    // 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
870
    expect(controller.index, equals(1));
871 872
    expect(find.text('First'), findsNothing);
    expect(find.text('Second'), findsOneWidget);
873

874
    gesture = await tester.startGesture(const Offset(100.0, 100.0));
875 876
    expect(controller.index, equals(1));

877 878 879 880
    // 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));
881 882 883 884
    expect(controller.index, equals(1));
    expect(find.text('First'), findsNothing);
    expect(find.text('Second'), findsOneWidget);

885 886 887 888 889 890 891 892 893 894
    // 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);
  });
895 896 897 898 899 900

  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'];

901
    const Color indicatorColor = Color(0xFFFF0000);
902 903 904
    await tester.pumpWidget(buildFrame(tabs: tabs, value: 'A', indicatorColor: indicatorColor));

    final RenderBox box = tester.renderObject(find.byType(TabBar));
905 906
    final TabIndicatorRecordingCanvas canvas = TabIndicatorRecordingCanvas(indicatorColor);
    final TestRecordingPaintingContext context = TestRecordingPaintingContext(canvas);
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929

    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);
  });
930 931 932 933 934

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

935
    final TabController controller = TabController(
936 937 938 939 940
      vsync: const TestVSync(),
      length: 2,
    );

    Widget buildFrame() {
941
      return boilerplate(
942 943
        child: TabBar(
          key: UniqueKey(),
944
          controller: controller,
945
          tabs: const <Widget>[ Text('A'), Text('B') ],
946 947 948 949 950 951 952 953 954 955 956 957 958
        ),
      );
    }

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

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

963
    final TabController tabController = TabController(
964 965 966 967 968
      vsync: const TestVSync(),
      initialIndex: 1,
      length: 3,
    );

969
    await tester.pumpWidget(Directionality(
970
      textDirection: TextDirection.ltr,
971 972 973
      child: SizedBox.expand(
        child: Center(
          child: SizedBox(
974 975
            width: 400.0,
            height: 400.0,
976
            child: TabBarView(
977
              controller: tabController,
978
              children: const <Widget>[
979 980 981
                Center(child: Text('0')),
                Center(child: Text('1')),
                Center(child: Text('2')),
982 983 984 985 986
              ],
            ),
          ),
        ),
      ),
987
    ));
988 989 990 991 992 993

    expect(tabController.index, 1);

    final PageView pageView = tester.widget(find.byType(PageView));
    final PageController pageController = pageView.controller;
    final ScrollPosition position = pageController.position;
994 995 996 997 998 999 1000

    // 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
1001
    pageController.jumpTo(500.0);
1002 1003 1004
    expect(tabController.index, 1);

    // Close enough to switch to page 2
1005
    pageController.jumpTo(700.0);
1006
    expect(tabController.index, 2);
1007 1008 1009 1010 1011 1012 1013 1014

    // 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);
1015 1016
  });

1017
  testWidgets('Can switch to non-neighboring tab in nested TabBarView without crashing', (WidgetTester tester) async {
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
    // 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),
            ],
          ),
        ),
1047
      ),
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
    );

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

1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
  testWidgets('TabBarView can warp when child is kept alive and contains ink', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/57662.
    final TabController controller = TabController(
      vsync: const TestVSync(),
      length: 3,
    );

    await tester.pumpWidget(
      boilerplate(
        child: TabBarView(
          controller: controller,
          children: const <Widget>[
            Text('Page 1'),
            Text('Page 2'),
            KeepAliveInk('Page 3'),
          ],
        ),
      ),
    );

    expect(controller.index, equals(0));
    expect(find.text('Page 1'), findsOneWidget);
    expect(find.text('Page 3'), findsNothing);

    controller.index = 2;
    await tester.pumpAndSettle();
    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 3'), findsOneWidget);

    controller.index = 0;
    await tester.pumpAndSettle();
    expect(find.text('Page 1'), findsOneWidget);
    expect(find.text('Page 3'), findsNothing);

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

1098
  testWidgets('TabBarView scrolls end close to a new page with custom physics', (WidgetTester tester) async {
1099
    final TabController tabController = TabController(
1100 1101 1102 1103 1104
      vsync: const TestVSync(),
      initialIndex: 1,
      length: 3,
    );

1105
    await tester.pumpWidget(Directionality(
1106
      textDirection: TextDirection.ltr,
1107 1108 1109
      child: SizedBox.expand(
        child: Center(
          child: SizedBox(
1110 1111
            width: 400.0,
            height: 400.0,
1112
            child: TabBarView(
1113 1114
              controller: tabController,
              physics: const TestScrollPhysics(),
1115
              children: const <Widget>[
1116 1117 1118
                Center(child: Text('0')),
                Center(child: Text('1')),
                Center(child: Text('2')),
1119 1120 1121 1122 1123
              ],
            ),
          ),
        ),
      ),
1124
    ));
1125 1126 1127 1128 1129 1130

    expect(tabController.index, 1);

    final PageView pageView = tester.widget(find.byType(PageView));
    final PageController pageController = pageView.controller;
    final ScrollPosition position = pageController.position;
1131 1132 1133 1134 1135 1136 1137

    // 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
1138
    pageController.jumpTo(500.0);
1139 1140 1141
    expect(tabController.index, 1);

    // Close enough to switch to page 2
1142
    pageController.jumpTo(700.0);
1143
    expect(tabController.index, 2);
1144 1145 1146 1147 1148 1149 1150 1151

    // 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);
1152 1153
  });

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
  testWidgets('TabBar accepts custom physics', (WidgetTester tester) async {
    final List<Tab> tabs = List<Tab>.generate(20, (int index) {
      return Tab(text: 'TAB #$index');
    });

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

    await tester.pumpWidget(
      boilerplate(
        child: TabBar(
          isScrollable: true,
          controller: controller,
          tabs: tabs,
          physics: const TestScrollPhysics(),
        ),
      ),
    );

    final TabBar tabBar = tester.widget(find.byType(TabBar));
1177
    final double position = tabBar.physics!.applyPhysicsToUserOffset(MockScrollMetrics(), 10);
1178 1179 1180 1181

    expect(position, equals(20));
  });

1182 1183 1184
  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

1185 1186
    final List<Tab> tabs = List<Tab>.generate(20, (int index) {
      return Tab(text: 'TAB #$index');
1187 1188
    });

1189
    final TabController controller = TabController(
1190 1191 1192 1193 1194 1195
      vsync: const TestVSync(),
      length: tabs.length,
      initialIndex: tabs.length - 1,
    );

    await tester.pumpWidget(
1196
      boilerplate(
1197
        child: TabBar(
1198 1199 1200 1201 1202 1203
          isScrollable: true,
          controller: controller,
          tabs: tabs,
        ),
      ),
    );
1204

1205 1206
    // The initialIndex tab should be visible and right justified
    expect(find.text('TAB #19'), findsOneWidget);
1207 1208 1209 1210 1211 1212

    // 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);
1213
  });
1214

Ian Hickson's avatar
Ian Hickson committed
1215
  testWidgets('TabBar with indicatorWeight, indicatorPadding (LTR)', (WidgetTester tester) async {
1216
    const Color indicatorColor = Color(0xFF00FF00);
1217
    const double indicatorWeight = 8.0;
1218 1219 1220
    const double padLeft = 8.0;
    const double padRight = 4.0;

1221 1222
    final List<Widget> tabs = List<Widget>.generate(4, (int index) {
      return Tab(text: 'Tab $index');
1223 1224
    });

1225
    final TabController controller = TabController(
1226 1227 1228 1229 1230
      vsync: const TestVSync(),
      length: tabs.length,
    );

    await tester.pumpWidget(
1231
      boilerplate(
1232
        child: Container(
1233
          alignment: Alignment.topLeft,
1234
          child: TabBar(
1235 1236 1237 1238 1239 1240
            indicatorWeight: indicatorWeight,
            indicatorColor: indicatorColor,
            indicatorPadding: const EdgeInsets.only(left: padLeft, right: padRight),
            controller: controller,
            tabs: tabs,
          ),
1241 1242 1243 1244 1245
        ),
      ),
    );

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

1248
    const double indicatorY = 54.0 - indicatorWeight / 2.0;
1249 1250
    double indicatorLeft = padLeft + indicatorWeight / 2.0;
    double indicatorRight = 200.0 - (padRight + indicatorWeight / 2.0);
1251

1252 1253 1254
    expect(tabBarBox, paints..line(
      color: indicatorColor,
      strokeWidth: indicatorWeight,
1255 1256
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
1257 1258 1259 1260 1261 1262
    ));

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

1263 1264
    indicatorLeft = 600.0 + padLeft + indicatorWeight / 2.0;
    indicatorRight = 800.0 - (padRight + indicatorWeight / 2.0);
1265

1266 1267 1268
    expect(tabBarBox, paints..line(
      color: indicatorColor,
      strokeWidth: indicatorWeight,
1269 1270
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
1271 1272
    ));
  });
1273

Ian Hickson's avatar
Ian Hickson committed
1274
  testWidgets('TabBar with indicatorWeight, indicatorPadding (RTL)', (WidgetTester tester) async {
1275
    const Color indicatorColor = Color(0xFF00FF00);
1276
    const double indicatorWeight = 8.0;
Ian Hickson's avatar
Ian Hickson committed
1277 1278 1279
    const double padLeft = 8.0;
    const double padRight = 4.0;

1280 1281
    final List<Widget> tabs = List<Widget>.generate(4, (int index) {
      return Tab(text: 'Tab $index');
Ian Hickson's avatar
Ian Hickson committed
1282 1283
    });

1284
    final TabController controller = TabController(
Ian Hickson's avatar
Ian Hickson committed
1285 1286 1287 1288 1289 1290 1291
      vsync: const TestVSync(),
      length: tabs.length,
    );

    await tester.pumpWidget(
      boilerplate(
        textDirection: TextDirection.rtl,
1292
        child: Container(
1293
          alignment: Alignment.topLeft,
1294
          child: TabBar(
1295 1296 1297 1298 1299 1300
            indicatorWeight: indicatorWeight,
            indicatorColor: indicatorColor,
            indicatorPadding: const EdgeInsets.only(left: padLeft, right: padRight),
            controller: controller,
            tabs: tabs,
          ),
Ian Hickson's avatar
Ian Hickson committed
1301 1302 1303 1304 1305
        ),
      ),
    );

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

1309
    const double indicatorY = 54.0 - indicatorWeight / 2.0;
1310 1311 1312 1313 1314 1315
    double indicatorLeft = 600.0 + padLeft + indicatorWeight / 2.0;
    double indicatorRight = 800.0 - padRight - indicatorWeight / 2.0;

    expect(tabBarBox, paints..line(
      color: indicatorColor,
      strokeWidth: indicatorWeight,
1316 1317
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
Ian Hickson's avatar
Ian Hickson committed
1318 1319 1320 1321 1322 1323
    ));

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

1324 1325 1326 1327 1328 1329
    indicatorLeft = padLeft + indicatorWeight / 2.0;
    indicatorRight = 200.0 - padRight -  indicatorWeight / 2.0;

    expect(tabBarBox, paints..line(
      color: indicatorColor,
      strokeWidth: indicatorWeight,
1330 1331
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
1332 1333 1334 1335
    ));
  });

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

1340
    final TabController controller = TabController(
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
      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(
1352
        child: Container(
1353
          alignment: Alignment.topLeft,
1354
          child: TabBar(
1355 1356
            indicatorWeight: indicatorWeight,
            indicatorColor: indicatorColor,
1357
            indicatorPadding: EdgeInsets.only(left: padLeft, right: padRight),
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
            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
1369

1370 1371 1372 1373 1374 1375 1376
    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,
1377 1378
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
    ));

    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,
1397 1398
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
Ian Hickson's avatar
Ian Hickson committed
1399 1400 1401 1402 1403
    ));
  });

  testWidgets('TabBar with directional indicatorPadding (LTR)', (WidgetTester tester) async {
    final List<Widget> tabs = <Widget>[
1404 1405 1406
      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
1407 1408
    ];

1409 1410
    const double indicatorWeight = 2.0; // the default

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

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

1430 1431 1432 1433 1434 1435 1436 1437 1438
    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;
1439
    Rect tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
1440
    expect(tester.getRect(find.byKey(tabs[0].key!)), tabRect);
1441 1442 1443 1444 1445 1446 1447


    // 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;
1448
    tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
1449
    expect(tester.getRect(find.byKey(tabs[1].key!)), tabRect);
1450 1451 1452 1453 1454 1455 1456


    // 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;
1457
    tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
1458
    expect(tester.getRect(find.byKey(tabs[2].key!)), tabRect);
1459 1460

    // Tab 0 selected, indicator padding resolves to left: 100.0
1461
    const double indicatorLeft = 100.0 + indicatorWeight / 2.0;
1462 1463 1464 1465
    final double indicatorRight = 130.0 + kTabLabelPadding.horizontal - indicatorWeight / 2.0;
    final double indicatorY = tabBottom + indicatorWeight / 2.0;
    expect(tabBarBox, paints..line(
      strokeWidth: indicatorWeight,
1466 1467
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
Ian Hickson's avatar
Ian Hickson committed
1468 1469 1470 1471 1472
    ));
  });

  testWidgets('TabBar with directional indicatorPadding (RTL)', (WidgetTester tester) async {
    final List<Widget> tabs = <Widget>[
1473 1474 1475
      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
1476 1477
    ];

1478 1479
    const double indicatorWeight = 2.0; // the default

1480
    final TabController controller = TabController(
Ian Hickson's avatar
Ian Hickson committed
1481 1482 1483 1484 1485 1486 1487
      vsync: const TestVSync(),
      length: tabs.length,
    );

    await tester.pumpWidget(
      boilerplate(
        textDirection: TextDirection.rtl,
1488
        child: Container(
1489
          alignment: Alignment.topLeft,
1490
          child: TabBar(
1491 1492 1493 1494
            indicatorPadding: const EdgeInsetsDirectional.only(start: 100.0),
            isScrollable: true,
            controller: controller,
            tabs: tabs,
Ian Hickson's avatar
Ian Hickson committed
1495 1496 1497 1498 1499
          ),
        ),
      ),
    );

1500 1501 1502 1503 1504 1505 1506 1507 1508
    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;
1509
    Rect tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
1510
    expect(tester.getRect(find.byKey(tabs[2].key!)), tabRect);
1511 1512 1513 1514 1515 1516

    // 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;
1517
    tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
1518
    expect(tester.getRect(find.byKey(tabs[1].key!)), tabRect);
1519 1520 1521 1522 1523 1524

    // 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;
1525
    tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
1526
    expect(tester.getRect(find.byKey(tabs[0].key!)), tabRect);
1527 1528 1529 1530

    // 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;
1531
    const double indicatorY = 50.0 + indicatorWeight / 2.0;
1532 1533
    expect(tabBarBox, paints..line(
      strokeWidth: indicatorWeight,
1534
      p1: Offset(indicatorLeft, indicatorY),
1535 1536 1537 1538
      p2: Offset(indicatorRight, indicatorY),
    ));
  });

1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
  testWidgets('TabBar with custom indicator and indicatorPadding(LTR)', (WidgetTester tester) async {
    const Color indicatorColor = Color(0xFF00FF00);
    const double padTop = 10.0;
    const double padBottom = 12.0;
    const double padLeft = 8.0;
    const double padRight = 4.0;
    const Decoration indicator = BoxDecoration(color: indicatorColor);

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

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

    await tester.pumpWidget(
      boilerplate(
        child: Container(
          alignment: Alignment.topLeft,
          child: TabBar(
            indicator: indicator,
            indicatorPadding:
            const EdgeInsets.fromLTRB(padLeft, padTop, padRight, padBottom),
            controller: controller,
            tabs: tabs,
          ),
        ),
      ),
    );

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

    const double indicatorBottom = 48.0 - padBottom;
    const double indicatorTop = padTop;
    double indicatorLeft = padLeft;
    double indicatorRight = 200.0 - padRight;

    expect(tabBarBox, paints..rect(
      rect: Rect.fromLTRB(
        indicatorLeft,
        indicatorTop,
        indicatorRight,
        indicatorBottom,
      ),
      color: indicatorColor,
    ));

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

    indicatorLeft = 600.0 + padLeft;
    indicatorRight = 800.0 - padRight;

    expect(tabBarBox, paints..rect(
      rect: Rect.fromLTRB(
        indicatorLeft,
        indicatorTop,
        indicatorRight,
        indicatorBottom,
      ),
      color: indicatorColor,
    ));
  });

  testWidgets('TabBar with custom indicator and indicatorPadding (RTL)', (WidgetTester tester) async {
    const Color indicatorColor = Color(0xFF00FF00);
    const double padTop = 10.0;
    const double padBottom = 12.0;
    const double padLeft = 8.0;
    const double padRight = 4.0;
    const Decoration indicator = BoxDecoration(color: indicatorColor);

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

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

    await tester.pumpWidget(
      boilerplate(
        textDirection: TextDirection.rtl,
        child: Container(
          alignment: Alignment.topLeft,
          child: TabBar(
          indicator: indicator,
          indicatorPadding: const EdgeInsets.fromLTRB(padLeft, padTop, padRight, padBottom),
          controller: controller,
          tabs: tabs,
          ),
        ),
      ),
    );

    final RenderBox tabBarBox =
    tester.firstRenderObject<RenderBox>(find.byType(TabBar));
    expect(tabBarBox.size.height, 48.0);
    // 48 = _kTabHeight(46) + indicatorWeight(2.0) ~default
    expect(tabBarBox.size.width, 800.0);
    const double indicatorBottom = 48.0 - padBottom;
    const double indicatorTop = padTop;
    double indicatorLeft = 600.0 + padLeft;
    double indicatorRight = 800.0 - padRight;

    expect(tabBarBox, paints..rect(
      rect: Rect.fromLTRB(
        indicatorLeft,
        indicatorTop,
        indicatorRight,
        indicatorBottom,
      ),
      color: indicatorColor,
    ));

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

    indicatorLeft = padLeft;
    indicatorRight = 200.0 - padRight;

    expect(tabBarBox,paints..rect(
      rect: Rect.fromLTRB(
        indicatorLeft,
        indicatorTop,
        indicatorRight,
        indicatorBottom,
      ),
      color: indicatorColor,
    ));
  });

  testWidgets('TabBar with custom indicator - directional indicatorPadding (LTR)', (WidgetTester tester) async {
    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),
    ];
    const Color indicatorColor = Color(0xFF00FF00);
    const double padTop = 10.0;
    const double padBottom = 12.0;
    const double padStart = 8.0;
    const double padEnd = 4.0;
    const Decoration indicator = BoxDecoration(color: indicatorColor);
    const double indicatorWeight = 2.0; // the default

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

    await tester.pumpWidget(
      boilerplate(
        child: Container(
          alignment: Alignment.topLeft,
          child: TabBar(
            indicator: indicator,
1704
            indicatorPadding: const EdgeInsetsDirectional.fromSTEB(padStart, padTop, padEnd, padBottom),
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
            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 = kTabLabelPadding.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 + kTabLabelPadding.right + kTabLabelPadding.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 + kTabLabelPadding.right + kTabLabelPadding.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, indicator padding resolves to left: 8.0, right: 4.0
    const double indicatorLeft = padStart;
    final double indicatorRight = 130.0 + kTabLabelPadding.horizontal - padEnd;
    const double indicatorTop = padTop;
    const double indicatorBottom = tabBarHeight - padBottom;
    expect(tabBarBox, paints..rect(
      rect: Rect.fromLTRB(
        indicatorLeft,
        indicatorTop,
        indicatorRight,
        indicatorBottom,
      ),
      color: indicatorColor,
    ));
  });

  testWidgets('TabBar with custom indicator - directional indicatorPadding (RTL)', (WidgetTester tester) async {
    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),
    ];
    const Color indicatorColor = Color(0xFF00FF00);
    const double padTop = 10.0;
    const double padBottom = 12.0;
    const double padStart = 8.0;
    const double padEnd = 4.0;
    const Decoration indicator = BoxDecoration(color: indicatorColor);
    const double indicatorWeight = 2.0; // the default

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

    await tester.pumpWidget(
      boilerplate(
        textDirection: TextDirection.rtl,
        child: Container(
          alignment: Alignment.topLeft,
          child: TabBar(
            indicator: indicator,
1783
            indicatorPadding: const EdgeInsetsDirectional.fromSTEB(padStart, padTop, padEnd, padBottom),
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
            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);

    // 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;
    Rect tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
    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;
    tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
    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;
    tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
    expect(tester.getRect(find.byKey(tabs[0].key!)), tabRect);

    // Tab 0 selected, indicator padding resolves to left: 4.0, right: 8.0
    final double indicatorLeft = tabLeft - kTabLabelPadding.left + padEnd;
    final double indicatorRight = tabRight + kTabLabelPadding.left - padStart;
    const double indicatorTop = padTop;
    const double indicatorBottom = tabBarHeight - padBottom;

    expect(tabBarBox, paints..rect(
      rect: Rect.fromLTRB(
        indicatorLeft,
        indicatorTop,
        indicatorRight,
        indicatorBottom,
      ),
      color: indicatorColor,
    ));
  });

1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956
   testWidgets('TabBar with padding isScrollable: false', (WidgetTester tester) async {
    const double indicatorWeight = 2.0; // default indicator weight
    const EdgeInsets padding = EdgeInsets.only(left: 3.0, top: 7.0, right: 5.0, bottom: 3.0);

    final List<Widget> tabs = <Widget>[
      SizedBox(key: UniqueKey(), width: double.infinity, height: 30.0),
      SizedBox(key: UniqueKey(), width: double.infinity, height: 40.0),
    ];

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

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

    final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));
    final double tabBarHeight = 40.0 + indicatorWeight + padding.top + padding.bottom;  // 40 = max tab height
    expect(tabBarBox.size.height, tabBarHeight);

    final double tabSize = (tabBarBox.size.width - padding.horizontal) / 2.0;

    // Tab0 height = 30
    double tabLeft = padding.left;
    double tabRight = tabLeft + tabSize;
    double tabTop = (tabBarHeight - indicatorWeight + (padding.top - padding.bottom) - 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 height = 40
    tabLeft = tabRight;
    tabRight = tabLeft + tabSize;
    tabTop = (tabBarHeight - indicatorWeight + (padding.top - padding.bottom) - 40.0) / 2.0;
    tabBottom = tabTop + 40.0;
    tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
    expect(tester.getRect(find.byKey(tabs[1].key!)), tabRect);

    tabRight += padding.right;
    expect(tabBarBox.size.width, tabRight);
  });

  testWidgets('TabBar with padding isScrollable: true', (WidgetTester tester) async {
    const double indicatorWeight = 2.0; // default indicator weight
    const EdgeInsets padding = EdgeInsets.only(left: 3.0, top: 7.0, right: 5.0, bottom: 3.0);

    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(
            padding: padding,
            labelPadding: EdgeInsets.zero,
            indicatorPadding: EdgeInsets.zero,
            isScrollable: true,
            controller: controller,
            tabs: tabs,
          ),
        ),
      ),
    );

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

    // Tab0 width = 130, height = 30
    double tabLeft = padding.left;
    double tabRight = tabLeft + 130.0;
    double tabTop = (tabBarHeight - indicatorWeight + (padding.top - padding.bottom) - 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;
    tabRight = tabLeft + 140.0;
    tabTop = (tabBarHeight - indicatorWeight + (padding.top - padding.bottom) - 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;
    tabRight = tabLeft + 150.0;
    tabTop = (tabBarHeight - indicatorWeight + (padding.top - padding.bottom) - 50.0) / 2.0;
    tabBottom = tabTop + 50.0;
    tabRect = Rect.fromLTRB(tabLeft, tabTop, tabRight, tabBottom);
    expect(tester.getRect(find.byKey(tabs[2].key!)), tabRect);

    tabRight += padding.right;
    expect(tabBarBox.size.width, tabRight);
  });

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
  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);
1998
    expect(tester.getRect(find.byKey(tabs[0].key!)), tabRect);
1999 2000 2001 2002 2003 2004 2005

    // 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);
2006
    expect(tester.getRect(find.byKey(tabs[1].key!)), tabRect);
2007 2008 2009 2010 2011 2012 2013

    // 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);
2014
    expect(tester.getRect(find.byKey(tabs[2].key!)), tabRect);
2015 2016 2017 2018 2019 2020 2021 2022

    // 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),
2023
      p2: Offset(indicatorRight, indicatorY),
Ian Hickson's avatar
Ian Hickson committed
2024 2025 2026 2027
    ));
  });

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

2034
    final TabController controller = TabController(
Ian Hickson's avatar
Ian Hickson committed
2035 2036 2037 2038
      vsync: const TestVSync(),
      length: tabs.length,
    );

2039 2040
    const double indicatorWeight = 2.0; // the default

Ian Hickson's avatar
Ian Hickson committed
2041 2042 2043
    await tester.pumpWidget(
      boilerplate(
        textDirection: TextDirection.rtl,
2044
        child: Container(
2045
          alignment: Alignment.topLeft,
2046
          child: TabBar(
Ian Hickson's avatar
Ian Hickson committed
2047 2048 2049 2050 2051 2052 2053 2054
            isScrollable: true,
            controller: controller,
            tabs: tabs,
          ),
        ),
      ),
    );

2055 2056 2057 2058 2059 2060 2061
    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;
2062
    const double indicatorY = 40.0 + indicatorWeight / 2.0;
2063 2064
    expect(tabBarBox, paints..line(
      strokeWidth: indicatorWeight,
2065 2066
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
Ian Hickson's avatar
Ian Hickson committed
2067 2068 2069 2070 2071 2072
    ));

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

2073 2074
    expect(tabBarBox, paints..line(
      strokeWidth: indicatorWeight,
2075 2076
      p1: const Offset(4951.0, indicatorY),
      p2: const Offset(5049.0, indicatorY),
Ian Hickson's avatar
Ian Hickson committed
2077 2078 2079 2080
    ));

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

2081 2082 2083 2084 2085
    // 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,
2086 2087
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
Ian Hickson's avatar
Ian Hickson committed
2088 2089 2090
    ));
  });

2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 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 2158 2159 2160 2161 2162 2163 2164 2165 2166
  testWidgets('Tab indicator animation test', (WidgetTester tester) async {
    const double indicatorWeight = 8.0;

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

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

    await tester.pumpWidget(
      boilerplate(
        child: Container(
          alignment: Alignment.topLeft,
          child: TabBar(
            indicatorWeight: indicatorWeight,
            controller: controller,
            tabs: tabs,
          ),
        ),
      ),
    );

    final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));

    // Initial indicator position.
    const double indicatorY = 54.0 - indicatorWeight / 2.0;
    double indicatorLeft = indicatorWeight / 2.0;
    double indicatorRight = 200.0 - (indicatorWeight / 2.0);

    expect(tabBarBox, paints..line(
      strokeWidth: indicatorWeight,
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
    ));

    // Select tab 1.
    controller.animateTo(1, duration: const Duration(milliseconds: 1000), curve: Curves.linear);
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 500));
    indicatorLeft = 100.0 + indicatorWeight / 2.0;
    indicatorRight = 300.0 - (indicatorWeight / 2.0);

    expect(tabBarBox, paints..line(
      strokeWidth: indicatorWeight,
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
    ));

    // Select tab 2 when animation is running.
    controller.animateTo(2, duration: const Duration(milliseconds: 1000), curve: Curves.linear);
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 500));
    indicatorLeft = 250.0 + indicatorWeight / 2.0;
    indicatorRight = 450.0 - (indicatorWeight / 2.0);

    expect(tabBarBox, paints..line(
      strokeWidth: indicatorWeight,
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
    ));

    // Final indicator position.
    await tester.pumpAndSettle();
    indicatorLeft = 400.0 + indicatorWeight / 2.0;
    indicatorRight = 600.0 - (indicatorWeight / 2.0);

    expect(tabBarBox, paints..line(
      strokeWidth: indicatorWeight,
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
    ));
  });

2167
  testWidgets('correct semantics', (WidgetTester tester) async {
2168
    final SemanticsTester semantics = SemanticsTester(tester);
2169

2170 2171
    final List<Tab> tabs = List<Tab>.generate(2, (int index) {
      return Tab(text: 'TAB #$index');
2172 2173
    });

2174
    final TabController controller = TabController(
2175 2176 2177 2178 2179 2180
      vsync: const TestVSync(),
      length: tabs.length,
      initialIndex: 0,
    );

    await tester.pumpWidget(
2181
      boilerplate(
2182
        child: Semantics(
2183
          container: true,
2184
          child: TabBar(
2185 2186 2187 2188 2189 2190 2191 2192
            isScrollable: true,
            controller: controller,
            tabs: tabs,
          ),
        ),
      ),
    );

2193
    final TestSemantics expectedSemantics = TestSemantics.root(
2194
      children: <TestSemantics>[
2195
        TestSemantics.rootChild(
2196
          id: 1,
2197 2198
          rect: TestSemantics.fullScreen,
          children: <TestSemantics>[
2199
            TestSemantics(
2200
              id: 2,
2201 2202
              rect: TestSemantics.fullScreen,
              children: <TestSemantics>[
2203
                TestSemantics(
2204 2205
                    id: 3,
                    rect: TestSemantics.fullScreen,
2206
                    flags: <SemanticsFlag>[SemanticsFlag.hasImplicitScrolling],
2207
                    children: <TestSemantics>[
2208
                      TestSemantics(
2209
                        id: 4,
2210 2211 2212 2213 2214
                        actions: <SemanticsAction>[SemanticsAction.tap],
                        flags: <SemanticsFlag>[
                          SemanticsFlag.isSelected,
                          SemanticsFlag.isFocusable,
                        ],
2215
                        label: 'TAB #0\nTab 1 of 2',
Dan Field's avatar
Dan Field committed
2216
                        rect: const Rect.fromLTRB(0.0, 0.0, 116.0, kTextTabBarHeight),
2217
                        transform: Matrix4.translationValues(0.0, 276.0, 0.0),
2218
                      ),
2219
                      TestSemantics(
2220
                        id: 5,
2221 2222
                        flags: <SemanticsFlag>[SemanticsFlag.isFocusable],
                        actions: <SemanticsAction>[SemanticsAction.tap],
2223
                        label: 'TAB #1\nTab 2 of 2',
Dan Field's avatar
Dan Field committed
2224
                        rect: const Rect.fromLTRB(0.0, 0.0, 116.0, kTextTabBarHeight),
2225
                        transform: Matrix4.translationValues(116.0, 276.0, 0.0),
2226
                      ),
2227 2228
                    ],
                ),
2229 2230
              ],
            ),
2231 2232
          ],
        ),
2233 2234 2235 2236 2237 2238
      ],
    );

    expect(semantics, hasSemantics(expectedSemantics));

    semantics.dispose();
2239
  });
2240

2241
  testWidgets('correct scrolling semantics', (WidgetTester tester) async {
2242
    final SemanticsTester semantics = SemanticsTester(tester);
2243

2244 2245
    final List<Tab> tabs = List<Tab>.generate(20, (int index) {
      return Tab(text: 'This is a very wide tab #$index');
2246 2247
    });

2248
    final TabController controller = TabController(
2249 2250 2251 2252 2253 2254 2255
      vsync: const TestVSync(),
      length: tabs.length,
      initialIndex: 0,
    );

    await tester.pumpWidget(
      boilerplate(
2256
        child: Semantics(
2257
          container: true,
2258
          child: TabBar(
2259 2260 2261 2262 2263 2264 2265 2266
            isScrollable: true,
            controller: controller,
            tabs: tabs,
          ),
        ),
      ),
    );

2267 2268 2269
    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';

2270
    expect(semantics, includesNodeWith(actions: <SemanticsAction>[SemanticsAction.scrollLeft]));
2271 2272
    expect(semantics, includesNodeWith(label: tab0title));
    expect(semantics, isNot(includesNodeWith(label: tab10title)));
2273 2274 2275 2276

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

2277
    expect(semantics, isNot(includesNodeWith(label: tab0title)));
2278
    expect(semantics, includesNodeWith(actions: <SemanticsAction>[SemanticsAction.scrollLeft, SemanticsAction.scrollRight]));
2279
    expect(semantics, includesNodeWith(label: tab10title));
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289

    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]));
2290 2291
    expect(semantics, includesNodeWith(label: tab0title));
    expect(semantics, isNot(includesNodeWith(label: tab10title)));
2292 2293 2294 2295

    semantics.dispose();
  });

2296
  testWidgets('TabBar etc with zero tabs', (WidgetTester tester) async {
2297
    final TabController controller = TabController(
2298 2299 2300 2301 2302
      vsync: const TestVSync(),
      length: 0,
    );

    await tester.pumpWidget(
2303
      boilerplate(
2304
        child: Column(
2305
          children: <Widget>[
2306
            TabBar(
2307 2308 2309
              controller: controller,
              tabs: const <Widget>[],
            ),
2310 2311
            Flexible(
              child: TabBarView(
2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
                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.

2327
    await tester.fling(find.byType(TabBar), const Offset(-100.0, 0.0), 5000.0, warnIfMissed: false);
2328
    await tester.pumpAndSettle();
2329

2330 2331
    await tester.fling(find.byType(TabBarView), const Offset(100.0, 0.0), 5000.0);
    await tester.pumpAndSettle();
2332 2333 2334 2335 2336

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

  testWidgets('TabBar etc with one tab', (WidgetTester tester) async {
2337
    final TabController controller = TabController(
2338 2339 2340 2341 2342
      vsync: const TestVSync(),
      length: 1,
    );

    await tester.pumpWidget(
2343
      boilerplate(
2344
        child: Column(
2345
          children: <Widget>[
2346
            TabBar(
2347
              controller: controller,
2348
              tabs: const <Widget>[Tab(text: 'TAB')],
2349
            ),
2350 2351
            Flexible(
              child: TabBarView(
2352
                controller: controller,
2353
                children: const <Widget>[Text('PAGE')],
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366
              ),
            ),
          ],
        ),
      ),
    );

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

2367 2368 2369 2370
    // 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);
2371 2372 2373

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

2374 2375
    await tester.fling(find.byType(TabBar), const Offset(-100.0, 0.0), 5000.0);
    await tester.pump(const Duration(milliseconds: 50));
2376 2377
    expect(tester.getTopLeft(find.widgetWithText(Tab, 'TAB')).dx, tabLeft);
    expect(tester.getTopRight(find.widgetWithText(Tab, 'TAB')).dx, tabRight);
2378
    await tester.pumpAndSettle();
2379

2380 2381
    await tester.fling(find.byType(TabBarView), const Offset(100.0, 0.0), 5000.0);
    await tester.pump(const Duration(milliseconds: 50));
2382 2383
    expect(tester.getTopLeft(find.widgetWithText(Tab, 'TAB')).dx, tabLeft);
    expect(tester.getTopRight(find.widgetWithText(Tab, 'TAB')).dx, tabRight);
2384
    await tester.pumpAndSettle();
2385 2386 2387 2388 2389 2390

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

2391
  testWidgets('can tap on indicator at very bottom of TabBar to switch tabs', (WidgetTester tester) async {
2392
    final TabController controller = TabController(
2393 2394 2395 2396 2397 2398
      vsync: const TestVSync(),
      length: 2,
      initialIndex: 0,
    );

    await tester.pumpWidget(
2399
      boilerplate(
2400
        child: Column(
2401
          children: <Widget>[
2402
            TabBar(
2403 2404
              controller: controller,
              indicatorWeight: 30.0,
2405
              tabs: const <Widget>[Tab(text: 'TAB1'), Tab(text: 'TAB2')],
2406
            ),
2407 2408
            Flexible(
              child: TabBarView(
2409
                controller: controller,
2410
                children: const <Widget>[Text('PAGE1'), Text('PAGE2')],
2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426
              ),
            ),
          ],
        ),
      ),
    );

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

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

2431 2432 2433
    final List<Tab> tabs = List<Tab>.generate(2, (int index) {
      return Tab(
        child: Semantics(
2434
          label: 'Semantics override $index',
2435 2436
          child: ExcludeSemantics(
            child: Text('TAB #$index'),
2437 2438 2439 2440 2441
          ),
        ),
      );
    });

2442
    final TabController controller = TabController(
2443 2444 2445 2446 2447 2448 2449
      vsync: const TestVSync(),
      length: tabs.length,
      initialIndex: 0,
    );

    await tester.pumpWidget(
      boilerplate(
2450
        child: Semantics(
2451
          container: true,
2452
          child: TabBar(
2453 2454 2455 2456 2457 2458 2459 2460
            isScrollable: true,
            controller: controller,
            tabs: tabs,
          ),
        ),
      ),
    );

2461
    final TestSemantics expectedSemantics = TestSemantics.root(
2462
      children: <TestSemantics>[
2463
        TestSemantics.rootChild(
2464
          id: 1,
2465 2466
          rect: TestSemantics.fullScreen,
          children: <TestSemantics>[
2467
            TestSemantics(
2468 2469 2470
              id: 2,
              rect: TestSemantics.fullScreen,
              children: <TestSemantics>[
2471
                TestSemantics(
2472 2473
                    id: 3,
                    rect: TestSemantics.fullScreen,
2474
                    flags: <SemanticsFlag>[SemanticsFlag.hasImplicitScrolling],
2475
                    children: <TestSemantics>[
2476
                      TestSemantics(
2477
                        id: 4,
2478 2479 2480 2481 2482
                        flags: <SemanticsFlag>[
                          SemanticsFlag.isSelected,
                          SemanticsFlag.isFocusable,
                        ],
                        actions: <SemanticsAction>[SemanticsAction.tap],
2483
                        label: 'Semantics override 0\nTab 1 of 2',
Dan Field's avatar
Dan Field committed
2484
                        rect: const Rect.fromLTRB(0.0, 0.0, 116.0, kTextTabBarHeight),
2485
                        transform: Matrix4.translationValues(0.0, 276.0, 0.0),
2486
                      ),
2487
                      TestSemantics(
2488
                        id: 5,
2489 2490
                        flags: <SemanticsFlag>[SemanticsFlag.isFocusable],
                        actions: <SemanticsAction>[SemanticsAction.tap],
2491
                        label: 'Semantics override 1\nTab 2 of 2',
Dan Field's avatar
Dan Field committed
2492
                        rect: const Rect.fromLTRB(0.0, 0.0, 116.0, kTextTabBarHeight),
2493
                        transform: Matrix4.translationValues(116.0, 276.0, 0.0),
2494
                      ),
2495 2496
                    ],
                ),
2497 2498
              ],
            ),
2499 2500 2501 2502 2503 2504 2505 2506
          ],
        ),
      ],
    );

    expect(semantics, hasSemantics(expectedSemantics));

    semantics.dispose();
2507
  });
2508

2509 2510 2511 2512
  testWidgets('can be notified of TabBar onTap behavior', (WidgetTester tester) async {
    int tabIndex = -1;

    Widget buildFrame({
2513 2514
      required TabController controller,
      required List<String> tabs,
2515 2516
    }) {
      return boilerplate(
2517 2518 2519 2520 2521 2522
        child: TabBar(
          controller: controller,
          tabs: tabs.map<Widget>((String tab) => Tab(text: tab)).toList(),
          onTap: (int index) {
            tabIndex = index;
          },
2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587
        ),
      );
    }

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

2588
  test('illegal constructor combinations', () {
2589 2590 2591
    expect(() => Tab(icon: nonconst(null)), throwsAssertionError);
    expect(() => Tab(icon: Container(), text: 'foo', child: Container()), throwsAssertionError);
    expect(() => Tab(text: 'foo', child: Container()), throwsAssertionError);
2592
  });
2593

2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615
  testWidgets('Tabs changes mouse cursor when a tab is hovered', (WidgetTester tester) async {
    final List<String> tabs = <String>['A', 'B'];
    await tester.pumpWidget(MaterialApp(home: DefaultTabController(
        length: tabs.length,
        child: Scaffold(
          body: MouseRegion(
            cursor: SystemMouseCursors.forbidden,
            child: TabBar(
              mouseCursor: SystemMouseCursors.text,
              tabs: tabs.map<Widget>((String tab) => Tab(text: tab)).toList(),
            ),
          ),
        ),
      ),
    ));

    final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1);
    await gesture.addPointer(location: tester.getCenter(find.byType(Tab).first));
    addTearDown(gesture.removePointer);

    await tester.pump();

2616
    expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.text);
2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630

    // Test default cursor
    await tester.pumpWidget(MaterialApp(home: DefaultTabController(
        length: tabs.length,
        child: Scaffold(
          body: MouseRegion(
            cursor: SystemMouseCursors.forbidden,
            child: TabBar(
              tabs: tabs.map<Widget>((String tab) => Tab(text: tab)).toList(),
            ),
          ),
        ),
      ),
    ));
2631
    expect(RendererBinding.instance!.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.click);
2632
  });
2633 2634 2635 2636 2637 2638

  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(
2639
        child: Container(
2640
          alignment: Alignment.topLeft,
2641
          child: TabBar(
2642
            controller: controller,
2643
            tabs: const <Tab>[
2644 2645
              Tab(text: 'LEFT'),
              Tab(text: 'RIGHT'),
2646 2647 2648 2649 2650 2651
            ],
          ),
        ),
      );
    }

2652
    final TabController controller1 = TabController(
2653 2654 2655 2656 2657
      vsync: const TestVSync(),
      length: 2,
      initialIndex: 0,
    );

2658
    final TabController controller2 = TabController(
2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672
      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)

2673
    const double indicatorY = 48.0 - indicatorWeight / 2.0;
2674 2675 2676 2677
    double indicatorLeft = indicatorWeight / 2.0;
    double indicatorRight = 400.0 - indicatorWeight / 2.0; // 400 = screen_width / 2
    expect(tabBarBox, paints..line(
      strokeWidth: indicatorWeight,
2678 2679
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693
    ));

    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,
2694 2695
      p1: Offset(indicatorLeft, indicatorY),
      p2: Offset(indicatorRight, indicatorY),
2696 2697 2698
    ));
  });

2699 2700 2701 2702 2703 2704 2705 2706
  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,
    ));
2707
  });
2708

2709 2710 2711 2712 2713 2714 2715 2716
  testWidgets('Tab indicator color should not be adjusted when disable [automaticIndicatorColorAdjustment]', (WidgetTester tester) async {
     // Regression test for https://github.com/flutter/flutter/issues/68077
     final List<String> tabs = <String>['LEFT', 'RIGHT'];
     await tester.pumpWidget(buildLeftRightApp(tabs: tabs, value: 'LEFT', automaticIndicatorColorAdjustment: false));
     final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));
     expect(tabBarBox, paints..line(
       color: const Color(0xff2196f3),
     ));
2717 2718
  });

2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736
  group('Tab feedback', () {
    late FeedbackTester feedback;

    setUp(() {
      feedback = FeedbackTester();
    });

    tearDown(() {
      feedback.dispose();
    });

    testWidgets('Tab feedback is enabled (default)', (WidgetTester tester) async {
      await tester.pumpWidget(
        boilerplate(
          child: const DefaultTabController(
            length: 1,
            child: TabBar(
              tabs: <Tab>[
2737
                Tab(text: 'A'),
2738
              ],
2739 2740 2741
            ),
          ),
        ),
2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760
      );
      await tester.tap(find.byType(InkWell), pointer: 1);
      await tester.pump(const Duration(seconds: 1));
      expect(feedback.clickSoundCount, 1);
      expect(feedback.hapticCount, 0);

      await tester.tap(find.byType(InkWell), pointer: 1);
      await tester.pump(const Duration(seconds: 1));
      expect(feedback.clickSoundCount, 2);
      expect(feedback.hapticCount, 0);
    });

    testWidgets('Tab feedback is disabled', (WidgetTester tester) async {
      await tester.pumpWidget(
        boilerplate(
          child: const DefaultTabController(
            length: 1,
            child: TabBar(
              tabs: <Tab>[
2761
                Tab(text: 'A'),
2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780
              ],
              enableFeedback: false,
            ),
          ),
        ),
      );
      await tester.tap(find.byType(InkWell), pointer: 1);
      await tester.pump(const Duration(seconds: 1));
      expect(feedback.clickSoundCount, 0);
      expect(feedback.hapticCount, 0);

      await tester.longPress(find.byType(InkWell), pointer: 1);
      await tester.pump(const Duration(seconds: 1));
      expect(feedback.clickSoundCount, 0);
      expect(feedback.hapticCount, 0);
    });
  });

  group('Tab overlayColor affects ink response', () {
2781
    testWidgets("Tab's ink well changes color on hover with Tab overlayColor", (WidgetTester tester) async {
2782 2783 2784 2785 2786 2787
      await tester.pumpWidget(
        boilerplate(
          child: DefaultTabController(
            length: 1,
            child: TabBar(
              tabs: const <Tab>[
2788
                Tab(text: 'A'),
2789 2790 2791 2792 2793 2794 2795 2796
              ],
              overlayColor: MaterialStateProperty.resolveWith<Color>(
                (Set<MaterialState> states) {
                 if (states.contains(MaterialState.hovered))
                    return const Color(0xff00ff00);
                  if (states.contains(MaterialState.pressed))
                    return const Color(0xf00fffff);
                  return const Color(0xffbadbad); // Shouldn't happen.
2797
                },
2798
              ),
2799 2800 2801
            ),
          ),
        ),
2802 2803 2804 2805 2806 2807 2808 2809 2810 2811
      );
      final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
      await gesture.addPointer();
      addTearDown(gesture.removePointer);
      await gesture.moveTo(tester.getCenter(find.byType(Tab)));
      await tester.pumpAndSettle();
      final RenderObject inkFeatures = tester.allRenderObjects.firstWhere((RenderObject object) => object.runtimeType.toString() == '_RenderInkFeatures');
      expect(inkFeatures, paints..rect(rect: const Rect.fromLTRB(0.0, 276.0, 800.0, 324.0), color: const Color(0xff00ff00)));
    });

2812
    testWidgets(
2813
      "Tab's ink response splashColor matches resolved Tab overlayColor for MaterialState.pressed",
2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833
      (WidgetTester tester) async {
        const Color splashColor = Color(0xf00fffff);
        await tester.pumpWidget(
          boilerplate(
            child: DefaultTabController(
              length: 1,
              child: TabBar(
                tabs: const <Tab>[
                  Tab(text: 'A'),
                ],
                overlayColor: MaterialStateProperty.resolveWith<Color>(
                  (Set<MaterialState> states) {
                    if (states.contains(MaterialState.hovered))
                      return const Color(0xff00ff00);
                    if (states.contains(MaterialState.pressed))
                      return splashColor;
                    return const Color(0xffbadbad); // Shouldn't happen.
                  },
                ),
              ),
2834
            ),
2835 2836 2837 2838 2839 2840 2841 2842 2843
          ),
        );
        await tester.pumpAndSettle();
        final TestGesture gesture = await tester.startGesture(tester.getRect(find.byType(InkWell)).center);
        await tester.pump(const Duration(milliseconds: 200)); // unconfirmed splash is well underway
        final RenderObject inkFeatures = tester.allRenderObjects.firstWhere((RenderObject object) => object.runtimeType.toString() == '_RenderInkFeatures');
        expect(inkFeatures, paints..circle(x: 400, y: 24, color: splashColor));
        await gesture.up();
      },
2844 2845
    );
  });
2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895
  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);
  });

2896 2897 2898 2899 2900 2901 2902 2903 2904
  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',
    ];
2905
    final TabController controller = TabController(
2906 2907 2908 2909
      vsync: const TestVSync(),
      length: tabs.length,
    );
    await tester.pumpWidget(
2910 2911
      MaterialApp(
        home: Align(
2912
          alignment: Alignment.topLeft,
2913
          child: SizedBox(
2914 2915
            width: 300.0,
            height: 200.0,
2916 2917
            child: Scaffold(
              appBar: AppBar(
2918
                title: const Text('tabs'),
2919
                bottom: TabBar(
2920
                  controller: controller,
2921
                  tabs: tabs.map<Widget>((String tab) => Tab(text: tab)).toList(),
2922 2923
                ),
              ),
2924
              body: TabBarView(
2925 2926
                controller: controller,
                children: <Widget>[
2927
                  AlwaysKeepAliveWidget(key: UniqueKey()),
2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947
                  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);
  });
2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958

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

2959
    Widget buildTestWidget({double? width, double? height}) {
2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008
      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.
  });
3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026

  // 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(),
          ),
        ),
      );
    }
3027
    TabController getController() => DefaultTabController.of(tester.element(find.text('A')))!;
3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058

    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');
3059 3060
    const Color selectedTabColor = Color(0x00000001);
    const Color unselectedTabColor = Color(0x00000002);
3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075

    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));
  });
3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091

  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>[
3092
                  TextButton(
3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126
                    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);
  });
3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149

  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(
3150
                      children: tabTextContent.map((String textContent) => Tab(text: "$textContent's view")).toList(),
3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192
                    )
                  : 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);
3193
    expect(find.text("Tab 1's view"), findsOneWidget);
3194 3195 3196 3197 3198 3199

    // Dynamically updates to zero tabs properly
    await tester.tap(find.byKey(const Key('Delete tab')));
    await tester.pumpAndSettle();
    expect(find.text('No tabs'), findsOneWidget);
  });
3200

3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235
  testWidgets('TabBar - updating to and from zero tabs', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/68962.
    final List<String> tabTitles = <String>[];
    final List<Widget> tabContents = <Widget>[];
    TabController _tabController = TabController(length: tabContents.length, vsync: const TestVSync());

    void _onTabAdd(StateSetter setState) {
      setState(() {
        tabTitles.add('Tab ${tabTitles.length + 1}');
        tabContents.add(
          Container(
            color: Colors.red,
            height: 200,
            width: 200,
          ),
        );
        _tabController = TabController(length: tabContents.length, vsync: const TestVSync());
      });
    }

    void _onTabRemove(StateSetter setState) {
      setState(() {
        tabTitles.removeLast();
        tabContents.removeLast();
        _tabController = TabController(length: tabContents.length, vsync: const TestVSync());
      });
    }

    await tester.pumpWidget(
      MaterialApp(
        home: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
            return Scaffold(
              appBar: AppBar(
                actions: <Widget>[
3236
                  TextButton(
3237 3238 3239 3240
                    key: const Key('Add tab'),
                    child: const Text('Add tab'),
                    onPressed: () => _onTabAdd(setState),
                  ),
3241
                  TextButton(
3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275
                    key: const Key('Remove tab'),
                    child: const Text('Remove tab'),
                    onPressed: () => _onTabRemove(setState),
                  ),
                ],
                bottom: PreferredSize(
                  preferredSize: const Size.fromHeight(40.0),
                  child: Expanded(
                    child: TabBar(
                      controller: _tabController,
                      tabs: tabTitles
                        .map((String title) => Tab(text: title))
                        .toList(),
                    ),
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );

    expect(find.text('Tab 1'), findsNothing);
    expect(find.text('Add tab'), findsOneWidget);
    await tester.tap(find.byKey(const Key('Add tab')));
    await tester.pumpAndSettle();
    expect(find.text('Tab 1'), findsOneWidget);

    await tester.tap(find.byKey(const Key('Remove tab')));
    await tester.pumpAndSettle();
    expect(find.text('Tab 1'), findsNothing);
  });

3276 3277 3278 3279 3280 3281 3282 3283 3284 3285
   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')),
    ];

3286 3287
    const TabBar tabBarWithText = TabBar(tabs: tabListWithText, indicatorWeight: indicatorWeight);
    const TabBar tabBarWithTextChild = TabBar(tabs: tabListWithTextChild, indicatorWeight: indicatorWeight);
3288 3289 3290

    expect(tabBarWithText.preferredSize, tabBarWithTextChild.preferredSize);
   });
3291 3292 3293

  testWidgets('Setting TabController index should make TabBar indicator immediately pop into the position', (WidgetTester tester) async {
    const List<Tab> tabs = <Tab>[
3294
      Tab(text: 'A'), Tab(text: 'B'), Tab(text: 'C'),
3295 3296
    ];
    const Color indicatorColor = Color(0xFFFF0000);
3297
    late TabController tabController;
3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312

    Widget buildTabControllerFrame(BuildContext context, TabController controller) {
      tabController = controller;
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            bottom: TabBar(
              controller: controller,
              tabs: tabs,
              indicatorColor: indicatorColor,
            ),
          ),
          body: TabBarView(
            controller: controller,
            children: tabs.map((Tab tab) {
3313
              return Center(child: Text(tab.text!));
3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355
            }).toList(),
          ),
        ),
      );
    }

    await tester.pumpWidget(TabControllerFrame(
      builder: buildTabControllerFrame,
      length: tabs.length,
      initialIndex: 0,
    ));

    final RenderBox box = tester.renderObject(find.byType(TabBar));
    final TabIndicatorRecordingCanvas canvas = TabIndicatorRecordingCanvas(indicatorColor);
    final TestRecordingPaintingContext context = TestRecordingPaintingContext(canvas);

    box.paint(context, Offset.zero);
    double expectedIndicatorLeft = canvas.indicatorRect.left;

    final PageView pageView = tester.widget(find.byType(PageView));
    final PageController pageController = pageView.controller;
    void pageControllerListener() {
      // Whenever TabBarView scrolls due to changing TabController's index,
      // check if indicator stays idle in its expectedIndicatorLeft
      box.paint(context, Offset.zero);
      expect(canvas.indicatorRect.left, expectedIndicatorLeft);
    }

    // Moving from index 0 to 2 (distanced tabs)
    tabController.index = 2;
    box.paint(context, Offset.zero);
    expectedIndicatorLeft = canvas.indicatorRect.left;
    pageController.addListener(pageControllerListener);
    await tester.pumpAndSettle();

    // Moving from index 2 to 1 (neighboring tabs)
    tabController.index = 1;
    box.paint(context, Offset.zero);
    expectedIndicatorLeft = canvas.indicatorRect.left;
    await tester.pumpAndSettle();
    pageController.removeListener(pageControllerListener);
  });
3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367

  testWidgets('Setting BouncingScrollPhysics on TabBarView does not include ClampingScrollPhysics', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/57708
    await tester.pumpWidget(MaterialApp(
      home: DefaultTabController(
        length: 10,
        child: Scaffold(
          body: TabBarView(
            physics: const BouncingScrollPhysics(),
            children: List<Widget>.generate(10, (int i) => Center(child: Text('index $i'))),
          ),
        ),
3368
      ),
3369 3370 3371 3372 3373
    ));

    final PageView pageView = tester.widget<PageView>(find.byType(PageView));
    expect(pageView.physics.toString().contains('ClampingScrollPhysics'), isFalse);
  });
3374

3375 3376 3377 3378 3379 3380
  testWidgets('TabController changes offset attribute', (WidgetTester tester) async {
    final TabController controller = TabController(
      vsync: const TestVSync(),
      length: 2,
    );

3381 3382
    late Color firstColor;
    late Color secondColor;
3383 3384 3385 3386 3387 3388 3389 3390 3391

    await tester.pumpWidget(
      boilerplate(
        child: TabBar(
          controller: controller,
          labelColor: Colors.white,
          unselectedLabelColor: Colors.black,
          tabs: <Widget>[
            Builder(builder: (BuildContext context) {
3392
              firstColor = DefaultTextStyle.of(context).style.color!;
3393 3394 3395
              return const Text('First');
            }),
            Builder(builder: (BuildContext context) {
3396
              secondColor = DefaultTextStyle.of(context).style.color!;
3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425
              return const Text('Second');
            }),
          ],
        ),
      ),
    );

    expect(firstColor, equals(Colors.white));
    expect(secondColor, equals(Colors.black));

    controller.offset = 0.6;
    await tester.pump();

    expect(firstColor, equals(Color.lerp(Colors.white, Colors.black, 0.6)));
    expect(secondColor, equals(Color.lerp(Colors.black, Colors.white, 0.6)));

    controller.index = 1;
    await tester.pump();

    expect(firstColor, equals(Colors.black));
    expect(secondColor, equals(Colors.white));

    controller.offset = 0.6;
    await tester.pump();

    expect(firstColor, equals(Colors.black));
    expect(secondColor, equals(Colors.white));
  });

3426
  testWidgets('Crash on dispose', (WidgetTester tester) async {
3427
    await tester.pumpWidget(const Padding(padding: EdgeInsets.only(right: 200.0), child: TabBarDemo()));
3428 3429 3430 3431
    await tester.tap(find.byIcon(Icons.directions_bike));
    // There was a time where this would throw an exception
    // because we tried to send a notification on dispose.
  });
3432

3433
  testWidgets("TabController's animation value should be in sync with TabBarView's scroll value when user interrupts ballistic scroll", (WidgetTester tester) async {
3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467
    final TabController tabController = TabController(
      vsync: const TestVSync(),
      length: 3,
    );

    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: SizedBox.expand(
        child: Center(
          child: SizedBox(
            width: 400.0,
            height: 400.0,
            child: TabBarView(
              controller: tabController,
              children: const <Widget>[
                Center(child: Text('0')),
                Center(child: Text('1')),
                Center(child: Text('2')),
              ],
            ),
          ),
        ),
      ),
    ));

    final PageView pageView = tester.widget(find.byType(PageView));
    final PageController pageController = pageView.controller;
    final ScrollPosition position = pageController.position;

    expect(tabController.index, 0);
    expect(position.pixels, 0.0);

    pageController.jumpTo(300.0);
    await tester.pump();
3468
    expect(tabController.animation!.value, pageController.page);
3469 3470 3471 3472 3473

    // Touch TabBarView while ballistic scrolling is happening and
    // check if tabController's animation value properly follows page value.
    await tester.startGesture(tester.getCenter(find.byType(PageView)));
    await tester.pump();
3474
    expect(tabController.animation!.value, pageController.page);
3475
  });
3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521

  testWidgets('Does not instantiate intermediate tabs during animation', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/14316.
    final List<String> log = <String>[];
    await tester.pumpWidget(MaterialApp(
      home: DefaultTabController(
        length: 5,
        child: Scaffold(
          appBar: AppBar(
            bottom: const TabBar(
              tabs: <Widget>[
                Tab(text: 'car'),
                Tab(text: 'transit'),
                Tab(text: 'bike'),
                Tab(text: 'boat'),
                Tab(text: 'bus'),
              ],
            ),
            title: const Text('Tabs Test'),
          ),
          body: TabBarView(
            children: <Widget>[
              TabBody(index: 0, log: log),
              TabBody(index: 1, log: log),
              TabBody(index: 2, log: log),
              TabBody(index: 3, log: log),
              TabBody(index: 4, log: log),
            ],
          ),
        ),
      ),
    ));

    expect(find.text('0'), findsOneWidget);
    expect(find.text('3'), findsNothing);
    expect(log, <String>['init: 0']);

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

    expect(find.text('0'), findsNothing);
    expect(find.text('3'), findsOneWidget);

    // No other tab got instantiated during the animation.
    expect(log, <String>['init: 0', 'init: 3', 'dispose: 0']);
  });
3522

3523
  testWidgets("TabController's animation value should be updated when TabController's index >= tabs's length", (WidgetTester tester) async {
3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552
    // This is a regression test for the issue brought up here
    // https://github.com/flutter/flutter/issues/79226

    final List<String> tabs = <String>['A', 'B', 'C'];
    await tester.pumpWidget(
      MaterialApp(
        home: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
            return DefaultTabController(
              length: tabs.length,
              child: Scaffold(
                appBar: AppBar(
                  bottom: TabBar(
                    tabs: tabs.map<Widget>((String tab) => Tab(text: tab)).toList(),
                  ),
                  actions: <Widget>[
                    TextButton(
                      child: const Text('Remove Last Tab'),
                      onPressed: () {
                        setState(() {
                          tabs.removeLast();
                        });
                      },
                    ),
                  ],
                ),
                body: TabBarView(
                  children: tabs.map<Widget>((String tab) => Tab(text: 'Tab child $tab')).toList(),
                ),
3553
              ),
3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577
            );
          },
        ),
      ),
    );

    TabController getController() => DefaultTabController.of(tester.element(find.text('B')))!;
    TabController controller = getController();

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

    controller = getController();
    expect(controller.index, 2);
    expect(controller.animation!.value, 2);

    await tester.tap(find.text('Remove Last Tab'));
    await tester.pumpAndSettle();

    controller = getController();
    expect(controller.index, 1);
    expect(controller.animation!.value, 1);
  });
3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653

  testWidgets('Tab preferredSize gives correct value', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: Row(
            children: const <Tab>[
              Tab(icon: Icon(Icons.message)),
              Tab(text: 'Two'),
              Tab(text: 'Three', icon: Icon(Icons.chat)),
            ],
          ),
        ),
      ),
    );

    final Tab firstTab = tester.widget(find.widgetWithIcon(Tab, Icons.message));
    final Tab secondTab = tester.widget(find.widgetWithText(Tab, 'Two'));
    final Tab thirdTab = tester.widget(find.widgetWithText(Tab, 'Three'));

    expect(firstTab.preferredSize, const Size.fromHeight(46.0));
    expect(secondTab.preferredSize, const Size.fromHeight(46.0));
    expect(thirdTab.preferredSize, const Size.fromHeight(72.0));
  });

  testWidgets('TabBar preferredSize gives correct value when there are both icon and text in tabs', (WidgetTester tester) async {
    await tester.pumpWidget(MaterialApp(
      home: DefaultTabController(
        length: 5,
        child: Scaffold(
          appBar: AppBar(
            bottom: const TabBar(
              tabs: <Widget>[
                Tab(text: 'car'),
                Tab(text: 'transit'),
                Tab(text: 'bike'),
                Tab(text: 'boat', icon: Icon(Icons.message)),
                Tab(text: 'bus'),
              ],
            ),
            title: const Text('Tabs Test'),
          ),
        ),
      ),
    ));

    final TabBar tabBar = tester.widget(find.widgetWithText(TabBar, 'car'));

    expect(tabBar.preferredSize, const Size.fromHeight(74.0));
  });

  testWidgets('TabBar preferredSize gives correct value when there is only icon or text in tabs', (WidgetTester tester) async {
    await tester.pumpWidget(MaterialApp(
      home: DefaultTabController(
        length: 5,
        child: Scaffold(
          appBar: AppBar(
            bottom: const TabBar(
              tabs: <Widget>[
                Tab(text: 'car'),
                Tab(icon: Icon(Icons.message)),
                Tab(text: 'bike'),
                Tab(icon: Icon(Icons.chat)),
                Tab(text: 'bus'),
              ],
            ),
            title: const Text('Tabs Test'),
          ),
        ),
      ),
    ));

    final TabBar tabBar = tester.widget(find.widgetWithText(TabBar, 'car'));

    expect(tabBar.preferredSize, const Size.fromHeight(48.0));
  });
3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749

  testWidgets('Tabs are given uniform padding in case of few tabs having both text and icon', (WidgetTester tester) async {
    const EdgeInsetsGeometry expectedPaddingAdjusted = EdgeInsets.symmetric(vertical: 13.0, horizontal: 16.0);
    const EdgeInsetsGeometry expectedPaddingDefault = EdgeInsets.symmetric(vertical: 0.0, horizontal: 16.0);

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            bottom: TabBar(
              controller: TabController(length: 3, vsync: const TestVSync()),
              tabs: const <Widget>[
                Tab(text: 'Tab 1', icon: Icon(Icons.plus_one)),
                Tab(text: 'Tab 2'),
                Tab(text: 'Tab 3'),
              ],
            ),
          ),
        ),
      ),
    );

    final Padding tabOne = tester.widget<Padding>(find.widgetWithText(Padding, 'Tab 1').first);
    final Padding tabTwo = tester.widget<Padding>(find.widgetWithText(Padding, 'Tab 2').first);
    final Padding tabThree = tester.widget<Padding>(find.widgetWithText(Padding, 'Tab 3').first);

    expect(tabOne.padding, expectedPaddingDefault);
    expect(tabTwo.padding, expectedPaddingAdjusted);
    expect(tabThree.padding, expectedPaddingAdjusted);
  });

  testWidgets('Tabs are given uniform padding when labelPadding is given', (WidgetTester tester) async {
    const EdgeInsetsGeometry labelPadding = EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0);
    const EdgeInsetsGeometry expectedPaddingAdjusted = EdgeInsets.symmetric(vertical: 23.0, horizontal: 20.0);
    const EdgeInsetsGeometry expectedPaddingDefault = EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0);

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            bottom: TabBar(
              labelPadding: labelPadding,
              controller: TabController(length: 3, vsync: const TestVSync()),
              tabs: const <Widget>[
                Tab(text: 'Tab 1', icon: Icon(Icons.plus_one)),
                Tab(text: 'Tab 2'),
                Tab(text: 'Tab 3'),
              ],
            ),
          ),
        ),
      ),
    );

    final Padding tabOne = tester.widget<Padding>(find.widgetWithText(Padding, 'Tab 1').first);
    final Padding tabTwo = tester.widget<Padding>(find.widgetWithText(Padding, 'Tab 2').first);
    final Padding tabThree = tester.widget<Padding>(find.widgetWithText(Padding, 'Tab 3').first);

    expect(tabOne.padding, expectedPaddingDefault);
    expect(tabTwo.padding, expectedPaddingAdjusted);
    expect(tabThree.padding, expectedPaddingAdjusted);
  });

  testWidgets('Tabs are given uniform padding TabBarTheme.labelPadding is given', (WidgetTester tester) async {
    const EdgeInsetsGeometry labelPadding = EdgeInsets.symmetric(vertical: 15.0, horizontal: 20);
    const EdgeInsetsGeometry expectedPaddingAdjusted = EdgeInsets.symmetric(vertical: 28.0, horizontal: 20.0);
    const EdgeInsetsGeometry expectedPaddingDefault = EdgeInsets.symmetric(vertical: 15.0, horizontal: 20.0);

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(
          tabBarTheme: const TabBarTheme(labelPadding: labelPadding),
        ),
        home: Scaffold(
          appBar: AppBar(
            bottom: TabBar(
              controller: TabController(length: 3, vsync: const TestVSync()),
              tabs: const <Widget>[
                Tab(text: 'Tab 1', icon: Icon(Icons.plus_one)),
                Tab(text: 'Tab 2'),
                Tab(text: 'Tab 3'),
              ],
            ),
          ),
        ),
      ),
    );

    final Padding tabOne = tester.widget<Padding>(find.widgetWithText(Padding, 'Tab 1').first);
    final Padding tabTwo = tester.widget<Padding>(find.widgetWithText(Padding, 'Tab 2').first);
    final Padding tabThree = tester.widget<Padding>(find.widgetWithText(Padding, 'Tab 3').first);

    expect(tabOne.padding, expectedPaddingDefault);
    expect(tabTwo.padding, expectedPaddingAdjusted);
    expect(tabThree.padding, expectedPaddingAdjusted);
  });
3750
}
3751 3752

class KeepAliveInk extends StatefulWidget {
3753
  const KeepAliveInk(this.title, {Key? key}) : super(key: key);
3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772
  final String title;
  @override
  State<StatefulWidget> createState() {
    return _KeepAliveInkState();
  }
}

class _KeepAliveInkState extends State<KeepAliveInk> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Ink(
      child: Text(widget.title),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
3773 3774

class TabBarDemo extends StatelessWidget {
3775 3776
  const TabBarDemo({Key? key}) : super(key: key);

3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DefaultTabController(
        length: 3,
        child: Scaffold(
          appBar: AppBar(
            bottom: const TabBar(
              tabs: <Widget>[
                Tab(icon: Icon(Icons.directions_car)),
                Tab(icon: Icon(Icons.directions_transit)),
                Tab(icon: Icon(Icons.directions_bike)),
              ],
            ),
            title: const Text('Tabs Demo'),
          ),
          body: const TabBarView(
            children: <Widget>[
              Icon(Icons.directions_car),
              Icon(Icons.directions_transit),
              Icon(Icons.directions_bike),
            ],
          ),
        ),
      ),
    );
  }
}
3805 3806

class MockScrollMetrics extends Fake implements ScrollMetrics {}
3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844

class TabBody extends StatefulWidget {
  const TabBody({ Key? key, required this.index, required this.log }) : super(key: key);

  final int index;
  final List<String> log;

  @override
  State<TabBody> createState() => TabBodyState();
}

class TabBodyState extends State<TabBody> {
  @override
  void initState() {
    widget.log.add('init: ${widget.index}');
    super.initState();
  }

  @override
  void didUpdateWidget(TabBody oldWidget) {
    super.didUpdateWidget(oldWidget);
    // To keep the logging straight, widgets must not change their index.
    assert(oldWidget.index == widget.index);
  }

  @override
  void dispose() {
    widget.log.add('dispose: ${widget.index}');
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text('${widget.index}'),
    );
  }
}