要像TabBar一样改变IconButton的颜色,可以通过自定义IconButton的颜色属性来实现。以下是一个示例代码:
import 'package:flutter/material.dart';
class CustomTabBar extends StatefulWidget {
@override
_CustomTabBarState createState() => _CustomTabBarState();
}
class _CustomTabBarState extends State<CustomTabBar> {
int _selectedIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Custom Tab Bar'),
),
body: Center(
child: Text('Content of tab ${_selectedIndex + 1}'),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _selectedIndex,
onTap: (index) {
setState(() {
_selectedIndex = index;
});
},
items: [
BottomNavigationBarItem(
icon: IconButton(
icon: Icon(Icons.home),
color: _selectedIndex == 0 ? Colors.blue : Colors.grey,
onPressed: () {},
),
label: 'Home',
),
BottomNavigationBarItem(
icon: IconButton(
icon: Icon(Icons.search),
color: _selectedIndex == 1 ? Colors.blue : Colors.grey,
onPressed: () {},
),
label: 'Search',
),
BottomNavigationBarItem(
icon: IconButton(
icon: Icon(Icons.person),
color: _selectedIndex == 2 ? Colors.blue : Colors.grey,
onPressed: () {},
),
label: 'Profile',
),
],
),
);
}
}
void main() {
runApp(MaterialApp(
home: CustomTabBar(),
));
}
在上述代码中,我们使用了BottomNavigationBar
作为底部导航栏,每个导航项都是一个BottomNavigationBarItem
,其中的icon
属性是一个IconButton
,通过设置color
属性来改变IconButton
的颜色。根据当前选中的导航项,我们可以根据需要设置不同的颜色。
这只是一个示例,你可以根据自己的需求进行修改和扩展。
领取专属 10元无门槛券
手把手带您无忧上云