【问题标题】:Flutter app bar layout: action buttons in the same row with tab bar?Flutter应用栏布局:与标签栏在同一行的操作按钮?
【发布时间】:2021-08-08 13:03:30
【问题描述】:

假设我想创建一个应用栏,在开始和结束时有两个操作按钮,同时在中间有一个标签栏,都在同一行。这是我为使标签栏工作而编写的代码:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        highlightColor: Colors.transparent,
        splashColor: Colors.transparent,
        hoverColor: Colors.transparent,
      ),
      title: 'Flutter Demo',
      home: DefaultTabController(
        length: 2,
        child: Scaffold(
          appBar: AppBar(
            leading: Icon(
              Icons.menu,
            ),
            automaticallyImplyLeading: false,
            backgroundColor: Colors.white,
            flexibleSpace: new Column(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                TabBar(
                  indicatorColor: Colors.pink[100],
                  tabs: [
                    Tab(text: 'Dogs'),
                    Tab(text: 'Cats'),
                  ],
                  labelColor: Colors.black,
                ), //tabbar
              ], //chilren on Tabbar
            ), //new Column
          ), //appbar
          body: TabBarView(
            children: [
              Center(child: Text('DOGS')),
              Center(child: Text('CATS')),
            ],
          ),
        ),
      ),
    );
  }
}

应用栏当前

应有应用栏,带有两个操作按钮(指示器位置需要更多工作:

但是如何在不让标签栏覆盖它们的情况下添加两个操作按钮?可以使用网格吗?

【问题讨论】:

  • 你能添加一个IMG吗?当前的 IMG 和预期是什么?
  • 是的! IMG 已添加 :)

标签: flutter material-design tabbar appbar


【解决方案1】:

尝试将标签栏放入标题中:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        highlightColor: Colors.transparent,
        splashColor: Colors.transparent,
        hoverColor: Colors.transparent,
      ),
      title: 'Flutter Demo',
      home: DefaultTabController(
        length: 2,
        child: Scaffold(
          appBar: AppBar(
            leading: Icon(
              Icons.menu,
              color: Colors.black,
            ),
            actions: [
              Icon(
                Icons.settings,
                color: Colors.black,
              ),
            ],
            automaticallyImplyLeading: false,
            backgroundColor: Colors.white,
            title: Padding(
              padding: EdgeInsets.only(left: 50, right: 50),
              child:TabBar(
              indicatorColor: Colors.pink[100],
              tabs: [
                Tab(text: 'Dogs'),
                Tab(text: 'Cats'),
              ],
              labelColor: Colors.black,
            ),),
          ), //appbar
          body: TabBarView(
            children: [
              Center(child: Text('DOGS')),
              Center(child: Text('CATS')),
            ],
          ),
        ),
      ),
    );
  }
}

【讨论】:

  • 嗨,我在 Dartpad 上试过这个,我认为这个方法有效。谢谢!还有一件事,是否可以调整标题内标签栏的宽度?还是直接修改标题的宽度?希望你能澄清一下
  • 我已经更新了答案,只是用填充包裹,如果有帮助请标记为答案~
  • 感谢吉姆的帮助!而且我刚刚注意到选项卡指示器的位置不太正确。它应该放在底部。也许用边距换行会有所帮助。
  • 是的,小调是为了完美,很高兴它有帮助!
  • 亲爱的吉姆,我刚回去尝试删除指标下方的底部空间,但没有任何运气......我看到了一个非常相似的问题stackoverflow.com/questions/59525806/…,但是对该帖子的回答并没有解决我的问题。你能再看看吗?提前致谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多