首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SliverAppBar与TabBar

SliverAppBar与TabBar
EN

Stack Overflow用户
提问于 2018-05-20 10:30:46
回答 4查看 22.7K关注 0票数 15

如何将TabBar添加到SliverAppBar中?到目前为止,当我向bottom添加SliverAppBar时,title被固定在这些选项卡上,而我希望它高于这些选项卡。

有什么想法吗?

我的代码:

代码语言:javascript
复制
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin{
  TabController controller;


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

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new CustomScrollView(
        slivers: <Widget>[
          new SliverAppBar(
            pinned: true,
            flexibleSpace: new FlexibleSpaceBar(
              title: new Text("Some title"),
            ),
            expandedHeight: 160.0,
            bottom: new TabBar(tabs: [
              new Tab(text: 'Tab 1'),
              new Tab(text: 'Tab 2'),
              new Tab(text: 'Tab 3'),
            ],
            controller: controller,
            ),
          ),
          new SliverList(
            delegate: new SliverChildBuilderDelegate(
              (context, idx) {
                return new ListTile(
                  title: new Text("$idx"),
                );
              },
              childCount: 20,
            ),
          ),
        ],
      ),
    );
  }
}
EN

回答 4

Stack Overflow用户

发布于 2018-06-14 08:44:50

FlexibleSpaceBar被堆放在工具栏和制表栏后面。https://docs.flutter.io/flutter/material/SliverAppBar/flexibleSpace.html

您应该将标题放在SilverAppBar:title而不是flexibleSpace中。

在代码中替换

代码语言:javascript
复制
flexibleSpace: new FlexibleSpaceBar(
                      title: new Text("Some title"),
               ),

使用

代码语言:javascript
复制
title: new Text("Some title"),
票数 4
EN

Stack Overflow用户

发布于 2019-12-02 14:51:46

您可以使用titlePadding填充:

代码语言:javascript
复制
              SliverAppBar(
                  pinned: true,
                  expandedHeight: 250,
                  primary: true,
                  forceElevated: true,
                  flexibleSpace: FlexibleSpaceBar(
                    titlePadding: EdgeInsets.only(bottom: 62),
                    title: Text(data.name),
                    centerTitle: true,
                    background: data.logo != null
                        ? Padding(
                            padding: const EdgeInsets.only(bottom: 46),
                            child: Container(
                              color: Colors.black,
                            ),
                          )
                        : null,
                  ),
                  bottom: TabBar(
                    tabs: [
                      Tab(text: "Tab1"),
                      Tab(text: "Tab2"),
                      Tab(text: "Tab3"),
                      Tab(text: "Tab4"),
                    ],
                  ),
                ),
票数 1
EN

Stack Overflow用户

发布于 2020-09-10 14:10:12

代码语言:javascript
复制
class Menu extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 2,
      child: SafeArea(
        child: Scaffold(
          body: Consumer<MenuBlock>(
              builder: (context, appController, child) {
            return CustomScrollView(
              slivers: <Widget>[
                SliverAppBar(
                  floating: true,
                  pinned: false,
                  snap: true,
                  forceElevated: true,
                  elevation: 1,
                  expandedHeight: 50.0,
                  title: Text('TITLE'),
                  actions: <Widget>[
                    IconButton(
                      icon: const Icon(Icons.add_circle),
                      tooltip: 'Add new entry',
                      onPressed: () {/* ... */},
                    ),
                  ],
                  bottom: TabBar(
                    tabs: [
                      Tab(text: "TAB1"),
                      Tab(text: "TAB2"),
                    ],
                  ),
                ),
                SliverList(
                  delegate: SliverChildListDelegate(
                    <Widget>[
                      Container(
                        // or SizedBox, etc.
                        height: 4200,
                        child: TabBarView(
                          children: [
                     
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            );
          }),
        ),
      ),
    );
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50433885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档