You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加resizeToAvoidBottomInset:false后底部导航栏仍贴键盘顶部的解决方法

resizeToAvoidBottomInset: false只控制Scaffold的body区域是否避开键盘,不会影响bottomNavigationBar的位置——系统默认会调整底部栏的位置以避开键盘,所以你设置这个属性没用。

给你两个靠谱的修复方案:


方案一:用Stack固定导航栏到屏幕底部

把BottomNavigationBar从Scaffold的bottomNavigationBar属性移到body里的Stack中,通过Positioned强制固定在屏幕物理底部,彻底不受键盘影响。

修改后的完整代码:

@override
Widget build(BuildContext context) {
  return Scaffold(
    resizeToAvoidBottomInset: false,
    key: _scaffoldKey,
    body: Stack(
      fit: StackFit.expand,
      children: [
        // 这里放置你的主内容区域(原来的body内容)
        // 示例:Center(child: Text("页面主内容")),
        // 固定底部导航栏到屏幕底部
        Positioned(
          left: 0,
          right: 0,
          bottom: 0,
          child: Theme(
            data: ThemeData(
              canvasColor: Color.fromARGB(255, 0, 0, 0),
            ),
            child: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              iconSize: 30,
              items: <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                  icon: Text(
                    currentDay.toString(),
                    style: TextStyle(
                      fontSize: 22,
                      fontWeight: FontWeight.w700,
                      color: _currentIndex == 0 ? Colors.blue : Colors.white,
                    ),
                  ),
                  label: '',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.folder),
                  label: '',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.add),
                  label: '',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.list),
                  label: '',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.person),
                  label: '',
                ),
              ],
              selectedItemColor: Colors.blue,
              unselectedItemColor: Colors.white,
              currentIndex: _currentIndex,
              onTap: (int index) {
                setState(() {
                  _currentIndex = index;
                });
              },
            ),
          ),
        ),
      ],
    ),
  );
}

方案二:通过MediaQuery移除底部Insets影响

如果不想调整整体布局,可以用MediaQuery.removePadding包裹导航栏,强制忽略键盘带来的底部Insets:

@override
Widget build(BuildContext context) {
  return Scaffold(
    resizeToAvoidBottomInset: false,
    key: _scaffoldKey,
    bottomNavigationBar: MediaQuery.removePadding(
      context: context,
      removeBottom: true,
      child: Theme(
        data: ThemeData(
          canvasColor: Color.fromARGB(255, 0, 0, 0),
        ),
        child: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          iconSize: 30,
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Text(
                currentDay.toString(),
                style: TextStyle(
                  fontSize: 22,
                  fontWeight: FontWeight.w700,
                  color: _currentIndex == 0 ? Colors.blue : Colors.white,
                ),
              ),
              label: '',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.folder),
              label: '',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.add),
              label: '',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.list),
              label: '',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.person),
              label: '',
            ),
          ],
          selectedItemColor: Colors.blue,
          unselectedItemColor: Colors.white,
          currentIndex: _currentIndex,
          onTap: (int index) {
            setState(() {
              _currentIndex = index;
            });
          },
        ),
      ),
    ),
  );
}

方案一更稳定,直接将导航栏固定在屏幕底部,不受任何Insets变化影响;方案二更简洁,适合不想调整整体布局的场景。

内容的提问来源于stack exchange,提问作者RAHUL THAKRANI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 11:21:34