首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何像TabBar一样改变IconButton的颜色?

要像TabBar一样改变IconButton的颜色,可以通过自定义IconButton的颜色属性来实现。以下是一个示例代码:

代码语言:txt
复制
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的颜色。根据当前选中的导航项,我们可以根据需要设置不同的颜色。

这只是一个示例,你可以根据自己的需求进行修改和扩展。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券