添加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
相关产品推荐
相关产品推荐

