【问题标题】:Flutter bottom and vertical navigation tabs at the same time同时颤动底部和垂直导航选项卡
【发布时间】:2020-01-15 23:19:03
【问题描述】:

我想使用 Flutter 创建一个带有垂直和底部导航选项卡的屏幕,我测试了 2 种方法:

  • 首先是制作底部和顶部选项卡,然后旋转顶部使其垂直,结果是垂直选项卡填满了整个屏幕。
  • 第二个是通过 vertical_tabs 包使用底部导航,在这种情况下,我无法使用底部标签进行导航

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    这是执行此操作的一种方法。我不确定这是否是最好的方法,但它有效。在底部,我使用的是 BottomNavigationBar,而对于垂直我使用的是 TabBar(您可以选择其他任何内容或您自己的自定义视图)。

    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
      var _bottomBarIndex = 0;
      String title = "Screen 0";
      TabController _tabController;
    
      @override
      void initState() {
        _tabController = TabController(initialIndex: 0, length: 3, vsync: this);
        _tabController.addListener(() {
          switch (_tabController.index) {
            case 0:
              {
                setState(() {
                  title = "Profile";
                });
                break;
              }
            case 1:
              {
                setState(() {
                  title = "Done";
                });
                break;
              }
            case 2:
              {
                setState(() {
                  title = "DashBoard";
                });
                break;
              }
          }
        });
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Row(
            children: <Widget>[
              RotatedBox(
                quarterTurns: 1,
                child: TabBar(
                  controller: _tabController,
                  tabs: <Widget>[
                    getItem(
                      icon: Icon(
                        Icons.person,
                        color: Colors.black,
                      ),
                      text: Text(
                        "Profile",
                        style: TextStyle(color: Colors.black),
                      ),
                    ),
                    getItem(
                      icon: Icon(
                        Icons.done,
                        color: Colors.black,
                      ),
                      text: Text(
                        "Done",
                        style: TextStyle(color: Colors.black),
                      ),
                    ),
                    getItem(
                      icon: Icon(
                        Icons.dashboard,
                        color: Colors.black,
                      ),
                      text: Text(
                        "Dashboard",
                        style: TextStyle(color: Colors.black),
                      ),
                    ),
                  ],
                ),
              ),
              Expanded(
                child: NewScreen(
                  title: title,
                ),
              )
            ],
          ),
          bottomNavigationBar: BottomNavigationBar(
            currentIndex: _bottomBarIndex,
            onTap: (index) {
              setState(() {
                _bottomBarIndex = index;
                title = "Screen $index";
              });
            },
            items: [
              BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('Home')),
              BottomNavigationBarItem(icon: Icon(Icons.add), title: Text('Add')),
              BottomNavigationBarItem(
                  icon: Icon(Icons.search), title: Text('Search')),
            ],
          ),
        );
      }
    
      Widget getItem({Icon icon, Text text}) {
        return RotatedBox(
          quarterTurns: -1,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[icon, text],
          ),
        );
      }
    }
    
    class NewScreen extends StatelessWidget {
      const NewScreen({this.title});
    
      final String title;
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: Text(
              title,
              style: TextStyle(color: Colors.black, fontSize: 20.0),
            ),
          ),
        );
      }
    }
    
    

    【讨论】:

    • 当我从垂直标签中选择一个标签然后我从底部选择一个时,第一个不会被取消选择!我该如何解决?
    • 不幸的是,无法将标签栏项目设置为未选中。一次,应将其中一个设置为选中状态。使用一些不同的小部件或自定义小部件。但是有一种解决方法。当您从底部选择时,将 TabBar 的 indicatorColor 更改为白色,使其看起来像未选中。
    • 好的,谢谢你的说明,我还有一个问题,我可以将垂直标签从屏幕左侧更改为右侧吗?
    • 是的,这很容易。只需将RotatedBox 小部件放在Expanded 小部件之后即可。
    • 它可以工作,但是在 Chrome 和 iPhone 上的文字和图标模糊了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-15
    相关资源
    最近更新 更多