底部和垂直导航选项卡同时抖动

时间:2019-09-14 10:12:57

标签: flutter flutter-layout

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

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

1 个答案:

答案 0 :(得分:1)

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

enter image description here


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),
        ),
      ),
    );
  }
}