在我的底部导航栏中,我有五个图标,其中四个图标打开4个不同的屏幕,我想要另一个图标打开showModalBottomSheet个,但当我实现它并运行应用程序时,它显示错误消息

在生成过程中调用setState()或markNeedsBuild().和‘Package:Ffltter/src/Widget/Navigator.dart’:失败的断言:行 2903位置18:‘!Navigator._DebugLocked’:不为真.

我也试过使用WidgetsBinding.instance?.addPostFrameCallback((_),但它也显示错误提示为'This expression has a type of 'void' so its value can't be used.' 我该怎么办??

class BottomNavigationPage extends StatefulWidget {
  const BottomNavigationPage({Key? key}) : super(key: key);

  @override
  State<BottomNavigationPage> createState() => _BottomNavigationPageState();
}

class _BottomNavigationPageState extends State<BottomNavigationPage> {
  int currentIndex = 1;

  @override
  Widget build(BuildContext context) {
    List body = [
      showModalBottomSheet(
          context: context,
          builder: (BuildContext context) {
            return const Containers(
              height: 400,
              color: Colors.green,
            );
          }),
      const Containers(
        height: 400,
        color: Colors.red,
      ),
      const Dashboard(),
      const DepartmentPage(),
      const Dashboard(),
      const DepartmentPage(),
    ];
    return Scaffold(
      body: Center(
        child: body[currentIndex],
      ),
      bottomNavigationBar: BottomNavigationBar(
        //showUnselectedLabels: true,
        selectedItemColor: AppColor.btnColor,
        selectedIconTheme: const IconThemeData(color: AppColor.btnColor),
        unselectedItemColor: Colors.black45,
        showSelectedLabels: true,
        currentIndex: currentIndex,
        onTap: (int newindex) {
          setState(() {
            currentIndex = newindex;
          });
        },
        items: const [
          BottomNavigationBarItem(
            label: '',
            activeIcon: Icon(
              Icons.menu,
              color: AppColor.btnColor,
              size: 30,
            ),
            icon: Icon(
              Icons.menu,
              size: 30,
            ),
          ),
          BottomNavigationBarItem(
            activeIcon: Icon(
              Icons.home,
              size: 30,
            ),
            label: 'होम',
            icon: Icon(
              Icons.home,
              size: 30,
            ),
          ),
          BottomNavigationBarItem(
            activeIcon: Icon(
              Icons.people,
              size: 30,
            ),
            label: 'शाखा हरु ',
            icon: Icon(
              Icons.people,
              size: 30,
            ),
          ),
          BottomNavigationBarItem(
            label: 'सूचना हरु',
            activeIcon: Icon(
              Icons.task,
              size: 30,
            ),
            icon: Icon(
              Icons.task,
              size: 30,
            ),
          ),
          BottomNavigationBarItem(
            activeIcon: Icon(
              Icons.task,
              size: 30,
            ),
            label: 'सूचना हरु',
            icon: Icon(
              Icons.task,
              size: 30,
            ),
          ),
        ],
      ),
    );
  }
}

推荐答案

你不必把showModalBottomSheet加到body的名单上.您可以在点击BottomNavigationBarItem时判断是否为newindex == 0,然后显示它.尝尝这个

class BottomNavigationPage extends StatefulWidget {
  const BottomNavigationPage({Key? key}) : super(key: key);

  @override
  State<BottomNavigationPage> createState() => _BottomNavigationPageState();
}

class _BottomNavigationPageState extends State<BottomNavigationPage> {
  int currentIndex = 1;

  @override
  Widget build(BuildContext context) {
    List body = [
      const Containers(
        height: 400,
        color: Colors.red,
      ),
      const Dashboard(),
      const DepartmentPage(),
      const Dashboard(),
      const DepartmentPage(),
    ];
    return Scaffold(
      body: Center(
        child: body[currentIndex-1],
      ),
      bottomNavigationBar: BottomNavigationBar(
        //showUnselectedLabels: true,
        selectedItemColor: AppColor.btnColor,
        selectedIconTheme: const IconThemeData(color: AppColor.btnColor),
        unselectedItemColor: Colors.black45,
        showSelectedLabels: true,
        currentIndex: currentIndex,
        onTap: (int newindex) {
          if (newindex == 0) {
            showModalBottomSheet(
              context: context,
              builder: (BuildContext context) {
                return const Containers(
                  height: 400,
                  color: Colors.green,
                );
              }
            );
          } else {
            setState(() {
              currentIndex = newindex;
            });
          }
        },
        items: const [
          BottomNavigationBarItem(
            label: '',
            activeIcon: Icon(
              Icons.menu,
              color: AppColor.btnColor,
              size: 30,
            ),
            icon: Icon(
              Icons.menu,
              size: 30,
            ),
          ),
          BottomNavigationBarItem(
            activeIcon: Icon(
              Icons.home,
              size: 30,
            ),
            label: 'होम',
            icon: Icon(
              Icons.home,
              size: 30,
            ),
          ),
          BottomNavigationBarItem(
            activeIcon: Icon(
              Icons.people,
              size: 30,
            ),
            label: 'शाखा हरु ',
            icon: Icon(
              Icons.people,
              size: 30,
            ),
          ),
          BottomNavigationBarItem(
            label: 'सूचना हरु',
            activeIcon: Icon(
              Icons.task,
              size: 30,
            ),
            icon: Icon(
              Icons.task,
              size: 30,
            ),
          ),
          BottomNavigationBarItem(
            activeIcon: Icon(
              Icons.task,
              size: 30,
            ),
            label: 'सूचना हरु',
            icon: Icon(
              Icons.task,
              size: 30,
            ),
          ),
        ],
      ),
    );
  }
}

Flutter相关问答推荐

如何增加容器高度?

blocTest错误:没有从`when()`中调用方法存根

什么是dart :这只是dart ?

分页控制器在到达页面末尾之前或在初始化页面时对每个索引调用API

如何在抖动中将PNG图像转换为WebP

在flutter web上渲染多个Youtube视频

在带有 flutter 的 Native Android 中,EventChannel.EventSink 始终为 null

Flutter 使用扩展的 TextField 定位小部件

如果有空间则居中CustomScrollView

如何在Flutter中动态绘制一条从A点到B点的线?

启动画面中的淡入淡出图像

如何从另一个页面访问 Firebase 值?

如何在两个不同的路由(屏幕)之间正确设置 BlocProvider?

flutter - 无法解析 JSON 消息:文档为空

Flutter Serverpod 在示例项目上返回 400 错误

如何在 Flutter 中将列小部件添加到行小部件?

仅使用 Flutter 在本地环境中托管 Web 服务器

如何使按钮被选中?

setState 不会在第一次更新 - Flutter

带有 Dismissible 和 Provider (NotifyListener) 的笨拙动画