Dart 颤振中的导航条样式
我正在试用Flatter,我正在尝试更改应用程序上Dart 颤振中的导航条样式,dart,flutter,bottomnavigationview,Dart,Flutter,Bottomnavigationview,我正在试用Flatter,我正在尝试更改应用程序上BottomNavigationBar的颜色,但我所能做的就是更改BottomNavigationItem(图标和文本)的颜色 下面是我声明我的底部导航栏的位置: class _BottomNavigationState extends State<BottomNavigationHolder>{ @override Widget build(BuildContext context) { return new Sc
BottomNavigationBar
的颜色,但我所能做的就是更改BottomNavigationItem
(图标和文本)的颜色
下面是我声明我的底部导航栏的位置:
class _BottomNavigationState extends State<BottomNavigationHolder>{
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: null,
body: pages(),
bottomNavigationBar:new BottomNavigationBar(
items: <BottomNavigationBarItem>[
new BottomNavigationBarItem(
icon: const Icon(Icons.home),
title: new Text("Home")
),
new BottomNavigationBarItem(
icon: const Icon(Icons.work),
title: new Text("Self Help")
),
new BottomNavigationBarItem(
icon: const Icon(Icons.face),
title: new Text("Profile")
)
],
currentIndex: index,
onTap: (int i){setState((){index = i;});},
fixedColor: Colors.white,
),
);
}
没有选项可以指定BottomNavigationBar
的背景色,但可以更改canvasColor
。一种不破坏整个应用程序的方法是将底部导航栏
包装在主题
中,使用所需的画布颜色
例如:
bottomNavigationBar: new Theme(
data: Theme.of(context).copyWith(
// sets the background color of the `BottomNavigationBar`
canvasColor: Colors.green,
// sets the active color of the `BottomNavigationBar` if `Brightness` is light
primaryColor: Colors.red,
textTheme: Theme
.of(context)
.textTheme
.copyWith(caption: new TextStyle(color: Colors.yellow))), // sets the inactive color of the `BottomNavigationBar`
child: new BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: 0,
items: [
new BottomNavigationBarItem(
icon: new Icon(Icons.add),
title: new Text("Add"),
),
new BottomNavigationBarItem(
icon: new Icon(Icons.delete),
title: new Text("Delete"),
)
],
),
),
@override
Widget build(BuildContext context) {
return Scaffold(
body: pages(),
bottomNavigationBar:new Container(
color: Colors.green,
child: BottomNavigationBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: const Icon(Icons.home),
title: Text("Home")
),
BottomNavigationBarItem(
icon: const Icon(Icons.work),
title: Text("Self Help")
),
BottomNavigationBarItem(
icon: const Icon(Icons.face),
title: Text("Profile")
)
],
currentIndex: index,
onTap: (int i){setState((){index = i;});},
fixedColor: Colors.white,
),
);
);
};
希望有帮助 尝试将底部导航栏
包装在容器
中,然后设置其颜色
例如:
bottomNavigationBar: new Theme(
data: Theme.of(context).copyWith(
// sets the background color of the `BottomNavigationBar`
canvasColor: Colors.green,
// sets the active color of the `BottomNavigationBar` if `Brightness` is light
primaryColor: Colors.red,
textTheme: Theme
.of(context)
.textTheme
.copyWith(caption: new TextStyle(color: Colors.yellow))), // sets the inactive color of the `BottomNavigationBar`
child: new BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: 0,
items: [
new BottomNavigationBarItem(
icon: new Icon(Icons.add),
title: new Text("Add"),
),
new BottomNavigationBarItem(
icon: new Icon(Icons.delete),
title: new Text("Delete"),
)
],
),
),
@override
Widget build(BuildContext context) {
return Scaffold(
body: pages(),
bottomNavigationBar:new Container(
color: Colors.green,
child: BottomNavigationBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: const Icon(Icons.home),
title: Text("Home")
),
BottomNavigationBarItem(
icon: const Icon(Icons.work),
title: Text("Self Help")
),
BottomNavigationBarItem(
icon: const Icon(Icons.face),
title: Text("Profile")
)
],
currentIndex: index,
onTap: (int i){setState((){index = i;});},
fixedColor: Colors.white,
),
);
);
};
@覆盖
小部件构建(构建上下文){
返回脚手架(
正文:pages(),
bottomNavigationBar:新容器(
颜色:颜色。绿色,
子项:底部导航栏(
项目:[
底部导航气压计(
图标:常量图标(Icons.home),
标题:文本(“主页”)
),
底部导航气压计(
图标:常量图标(Icons.work),
标题:文本(“自助”)
),
底部导航气压计(
图标:常量图标(Icons.face),
标题:文本(“简介”)
)
],
当前索引:索引,
onTap:(inti){setState((){index=i;});},
固定颜色:颜色。白色,
),
);
);
};
只需将backgroundColor
属性添加到BottomNavigationBar
小部件
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: null,
body: pages(),
bottomNavigationBar:new BottomNavigationBar(
items: <BottomNavigationBarItem>[
new BottomNavigationBarItem(
icon: const Icon(Icons.home),
title: new Text("Home")
),
new BottomNavigationBarItem(
icon: const Icon(Icons.work),
title: new Text("Self Help")
),
new BottomNavigationBarItem(
icon: const Icon(Icons.face),
title: new Text("Profile")
)
],
currentIndex: index,
onTap: (int i){setState((){index = i;});},
fixedColor: Colors.white,
backgroundColor: Colors.black45,
),
);
}
@覆盖
小部件构建(构建上下文){
归还新脚手架(
appBar:null,
正文:pages(),
bottomNavigationBar:新的bottomNavigationBar(
项目:[
新海底导航气压计(
图标:常量图标(Icons.home),
标题:新文本(“主页”)
),
新海底导航气压计(
图标:常量图标(Icons.work),
标题:新文本(“自助”)
),
新海底导航气压计(
图标:常量图标(Icons.face),
标题:新文本(“简介”)
)
],
当前索引:索引,
onTap:(inti){setState((){index=i;});},
固定颜色:颜色。白色,
背景颜色:Colors.black45,
),
);
}
公认的答案并非完全错误。但是,BottomNavigationBar
实际上具有backgroundColor
属性。根据文件
如果类型为BottomNavigationBarType.shifting,并且Items设置了BottomNavigationBarItem.backgroundColor,则项目的backgroundColor将闪烁并覆盖此颜色
这意味着BottomNavigation
的backgroundColor将被单个项目的backgroundColor覆盖,因为默认类型是BottomNavigationBarType.shift
要解决此问题,只需将以下属性添加到声明的BottomNavigationbar
小部件中
type: BottomNavigationBarType.fixed,
注意:但是,如果您想要改变效果,您必须为每个项目声明颜色,或者包装允许覆盖子小部件背景颜色的小部件
i、 例如容器
小部件。
type: BottomNavigationBarType.fixed,
以前没有直接设置颜色的方法,但现在可以使用
BottomNavigationBar(
backgroundColor: Colors.red,
selectedItemColor: Colors.black,
unselectedItemColor: Colors.white,
...
)
如果类型是固定的,可以通过将颜色设置为backgroundColor属性进行更改
BottomNavigationBar(
backgroundColor: Colors.red,
type: BottomNavigationBarType.fixed,
items: [
BottomNavigationBarItem(
icon:Icon(Icons.home, color: Color.fromARGB(255, 255, 255, 255)),
title: new Text('Home'),),
BottomNavigationBarItem(
icon: Icon(Icons.work,color: Color.fromARGB(255, 255, 255, 255)),
title: new Text('Self Help'),),
BottomNavigationBarItem(
icon:Icon(Icons.face, color: Color.fromARGB(255, 255, 255, 255)),
title: new Text('Profile'),),
]
)
如果类型正在移动,它将在bottomNavigationBarItem中使用颜色
BottomNavigationBar(
backgroundColor: Colors.red,
type: BottomNavigationBarType.shifting,
items: [
BottomNavigationBarItem(
icon:Icon(Icons.home, color: Color.fromARGB(255, 255, 255, 255)),
title: new Text('Home'),
backgroundColor: Colors.red),
BottomNavigationBarItem(
icon: Icon(Icons.work,color: Color.fromARGB(255, 255, 255, 255)),
title: new Text('Self Help'),
backgroundColor: Colors.blue),
BottomNavigationBarItem(
icon:Icon(Icons.face, color: Color.fromARGB(255, 255, 255, 255)),
title: new Text('Profile'),
backgroundColor: Colors.amber),
]
)
您可以看到,即使我设置了backgroundColor属性,它也不会应用该颜色,而BottomNavigationBarItem小部件中的背景色将覆盖该属性
从中找到设置以下属性以更改背景、选定的和未选定的颜色
bottomNavigationBar: BottomNavigationBar(
backgroundColor: Colors.blue,
selectedItemColor: Colors.black,
unselectedItemColor: Colors.white,
type: BottomNavigationBarType.fixed,
...
)
您当前可以直接从主题
设置它们的样式BottomNavigationBar
,如下所示:
ThemeData(
bottomNavigationBarTheme: BottomNavigationBarThemeData(
backgroundColor: Colors.grey[900],
elevation: 10,
selectedLabelStyle: TextStyle(
color: Color(0xFFA67926), fontFamily: 'Montserrat', fontSize: 14.0),
unselectedLabelStyle: TextStyle(
color: Colors.grey[600], fontFamily: 'Montserrat', fontSize: 12.0),
selectedItemColor: Color(0xFFA67926),
unselectedItemColor: Colors.grey[600],
showUnselectedLabels: true,
),
)不推荐使用标题
。我们使用标签
对于label
,我们可以使用相应的属性:selectedLabelStyle,unselectedLabelStyle
例如:
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
selectedItemColor: Theme.of(context).accentColor,
selectedFontSize: 0,
unselectedFontSize: 0,
iconSize: 22,
elevation: 0,
backgroundColor: Colors.transparent,
selectedIconTheme: IconThemeData(size: 28),
unselectedItemColor: Theme.of(context).focusColor.withOpacity(1),
selectedLabelStyle: Theme.of(context).textTheme.bodyText1.merge(TextStyle(fontSize: 12)),
unselectedLabelStyle: Theme.of(context).textTheme.button.merge(TextStyle(fontSize: 11)),
showUnselectedLabels: true,
currentIndex: widget.currentTabIdx,
onTap: (int i) {
this._selectTab(i);
},
showSelectedLabels: true,
// this will be set when a new tab is tapped
items: [
BottomNavigationBarItem(
icon: SvgPicture.asset(IMAGE_ASSETS_ICONS_HOME) ,
activeIcon: SvgPicture.asset(IMAGE_ASSETS_ICONS_HOME, color: Theme.of(context).accentColor),
label: 'Home',
),
BottomNavigationBarItem(
label: 'Categories',
icon: SvgPicture.asset(IMAGE_ASSETS_ICONS_CATEGORY),
activeIcon: SvgPicture.asset(IMAGE_ASSETS_ICONS_CATEGORY, color: Theme.of(context).accentColor) ,
),
BottomNavigationBarItem(
icon: SvgPicture.asset(IMAGE_ASSETS_ICONS_ORDER_HISTORY, ) ,
activeIcon: SvgPicture.asset(IMAGE_ASSETS_ICONS_ORDER_HISTORY, color: Theme.of(context).accentColor ) ,
label: 'Order History',
),
BottomNavigationBarItem(
icon: SvgPicture.asset(IMAGE_ASSETS_ICONS_CART,) ,
activeIcon: SvgPicture.asset(IMAGE_ASSETS_ICONS_CART, color: Theme.of(context).accentColor) ,
label: 'Cart',
),
],
只需按照下面给出的代码根据您的需求进行定制。您只需使用主题设置NavigationBar的父级,并设置canvasColor即可更改背景色
bottomNavigationBar: Theme(
data: Theme.of(context).copyWith(
canvasColor: kOrangeMaterialColor
),
child: BottomNavigationBar(
type: BottomNavigationBarType.shifting,
currentIndex: _currentIndex,
onTap: _onTapItem,
items: [
BottomNavigationBarItem(icon: Icon(Icons.home,
color: kWhiteColor,),
label: ''),
BottomNavigationBarItem(icon: Icon(Icons.notifications,
color: kWhiteColor,),
label: ''),
// BottomNavigationBarItem(icon: Icon(Icons.favorite_border,
// color: kWhiteColor,),
// label: ''),
BottomNavigationBarItem(icon: Icon(Icons.account_circle,
color: kWhiteColor,),
label: ''),
BottomNavigationBarItem(icon: Icon(Icons.settings,
color: kWhiteColor,),
label: ''),
],
),
),
我还想更改bottomnav的大部分内容,结果只是复制了bottomnav的所有代码并更改了我想要的内容。我想这是颤振的主要好处之一,它的开放性和易用性。我做了这个:非常感谢你,好先生。工作得很好。您知道如何更改底部导航栏上非活动图标的颜色吗?ThemeData
有一个disabledColor
属性,请尝试设置它。我实际上已尝试设置ThemeData
下的所有可用属性以更改非活动图标的颜色,但没有一个属性起作用。即使是disabledColor
实际上,禁用的颜色也是从textTheme
的caption
中提取的。我已经更新了我的答案,以表明你如何能做到这一点。希望有帮助!你是最棒的!非常感谢你。。老实说,我怀疑我自己会明白这一点!非常感谢你!!如果只是将appBar设置为空,则根本不要设置它。它可能有一个默认值i已更改type:BottomNavigationBarType。通过type:BottomNavigationBarType.fixed
进行移位。这对我来说很有效,thanksi遇到了一个问题,当我添加4个孩子时,背景颜色会自动变为白色。将导航栏类型设置为“固定”后,它将自动停止更改。类型:BottomNavigationBarType.fixed,此属性为