【问题标题】:Flutter- add drawer into bottomNavigationBarItem with other items and navigateFlutter-将抽屉与其他项目一起添加到bottomNavigationBarItem并导航
【发布时间】:2021-12-28 11:32:06
【问题描述】:

一般情况下,我们在appBar中使用drawer(位置drawer/endDrawer)。我必须将抽屉功能设置为带有主页、设置的底部导航栏项目。这 3 个项目将适用于所有页面,并且必须显示这三 (3) 个项目的选定索引。我怎样才能做到这一点。

class BottomNavBar extends StatefulWidget {

  @override
  _BottomNavBarState createState() => _BottomNavBarState();
}

class _BottomNavBarState extends State<BottomNavBar> {
  int _selectedIndex = 1;

  @override
  Widget build(BuildContext context) {
    return BottomNavigationBar(
      currentIndex: _selectedIndex,
      onTap: onTabTapped,
      //for more than three items
      type: BottomNavigationBarType.fixed,
      items: [
        BottomNavigationBarItem(
          icon: Icon(processIcon),
          label: 'My Task',
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.home),
          label: 'Home',
        ),
        BottomNavigationBarItem(
            icon: Icon(settingIcon),
            label: 'Setting',
        ),
        BottomNavigationBarItem(
          icon: Icon(menuIcon),
          label: 'Menu',
        ),
      ],
    );
  }
  void onTabTapped(int index) {
    setState(() {
      _selectedIndex = index;
      print('index ${index}');
    });

  }
}

在这里,抽屉将被导航到菜单中。

【问题讨论】:

  • 你能详细描述一下这个问题吗?我不太明白
  • 主要我需要通过单击底部导航栏项目上的菜单按钮来打开和关闭抽屉。下一部分是假设我想从我的应用程序的任何页面进入设置页面,当我单击并进入设置页面时,设置将显示为已选择

标签: android ios flutter dart flutter-layout


【解决方案1】:

您可以在Scaffold 上使用GlobalKey 并使用它来打开抽屉。

 void onTabTapped(int index) {
    setState(() {
      _selectedIndex = index;

      if (_selectedIndex == 3 && _scaffoldKey.currentState != null) {
        _scaffoldKey.currentState!.openDrawer();
      } });
  }
class _BottomNavBarState extends State<BottomNavBar> {
  int _selectedIndex = 1;
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      body: Text("B $_selectedIndex"),
      drawer: const Drawer(
        backgroundColor: Colors.cyanAccent,

【讨论】:

  • 如何设置抽屉从右到左的过渡?
  • Directionality(textDirection:TextDirection.rtl,child:Drawer())包装
  • 我不想更改文本方向。当我们在 endDrawer 中使用 Drawer() 时,只想像从右到左一样打开 Drawer。
  • 基本上就是会改变inner child的方向性,可以查看this
  • 这项工作。但是当我使用 endDrawer 时,汉堡包同时在 appBar 的右侧可见,而我想通过单击菜单来打开它。如何从 appBar 右侧移除这个汉堡包图标?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多