颤动的底部导航,其中一页包含标签

时间:2019-02-12 22:21:19

标签: dart flutter cross-platform flutter-layout

我想创建一个带有底部导航栏的脚手架和一个始终显示当前页面标题的应用栏。当我更改底部栏选项时,内容明显更改。到目前为止,经典的NavigationBar结构都可以。但是问题开始于在内容页面上应该有标签的时候。我在父母脚手架中创建了我的appbar。无论如何,是否有向父窗口小部件appBar添加选项卡?

我的AppBar + BottomNavBar页面:

class MainPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MainPageState();
  }
}
class _MainPageState extends State<MainPage> {

  int _currentPageIndex;
  List<Widget> _pages = List();

  Widget _getCurrentPage() => _pages[_currentPageIndex];

  @override
  void initState() {
    setState(() {
      _currentPageIndex = 0;

      _pages.add(BlocProvider(bloc: AgendaBloc(), child: AgendaPage()));
      _pages.add(SpeakersPage());
      _pages.add(MenuPage());
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar( title: 'Agenda'),
      body: _getCurrentPage(),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentPageIndex,
        onTap: (index){
          setState(() {
            _currentPageIndex = index;
          });
        },
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.content_paste),
            title: Text('Agenda'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.group),
            title: Text('Speakers'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.menu),
            title: Text('Menu'),
          ),
        ],
      ),
    );
  }
}

现在可以说我希望我的AgendaPage小部件在视图顶部显示标签。有没有简单,轻松的方法来做到这一点?

所需效果: Tabs page

No Tabs page

2 个答案:

答案 0 :(得分:2)

您可以使用嵌套的Scaffold小部件。在Flutter 1.1.8中有效:

// This is the outer Scaffold. No AppBar here
Scaffold(
  // Workaround for https://github.com/flutter/flutter/issues/7036
  resizeToAvoidBottomPadding: false, 
  body: _getCurrentPage(),  // AppBar comes from the Scaffold of the current page 
  bottomNavigationBar: BottomNavigationBar(
    // ...
  )
)

答案 1 :(得分:0)

据我所知,但是我设法通过在通过AppBar导航的每个页面中添加单独的bottomNavigationBar来解决此问题:

为每个_pages提供一个单独的应用栏,然后从build方法中删除主要的应用栏。