Unverified Commit 321aa452 authored by Chinmoy's avatar Chinmoy Committed by GitHub

Modified TabBar.preferredSize to remove hardcoded (#80792)

parent 59fc9216
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:math' as math;
import 'dart:ui' show lerpDouble; import 'dart:ui' show lerpDouble;
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
...@@ -57,7 +58,7 @@ enum TabBarIndicatorSize { ...@@ -57,7 +58,7 @@ enum TabBarIndicatorSize {
/// * [TabBarView], which displays a widget for the currently selected tab. /// * [TabBarView], which displays a widget for the currently selected tab.
/// * [TabController], which coordinates tab selection between a [TabBar] and a [TabBarView]. /// * [TabController], which coordinates tab selection between a [TabBar] and a [TabBarView].
/// * <https://material.io/design/components/tabs.html> /// * <https://material.io/design/components/tabs.html>
class Tab extends StatelessWidget { class Tab extends StatelessWidget implements PreferredSizeWidget{
/// Creates a material design [TabBar] tab. /// Creates a material design [TabBar] tab.
/// ///
/// At least one of [text], [icon], and [child] must be non-null. The [text] /// At least one of [text], [icon], and [child] must be non-null. The [text]
...@@ -141,6 +142,14 @@ class Tab extends StatelessWidget { ...@@ -141,6 +142,14 @@ class Tab extends StatelessWidget {
properties.add(StringProperty('text', text, defaultValue: null)); properties.add(StringProperty('text', text, defaultValue: null));
properties.add(DiagnosticsProperty<Widget>('icon', icon, defaultValue: null)); properties.add(DiagnosticsProperty<Widget>('icon', icon, defaultValue: null));
} }
@override
Size get preferredSize {
if ((text != null || child != null) && icon != null)
return const Size.fromHeight(_kTextAndIconTabHeight);
else
return const Size.fromHeight(_kTabHeight);
}
} }
class _TabStyle extends AnimatedWidget { class _TabStyle extends AnimatedWidget {
...@@ -891,14 +900,14 @@ class TabBar extends StatefulWidget implements PreferredSizeWidget { ...@@ -891,14 +900,14 @@ class TabBar extends StatefulWidget implements PreferredSizeWidget {
/// [AppBar] uses this size to compute its own preferred size. /// [AppBar] uses this size to compute its own preferred size.
@override @override
Size get preferredSize { Size get preferredSize {
double maxHeight = _kTabHeight;
for (final Widget item in tabs) { for (final Widget item in tabs) {
if (item is Tab) { if (item is PreferredSizeWidget) {
final Tab tab = item; final double itemHeight = item.preferredSize.height;
if ((tab.text != null || tab.child != null) && tab.icon != null) maxHeight = math.max(itemHeight, maxHeight);
return Size.fromHeight(_kTextAndIconTabHeight + indicatorWeight);
} }
} }
return Size.fromHeight(_kTabHeight + indicatorWeight); return Size.fromHeight(maxHeight + indicatorWeight);
} }
@override @override
......
...@@ -3457,6 +3457,82 @@ void main() { ...@@ -3457,6 +3457,82 @@ void main() {
expect(controller.index, 1); expect(controller.index, 1);
expect(controller.animation!.value, 1); expect(controller.animation!.value, 1);
}); });
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));
});
} }
class KeepAliveInk extends StatefulWidget { class KeepAliveInk extends StatefulWidget {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment