没有AppBar的Flutter TabBar

spo*_*oss 16 dart flutter

我试图创建一个没有AppBar的标签栏布局屏幕.我已经在这个链接上提到了解决方案:如何在没有app bar的情况下创建标签栏?但它不适合我.当我将TabBar放入appbar:参数时,以下是我的屏幕的样子:

在此输入图像描述

我的TabBar已经移动到状态栏下方的左上角,并且它全部挤在一个角落里.这几乎就好像根本就没有.

当我使用AppBar类但只传递bottom:参数时会发生什么:

在此输入图像描述

TabBar顶部有一个丑陋的空间,显然是AppBar标题.这是我的代码:

return new Scaffold(
      appBar: new TabBar(
        tabs: widget._tabs.map((_Page page){
          return Text(page.tabTitle);
        }).toList(),
        controller: _tabController,
        isScrollable: true,

      ),
      backgroundColor: Colors.white,
      body: new TabBarView(
          controller: _tabController,
          children: widget._tabs.map((_Page page){
            return new SafeArea(
                top:false,
                bottom: false,
                child: (page.page == Pages.cart?new CartHomeScreen():_lunchesLayout())
            );
          }).toList()
      ),
    );
Run Code Online (Sandbox Code Playgroud)

如何在没有顶部的空间的情况下使用TabBar,是否可以使两个标签项及其指示器伸展并填充侧面空间?

rmt*_*zie 36

你的第一个截图实际上显示它工作得很好 - 问题是"好"并不是你所期望的.对于标签栏,默认文本颜色为白色,因此您的标签不显示,而只显示底线,这是您在左上角看到的.此外,TabBar已经是一个首选的大小小部件,但它与AppBar的高度不同,所以如果这就是你想要的,它看起来就不像了.

这是一个让它看起来像应用栏的例子.kToolbarHeight是AppBar使用的常量.

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new MyAppState();
}

class MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'msc',
      home: new DefaultTabController(
        length: 2,
        child: new Scaffold(
          appBar: new PreferredSize(
            preferredSize: Size.fromHeight(kToolbarHeight),
            child: new Container(
              color: Colors.green,
              child: new SafeArea(
                child: Column(
                  children: <Widget>[
                    new Expanded(child: new Container()),
                    new TabBar(
                      tabs: [new Text("Lunches"), new Text("Cart")],
                    ),
                  ],
                ),
              ),
            ),
          ),
          body: new TabBarView(
            children: <Widget>[
              new Column(
                children: <Widget>[new Text("Lunches Page")],
              ),
              new Column(
                children: <Widget>[new Text("Cart Page")],
              )
            ],
          ),
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

结果如下:

显示标签应用栏的屏幕截图

  • 这个答案很好,但我更喜欢使用“flexibleSpace”而不是 appBar(我的用例)。看看https://github.com/flutter/flutter/issues/17459#issuecomment-387984398 (2认同)
  • @Touch感谢您的建议,我已将其添加到答案中,因为评论有时会丢失=D (2认同)

Jew*_*ana 17

按照下面的代码

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  TabController _tabController;

  @override
  void initState() {
    _tabController = new TabController(length: 2, vsync: this);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Container(
              height: MediaQuery.of(context).size.height / 2,
              child: Center(
                child: Text(
                  "Tabbar with out Appbar",
                  style: TextStyle(
                      color: Colors.white, fontWeight: FontWeight.bold),
                ),
              ),
              color: Colors.blue,
            ),
            TabBar(
              unselectedLabelColor: Colors.black,
              labelColor: Colors.red,
              tabs: [
                Tab(
                  text: '1st tab',
                ),
                Tab(
                  text: '2 nd tab',
                )
              ],
              controller: _tabController,
              indicatorSize: TabBarIndicatorSize.tab,
            ),
            Expanded(
              child: TabBarView(
                children: [
                  Container(child: Center(child: Text('people'))),
                  Text('Person')
                ],
                controller: _tabController,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

查看结果

在此处输入图片说明

  • 这正是我所需要的。谢谢。但记住要处理`tabController` (3认同)

Cop*_*oad 13

您不需要使用appBar属性:

DefaultTabController(
  length: 2,
  child: Scaffold(
    body: Column(
      children: [
        TabBar( // <-- Your TabBar
          tabs: [
            Tab(icon: Icon(Icons.call)),
            Tab(icon: Icon(Icons.settings)),
          ],
        ),
        Expanded(
          child: TabBarView( // <-- Your TabBarView
            children: [
              Container(color: Colors.blue),
              Container(color: Colors.red),
            ],
          ),
        ),
      ],
    ),
  ),
)
Run Code Online (Sandbox Code Playgroud)


小智 6

我从rmtmckenzie中获取代码,但仅创建了没有实质性应用的小部件

return new DefaultTabController(
      length: 3,
      child: new Scaffold(
        appBar: new PreferredSize(
          preferredSize: Size.fromHeight(kToolbarHeight),
          child: new Container(
            height: 50.0,
            child: new TabBar(
              tabs: [
                Tab(icon: Icon(Icons.directions_car,color: Colors.grey,)),
                Tab(icon: Icon(Icons.directions_transit,color: Colors.grey)),
                Tab(icon: Icon(Icons.directions_bike,color: Colors.grey)),
              ],
            ),
          ),
        ),
        body: TabBarView(
          children: [
            Icon(Icons.directions_car),
            Icon(Icons.directions_transit),
            Icon(Icons.directions_bike),
          ],
        ),
      ),
    ); 
Run Code Online (Sandbox Code Playgroud)


hsu*_*l4n 6

只需使用小部件toolbarHeight中的属性AppBar及其kMinInteractiveDimension值,如下所示:

Scaffold(
  appBar: AppBar(
    toolbarHeight: kMinInteractiveDimension,
    bottom: TabBar(
      controller: _tabController,
      tabs: [], // your tab bars
    ),
  ),
  body: TabBarView(
    controller: _tabController,
    children: [], // your tab views
  ),
);
Run Code Online (Sandbox Code Playgroud)


iDe*_*ode 5

TabBar直接在属性中使用appBar

@override
Widget build(BuildContext context) {
  return DefaultTabController(
    length: 2,
    child: Scaffold(
      appBar: TabBar(
        tabs: [
          Tab(icon: Icon(Icons.call)),
          Tab(icon: Icon(Icons.message)),
        ],
      ),
      body: TabBarView(
        children: [
          Center(child: Text('Call')),
          Center(child: Text('Message')),
        ],
      ),
    ),
  );
}
Run Code Online (Sandbox Code Playgroud)