Flutter:如何让BottomNavigationBar子Container高度与导航栏默认高度一致?
你可以直接使用Flutter Material库提供的kBottomNavigationBarHeight常量来设置Container的高度,这个常量就是BottomNavigationBar的默认高度值(不同平台会自动适配对应的标准高度)。
修改要点:
- 确保已导入
package:flutter/material.dart(Flutter项目默认已包含该导入) - 将Container的
height: 100替换为height: kBottomNavigationBarHeight - 调整内部Image的高度,避免因padding导致内容溢出容器(比如设置为
kBottomNavigationBarHeight - 20,抵消上下各10的padding)
修改后的完整代码:
bottomNavigationBar: Container( padding: const EdgeInsets.all(10), alignment: Alignment.bottomCenter, width: MediaQuery.of(context).size.width, height: kBottomNavigationBarHeight, // 使用默认BottomNavigationBar高度常量 decoration: BoxDecoration( color: Colors.yellow, border: Border.all( color: Colors.black, // 设置边框颜色 width: 3.0), // 设置边框宽度 borderRadius: const BorderRadius.all( Radius.circular(10.0)), // 设置圆角半径 boxShadow: const [BoxShadow(blurRadius: 10,color: Colors.black,offset: Offset(1,3))] // 添加阴影效果 ), child: Row( mainAxisAlignment: MainAxisAlignment.center, children: const <Widget>[ Image( image: AssetImage('images/company_logo.png'), height: kBottomNavigationBarHeight - 20, // 适配padding后的高度 ), SizedBox( width: 15, ), Text("DEVELOPED BY COMPANY_NAME",style: TextStyle(fontWeight: FontWeight.bold, fontFamily: 'Lobster')) ] ) )
补充说明:kBottomNavigationBarHeight会自动适配iOS和Android平台的BottomNavigationBar标准高度,无需手动区分平台做适配。
内容的提问来源于stack exchange,提问作者user20415294
相关产品推荐
相关产品推荐

