你的代码在我的显示中有不同的结果!无论如何,您可以使用 'child' 而不是 'text':
tabs: [
Tab(child: Text('Tab 1'),),
Tab(child: Text('Tab 2'),),
Tab(child: Text('Tab 3'),),
Tab(child: Text('Tab 4'),),
],
更新
由于您的文本大小不寻常且大于标准,并且由于选项卡大小是固定的,因此您可以自定义选项卡类并将固定大小更改为您想要的大小。根据需要更改这些尺寸:
final double _kTabHeight = 48.0;
final double _kTextAndIconTabHeight = 72.0;
'custom_tab.dart' 类是:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
class CustomTab extends StatelessWidget {
final double _kTabHeight = 80.0;
final double _kTextAndIconTabHeight = 72.0;
/// Creates a material design [TabBar] tab.
///
/// At least one of [text], [icon], and [child] must be non-null. The [text]
/// and [child] arguments must not be used at the same time. The
/// [iconMargin] is only useful when [icon] and either one of [text] or
/// [child] is non-null.
const CustomTab({
Key? key,
this.text,
this.icon,
this.iconMargin = const EdgeInsets.only(bottom: 10.0),
this.child,
}) : assert(text != null || child != null || icon != null),
assert(text == null || child == null),
super(key: key);
/// The text to display as the tab's label.
///
/// Must not be used in combination with [child].
final String? text;
/// The widget to be used as the tab's label.
///
/// Usually a [Text] widget, possibly wrapped in a [Semantics] widget.
///
/// Must not be used in combination with [text].
final Widget? child;
/// An icon to display as the tab's label.
final Widget? icon;
/// The margin added around the tab's icon.
///
/// Only useful when used in combination with [icon], and either one of
/// [text] or [child] is non-null.
final EdgeInsetsGeometry iconMargin;
Widget _buildLabelText() {
return child ?? Text(text!, softWrap: false, overflow: TextOverflow.fade);
}
@override
Widget build(BuildContext context) {
assert(debugCheckHasMaterial(context));
final double height;
final Widget label;
if (icon == null) {
height = _kTabHeight;
label = _buildLabelText();
} else if (text == null && child == null) {
height = _kTabHeight;
label = icon!;
} else {
height = _kTextAndIconTabHeight;
label = Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
child: icon,
margin: iconMargin,
),
_buildLabelText(),
],
);
}
return SizedBox(
height: height,
child: Center(
child: label,
widthFactor: 1.0,
),
);
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(StringProperty('text', text, defaultValue: null));
properties.add(DiagnosticsProperty<Widget>('icon', icon, defaultValue: null));
}
}
并像这样使用它:
tabs: [
CustomTab(text: 'Tab 1'),
CustomTab(text: 'Tab 2'),
CustomTab(text: 'Tab 3'),
CustomTab(text: 'Tab 4'),
],
我希望这对你有用:)