Flutter 在 BottomNavigationBar 中为 selectedItemColor 添加渐变

问题描述 投票:0回答:1

我希望能够在 selectedItemColor 字段中添加渐变。

bottomNavigationBar: BottomNavigationBar(
        items: const [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
          BottomNavigationBarItem(
              icon: Icon(Icons.favorite), label: 'Favorite'),
          BottomNavigationBarItem(icon: Icon(Icons.settings), label: 'Setting'),
          BottomNavigationBarItem(icon: Icon(Icons.abc), label: 'idk'),
        ],
        selectedItemColor: Colors.black,
        unselectedItemColor: Theme.of(context).colorScheme.onBackground,
        showUnselectedLabels: true,
      ),
flutter colors gradient flutter-bottomnavigation
1个回答
0
投票

您可以使用ShaderMask

class GradientIcon extends StatelessWidget {
  const GradientIcon({
    super.key,
    required this.icon,
    required this.isSelected,
  });

  final IconData icon;
  final bool isSelected;

  @override
  Widget build(BuildContext context) {
    return ShaderMask(
      shaderCallback: (Rect bounds) {
        return RadialGradient(
          center: Alignment.topLeft,
          radius: 1.0,
          colors: isSelected
              ? [
                  Colors.yellow,
                  Colors.deepOrange.shade900,
                ]
              : [
                  Colors.black,
                  Colors.black,
                ],
          tileMode: TileMode.mirror,
        ).createShader(bounds);
      },
      child: Icon(
        icon,
        color: Colors.white,
      ),
    );
  }
}


BottomNavigationBar(
      items: const [
        BottomNavigationBarItem(
          icon: GradientIcon(
            icon: Icons.home,
            isSelected: true,
          ),
          label: 'Home',
        ),
        BottomNavigationBarItem(
          icon: GradientIcon(
            icon: Icons.favorite,
            isSelected: false,
          ),
          label: 'Favorite',
        ),
        BottomNavigationBarItem(
          icon: GradientIcon(
            icon: Icons.settings,
            isSelected: false,
          ),
          label: 'Setting',
        ),
        BottomNavigationBarItem(
          icon: GradientIcon(
            icon: Icons.abc,
            isSelected: false,
          ),
          label: 'idk',
        ),
      ],
    )
© www.soinside.com 2019 - 2024. All rights reserved.