我是Flutter的新手,我在屏幕底部创建了一个新的导航栏。我想更改单击时位于的选项卡的背景。我该如何更改?我无法正常工作。我只能用“ activeIcon”来更改标签颜色或图标颜色。 @override有什么作用?
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'Neu.dart';
import 'Beliebt.dart';
import 'Profil.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MyAppState();
}
}
class MyAppState extends State<MyApp> {
int _selectedTab = 0;
final _pageOptions = [
NeuPage(),
BeliebtPage(),
ProfilPage(),
];
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: Colors.deepOrangeAccent,
primaryTextTheme: TextTheme(
title: TextStyle(color: Colors.white),
)),
home: Scaffold(
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
'assets/logo_straight.png',
fit: BoxFit.contain,
height: 32,
),
],
),
),
body: _pageOptions[_selectedTab],
bottomNavigationBar: BottomNavigationBar(
backgroundColor: Colors.deepOrangeAccent,
currentIndex: _selectedTab,
onTap: (int index) {
setState(() {
_selectedTab = index;
});
},
items: [
BottomNavigationBarItem(
icon: Icon(FontAwesomeIcons.quoteRight, color: Colors.white),
title: Text('Neu', style: TextStyle(color: Colors.white),
)
),
BottomNavigationBarItem(
icon: Icon(Icons.whatshot, color: Colors.white),
title: Text('Beliebt', style: TextStyle(color: Colors.white),
)
),
BottomNavigationBarItem(
icon: Icon(Icons.account_circle, color: Colors.white),
title: Text('Profil', style: TextStyle(color: Colors.white),
)
),
],
),
),
);
}
}
答案 0 :(得分:0)
更改为您的项目数组,如下所示:
items: [
BottomNavigationBarItem(
icon: Icon(FontAwesomeIcons.quoteRight, color: Colors.white),
title: Text(
'Neu',
style:
TextStyle(color: _selectedTab == 0 ? Colors.red : Colors.white),
)),
BottomNavigationBarItem(
icon: Icon(Icons.whatshot, color: Colors.white),
title: Text(
'Beliebt',
style:
TextStyle(color: _selectedTab == 0 ? Colors.red : Colors.white),
)),
BottomNavigationBarItem(
icon: Icon(Icons.account_circle, color: Colors.white),
title: Text(
'Profil',
style:
TextStyle(color: _selectedTab == 0 ? Colors.red : Colors.white),
)),],
答案 1 :(得分:0)
您将需要构建自己的自定义小部件。 这就是Flutter的目的。您可以根据需要构建所有内容。 通过锻炼,您将流利地做到这一点。我做了一个MVE,供您结帐。有很多事情可以做得更好,更流畅,但是我认为对于初学者来说,详细的实现是很好的。
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MyAppState();
}
}
class MyAppState extends State<MyApp> {
int _selectedTab = 0;
final _pageOptions = [
Container(child: Center(child: Text('1', style: TextStyle(color: Colors.black87),)),),
Container(child: Center(child: Text('2', style: TextStyle(color: Colors.black87),)),),
Container(child: Center(child: Text('3 ', style: TextStyle(color: Colors.black87),)),),
];
_changePage(int index) {
setState(() {
if (index >= _pageOptions.length){
return;
}
_selectedTab = index;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: Colors.deepOrangeAccent,
primaryTextTheme: TextTheme(
title: TextStyle(color: Colors.white),
)),
home: Scaffold(
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
'assets/logo_straight.png',
fit: BoxFit.contain,
height: 32,
),
],
),
),
body: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Expanded(
child: _pageOptions[_selectedTab],
),
Center(
child: Container(
color: Colors.orangeAccent,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
BottomNavItem(
iconData: Icons.add,
title: 'Add',
selected: _selectedTab == 0,
onTap: (){
_changePage(0);
},
),
BottomNavItem(
iconData: Icons.add,
title: 'Add',
selected: _selectedTab == 1,
onTap: (){
_changePage(1);
},
),
BottomNavItem(
iconData: Icons.add,
title: 'Add',
selected: _selectedTab == 2,
onTap: (){
_changePage(2);
},
)
],
),
),
)
],
),
)
),
);
}
}
class BottomNavItem extends StatelessWidget {
final IconData iconData;
final String title;
final bool selected;
final Function onTap;
BottomNavItem({this.iconData, this.title, this.selected, this.onTap});
@override
Widget build(BuildContext context) {
return Expanded(
child: Container(
child: InkWell(
child: Container(
padding: EdgeInsets.all(8.0),
color: selected ? Colors.orange : Colors.orangeAccent,
child: Column(
children: <Widget>[
Icon(iconData),
Text(title)
],
),
),
onTap: onTap,
),
),
);
}
}
答案 2 :(得分:0)
我为您提供了这个解决方案,但是您还是应该考虑在将来构建自己的小部件 在这里和示例中,我将您的物品包装在一个容器中,然后GestureDetector容器获得相同大小的物品和默认背景色,每次单击时,您更改容器颜色并重置其他物品颜色,这些颜色是您在上面声明的变量
GestureDetector(onTap => {firstItemColor = newColor , seconditem = oldColor } ,child: Container(Color: firstItemColor ,height: itemHeight , width: itemWidth,
child:BottomNavigationBarItem(
icon: Icon(Icons.whatshot, color: Colors.white),
title: Text(
'Beliebt',
style:
TextStyle(color: _selectedTab == 0 ? Colors.red : Colors.white),
)))),