Search code examples
flutterflutter-layoutflutter-bottomnavigation

How to get gradient bottom navigation tab in flutter?


There is a package on pub https://pub.dev/packages/gradient_bottom_navigation_bar

but this is not updated for a very long time. So, is there a way to create own custom navigation bar with a gradient effect?

something like this... enter image description here


Solution

  • All it's possible with Flutter, one option could be use a transparent background in your BottomNavigationBar and put it inside a container with a BoxDecoration, try the next:

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            body: Center(
              child: Text("Hello"),
            ),
            bottomNavigationBar: _createBottomNavigationBar(),
          ),
        );
      }
    
      Widget _createBottomNavigationBar() {
        return Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              colors: [Color(0xFF00D0E1), Color(0xFF00B3FA)],
              begin: Alignment.topLeft,
              end: Alignment.topRight,
              stops: [0.0, 0.8],
              tileMode: TileMode.clamp,
            ),
          ),
          child: BottomNavigationBar(
            currentIndex: 0,
            onTap: (index) {},
            showUnselectedLabels: false,
            backgroundColor: Colors.transparent,
            type: BottomNavigationBarType.fixed,
            elevation: 0,
            unselectedItemColor: Colors.white,
            selectedIconTheme: IconThemeData(color: Colors.white),
            items: [
              BottomNavigationBarItem(
                icon: Icon(Icons.home),
                title: Text(
                  "Home",
                  style: TextStyle(color: Colors.white),
                ),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.business),
                title: Text(
                  "Business",
                  style: TextStyle(color: Colors.white),
                ),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.school),
                title: Text(
                  "School",
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ],
          ),
        );
      }
    }