Unverified Commit 8f866ab5 authored by Gian's avatar Gian Committed by GitHub

Fix to the tab height (Issue #81500) (#81505)

parent 2bc12917
...@@ -70,6 +70,7 @@ class Tab extends StatelessWidget implements PreferredSizeWidget{ ...@@ -70,6 +70,7 @@ class Tab extends StatelessWidget implements PreferredSizeWidget{
this.text, this.text,
this.icon, this.icon,
this.iconMargin = const EdgeInsets.only(bottom: 10.0), this.iconMargin = const EdgeInsets.only(bottom: 10.0),
this.height,
this.child, this.child,
}) : assert(text != null || child != null || icon != null), }) : assert(text != null || child != null || icon != null),
assert(text == null || child == null), assert(text == null || child == null),
...@@ -96,6 +97,13 @@ class Tab extends StatelessWidget implements PreferredSizeWidget{ ...@@ -96,6 +97,13 @@ class Tab extends StatelessWidget implements PreferredSizeWidget{
/// [text] or [child] is non-null. /// [text] or [child] is non-null.
final EdgeInsetsGeometry iconMargin; final EdgeInsetsGeometry iconMargin;
/// The height of the [Tab].
///
/// If null, the height will be calculated based on the content of the [Tab]. When `icon` is not
/// null along with `child` or `text`, the default height is 72.0 pixels. Without an `icon`, the
/// height is 46.0 pixels.
final double? height;
Widget _buildLabelText() { Widget _buildLabelText() {
return child ?? Text(text!, softWrap: false, overflow: TextOverflow.fade); return child ?? Text(text!, softWrap: false, overflow: TextOverflow.fade);
} }
...@@ -104,16 +112,16 @@ class Tab extends StatelessWidget implements PreferredSizeWidget{ ...@@ -104,16 +112,16 @@ class Tab extends StatelessWidget implements PreferredSizeWidget{
Widget build(BuildContext context) { Widget build(BuildContext context) {
assert(debugCheckHasMaterial(context)); assert(debugCheckHasMaterial(context));
final double height; final double calculatedHeight;
final Widget label; final Widget label;
if (icon == null) { if (icon == null) {
height = _kTabHeight; calculatedHeight = _kTabHeight;
label = _buildLabelText(); label = _buildLabelText();
} else if (text == null && child == null) { } else if (text == null && child == null) {
height = _kTabHeight; calculatedHeight = _kTabHeight;
label = icon!; label = icon!;
} else { } else {
height = _kTextAndIconTabHeight; calculatedHeight = _kTextAndIconTabHeight;
label = Column( label = Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
...@@ -128,7 +136,7 @@ class Tab extends StatelessWidget implements PreferredSizeWidget{ ...@@ -128,7 +136,7 @@ class Tab extends StatelessWidget implements PreferredSizeWidget{
} }
return SizedBox( return SizedBox(
height: height, height: height ?? calculatedHeight,
child: Center( child: Center(
widthFactor: 1.0, widthFactor: 1.0,
child: label, child: label,
...@@ -145,7 +153,9 @@ class Tab extends StatelessWidget implements PreferredSizeWidget{ ...@@ -145,7 +153,9 @@ class Tab extends StatelessWidget implements PreferredSizeWidget{
@override @override
Size get preferredSize { Size get preferredSize {
if ((text != null || child != null) && icon != null) if (height != null)
return Size.fromHeight(height!);
else if ((text != null || child != null) && icon != null)
return const Size.fromHeight(_kTextAndIconTabHeight); return const Size.fromHeight(_kTextAndIconTabHeight);
else else
return const Size.fromHeight(_kTabHeight); return const Size.fromHeight(_kTabHeight);
......
...@@ -3747,6 +3747,71 @@ void main() { ...@@ -3747,6 +3747,71 @@ void main() {
expect(tabTwo.padding, expectedPaddingAdjusted); expect(tabTwo.padding, expectedPaddingAdjusted);
expect(tabThree.padding, expectedPaddingAdjusted); expect(tabThree.padding, expectedPaddingAdjusted);
}); });
testWidgets('Change tab bar height', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: DefaultTabController(
length: 4,
child: Scaffold(
appBar: AppBar(
bottom: const TabBar(
tabs: <Widget>[
Tab(
icon: Icon(Icons.check,size: 40),
child: Text('1 - OK',style: TextStyle(fontSize: 25),),
height: 85,
), // icon and child
Tab(
child: Text('2 - OK',style: TextStyle(fontSize: 25),),
height: 85,
), // child
Tab(
icon: Icon(Icons.done,size: 40),
height: 85,
), // icon
Tab(
text: '4 - OK',
height: 85,
), // text
],
),
),
),
),
));
final Tab firstTab = tester.widget(find.widgetWithIcon(Tab, Icons.check));
final Tab secTab = tester.widget(find.widgetWithText(Tab, '2 - OK' ));
final Tab thirdTab = tester.widget(find.widgetWithIcon(Tab, Icons.done));
final Tab fourthTab = tester.widget(find.widgetWithText(Tab, '4 - OK' ));
expect(firstTab.preferredSize.height, 85);
expect(firstTab.height, 85);
expect(secTab.height, 85);
expect(thirdTab.height, 85);
expect(fourthTab.height, 85);
});
testWidgets('Change tab bar height 2', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: DefaultTabController(
length: 1,
child: Scaffold(
appBar: AppBar(
bottom: const TabBar(
tabs: <Widget>[
Tab(
icon: Icon(Icons.check,size: 40),
text: '1 - OK',
height: 85,
), // icon and text
],
),
),
),
),
));
final Tab firstTab = tester.widget(find.widgetWithIcon(Tab, Icons.check));
expect(firstTab.height, 85);
});
} }
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