bottomNavigationBar创建应用主页面简单,但在网上没看到自己想要的:
1.导航栏icon的修改(使用自己的icon而非环境中配置的Icons库中的)
2.切换Tab时,页面的刷新导致的再次数据请求及资源浪费,体验也不好
1关于icon的更换,点就在要在inItState()方法中加载在后面代码里会有
2切换Tab时常规写法一般会这么干
final _pages = [HomePage(),Market(),Mine(),];
body: _pages[_currentIndex]
这样写每次BottomNavigationBar点击切换页面时会重新取一遍_pages,而这会导致HomePage(),Market(),Mine()这三个页面的重建,这样问题就出现了
好了,小菜鸡一个的我就不哔哔了
class MainView extends StatefulWidget{
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _MainView();
}
}
class _MainView extends BaseState<MainView>{
int _currentIndex = 0;
//final _pages = [HomePage(),Market(),Mine(),];
final itemNames = [
_Item("首页",IconConfig.icon_homepage, IconConfig.icon_homepage_unselected),
_Item("资讯", '', ''),
_Item("我的", IconConfig.icon_mine_selected, IconConfig.icon_mine_unselected),
];
List<BottomNavigationBarItem> _bottomNavItems;
@override
void initState() {
// TODO: implement initState
super.initState();
//_currentIndex = 0;
if (_bottomNavItems == null) {
_bottomNavItems = itemNames
.map((item) => BottomNavigationBarItem(
icon: item.normalIcon==''?Container(width:ScreenUtil().setWidth(46),height: ScreenUtil().setWidth(46),):Image.asset(
item.normalIcon,
width: ScreenUtil().setWidth(46),
height: ScreenUtil().setWidth(46),
),
title: Text(
item.lable,
style: TextStyle(fontSize: ScreenUtil().setSp(32)),
),
activeIcon: item.normalIcon==''?Container(width:ScreenUtil().setWidth(46),height: ScreenUtil().setWidth(46),):Image.asset(
item.activeIcon,
width: ScreenUtil().setWidth(46),
height: ScreenUtil().setWidth(46),
)))
.toList();
}
}
@override
onActivityCreate(BuildContext context) {
return WillPopScope(child: _body(),
onWillPop: (){
return _exitApp();
});
}
_body(){
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
//去扫描
HPermissions.camera().then((_cameraIsGranted) {
if (_cameraIsGranted) {
NavigatorUtil.toScannerView(context,);
} else {
HPermissions.appSetting();
}
});
},
child: getTabImageall(IconConfig.icon_big_scanner),
elevation: 0,
),
floatingActionButtonLocation:
FloatingActionButtonLocation.centerDocked,
bottomNavigationBar: BottomNavigationBar(
items: _bottomNavItems,
currentIndex: _currentIndex,
type: BottomNavigationBarType.fixed,
backgroundColor: Colors.white,
fixedColor: ColorConfig.blue,
onTap:(index){
if(index==1){
showToast('点了资讯‘);
}
if (index != _currentIndex) {
setState(() {
_currentIndex = index;
});
}
},
),
body: IndexedStack(
children: <Widget>[
HomePage(),Market(),Mine()
],
index: _currentIndex,
),
);
}
_exitApp(){
return getMain().commonDialog(context, '退出应用', '取消', '退出',barr: true,rightClick: (){
getMain().exitApp();
},changeColor: true);
}
Future<bool> _exitAppDialog(BuildContext context) {
getMain().jumpToNatFinishApp();
}
}
class _Item {
String lable, activeIcon, normalIcon;
_Item(this.lable, this.activeIcon, this.normalIcon);
}
这是代码,希望能有点用