Skip to content

Commit 3ea4d06

Browse files
author
Hans Muller
authored
Update TabBar indicator painter when tab controller changes (flutter#14825)
1 parent 01b53bd commit 3ea4d06

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

packages/flutter/lib/src/material/tabs.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -752,14 +752,15 @@ class _TabBarState extends State<TabBar> {
752752
@override
753753
void didUpdateWidget(TabBar oldWidget) {
754754
super.didUpdateWidget(oldWidget);
755-
if (widget.controller != oldWidget.controller)
755+
if (widget.controller != oldWidget.controller) {
756756
_updateTabController();
757-
758-
if (widget.indicatorColor != oldWidget.indicatorColor ||
757+
_initIndicatorPainter();
758+
} else if (widget.indicatorColor != oldWidget.indicatorColor ||
759759
widget.indicatorWeight != oldWidget.indicatorWeight ||
760760
widget.indicatorSize != oldWidget.indicatorSize ||
761-
widget.indicator != oldWidget.indicator)
761+
widget.indicator != oldWidget.indicator) {
762762
_initIndicatorPainter();
763+
}
763764

764765
if (widget.tabs.length > oldWidget.tabs.length) {
765766
final int delta = widget.tabs.length - oldWidget.tabs.length;

packages/flutter/test/material/tabs_test.dart

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,4 +1650,71 @@ void main() {
16501650
expect(() => new Tab(icon: new Container(), text: 'foo', child: new Container()), throwsAssertionError);
16511651
expect(() => new Tab(text: 'foo', child: new Container()), throwsAssertionError);
16521652
});
1653+
1654+
1655+
testWidgets('TabController changes', (WidgetTester tester) async {
1656+
// This is a regression test for https://github.com/flutter/flutter/issues/14812
1657+
1658+
Widget buildFrame(TabController controller) {
1659+
return boilerplate(
1660+
child: new Container(
1661+
alignment: Alignment.topLeft,
1662+
child: new TabBar(
1663+
controller: controller,
1664+
tabs: <Tab>[
1665+
const Tab(text: 'LEFT'),
1666+
const Tab(text: 'RIGHT'),
1667+
],
1668+
),
1669+
),
1670+
);
1671+
}
1672+
1673+
final TabController controller1 = new TabController(
1674+
vsync: const TestVSync(),
1675+
length: 2,
1676+
initialIndex: 0,
1677+
);
1678+
1679+
final TabController controller2 = new TabController(
1680+
vsync: const TestVSync(),
1681+
length: 2,
1682+
initialIndex: 0,
1683+
);
1684+
1685+
await tester.pumpWidget(buildFrame(controller1));
1686+
await tester.pumpWidget(buildFrame(controller2));
1687+
expect(controller1.index, 0);
1688+
expect(controller2.index, 0);
1689+
1690+
const double indicatorWeight = 2.0;
1691+
final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));
1692+
expect(tabBarBox.size.height, 48.0); // 48 = _kTabHeight(46) + indicatorWeight(2.0)
1693+
1694+
final double indicatorY = 48.0 - indicatorWeight / 2.0;
1695+
double indicatorLeft = indicatorWeight / 2.0;
1696+
double indicatorRight = 400.0 - indicatorWeight / 2.0; // 400 = screen_width / 2
1697+
expect(tabBarBox, paints..line(
1698+
strokeWidth: indicatorWeight,
1699+
p1: new Offset(indicatorLeft, indicatorY),
1700+
p2: new Offset(indicatorRight, indicatorY),
1701+
));
1702+
1703+
await tester.tap(find.text('RIGHT'));
1704+
await tester.pumpAndSettle();
1705+
expect(controller1.index, 0);
1706+
expect(controller2.index, 1);
1707+
1708+
// Verify that the TabBar's _IndicatorPainter is now listening to
1709+
// tabController2.
1710+
1711+
indicatorLeft = 400.0 + indicatorWeight / 2.0;
1712+
indicatorRight = 800.0 - indicatorWeight / 2.0;
1713+
expect(tabBarBox, paints..line(
1714+
strokeWidth: indicatorWeight,
1715+
p1: new Offset(indicatorLeft, indicatorY),
1716+
p2: new Offset(indicatorRight, indicatorY),
1717+
));
1718+
});
1719+
16531720
}

0 commit comments

Comments
 (0)