@@ -70,6 +70,7 @@ class Tab extends StatelessWidget implements PreferredSizeWidget{
|
||||
this.text,
|
||||
this.icon,
|
||||
this.iconMargin = const EdgeInsets.only(bottom: 10.0),
|
||||
this.height,
|
||||
this.child,
|
||||
}) : assert(text != null || child != null || icon != null),
|
||||
assert(text == null || child == null),
|
||||
@@ -96,6 +97,13 @@ class Tab extends StatelessWidget implements PreferredSizeWidget{
|
||||
/// [text] or [child] is non-null.
|
||||
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() {
|
||||
return child ?? Text(text!, softWrap: false, overflow: TextOverflow.fade);
|
||||
}
|
||||
@@ -104,16 +112,16 @@ class Tab extends StatelessWidget implements PreferredSizeWidget{
|
||||
Widget build(BuildContext context) {
|
||||
assert(debugCheckHasMaterial(context));
|
||||
|
||||
final double height;
|
||||
final double calculatedHeight;
|
||||
final Widget label;
|
||||
if (icon == null) {
|
||||
height = _kTabHeight;
|
||||
calculatedHeight = _kTabHeight;
|
||||
label = _buildLabelText();
|
||||
} else if (text == null && child == null) {
|
||||
height = _kTabHeight;
|
||||
calculatedHeight = _kTabHeight;
|
||||
label = icon!;
|
||||
} else {
|
||||
height = _kTextAndIconTabHeight;
|
||||
calculatedHeight = _kTextAndIconTabHeight;
|
||||
label = Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
@@ -128,7 +136,7 @@ class Tab extends StatelessWidget implements PreferredSizeWidget{
|
||||
}
|
||||
|
||||
return SizedBox(
|
||||
height: height,
|
||||
height: height ?? calculatedHeight,
|
||||
child: Center(
|
||||
widthFactor: 1.0,
|
||||
child: label,
|
||||
@@ -145,7 +153,9 @@ class Tab extends StatelessWidget implements PreferredSizeWidget{
|
||||
|
||||
@override
|
||||
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);
|
||||
else
|
||||
return const Size.fromHeight(_kTabHeight);
|
||||
|
||||
@@ -3747,6 +3747,71 @@ void main() {
|
||||
expect(tabTwo.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 {
|
||||
|
||||
Reference in New Issue
Block a user