Flutter:TabBar 剪切选项卡标题

问题描述 投票:0回答:3

我使用带有静态 4 个选项卡的 TabBar。我希望 TabBar 在屏幕上扩展宽度(从右到左边缘),但长选项卡标题会像这样被剪切:

我尝试使用

isScrollable: true
作为 TabBar。标题不会被剪裁,但这会导致宽度折叠:

我的代码:

  Widget build(BuildContext context) {
    return DefaultTabController(
        length: 4,
        child: Scaffold(
            appBar: AppBar(title: Text('Tabs'), centerTitle: true),
            body: TabBar(tabs: [
              Tab(text: 'Tab 1'),
              Tab(text: 'Tab 2'),
              Tab(text: 'Long tab name'),
              Tab(text: 'Tab 4'),
            ])));
  }

问题:在这种情况下如何实现全屏 TabBar 宽度而不裁剪标题?

flutter flutter-layout
3个回答
4
投票

通过添加使选项卡栏可滚动

 isScrollable: true,

3
投票

我想出了一个不同的解决方案,它可能会有所帮助 只需将此属性添加到您的选项卡栏小部件即可,

labelPadding: EdgeInsets.all(0.0),

为我工作


2
投票
     @override
 Widget build(BuildContext context) {
   return DefaultTabController(
      length: 4,
      child: Scaffold(
        appBar: AppBar(title: Text('Tabs'), centerTitle: true),
        body: Container(
          child: TabBar(tabs: [
            Tab(
              child: Text(
                "Tab 1",
                overflow: TextOverflow.clip,
                maxLines: 1,
              ),
            ),
            Tab(
              child: Text(
                "Tab 2",
                overflow: TextOverflow.clip,
                maxLines: 1,
              ),
            ),
            Tab(
              child: Text(
                "Long tab name sdsadsadas dsadasdsadsa",
                overflow: TextOverflow.clip,
                maxLines: 1,
              ),
            ),
            Tab(
              child: Text(
                "Tab 4",
                overflow: TextOverflow.clip,
                maxLines: 1,
              ),
            ),
          ]),
        )));
  }

© www.soinside.com 2019 - 2024. All rights reserved.