Prashant Mishra
Prashant Mishra

Reputation: 319

Flutter bottom nav bar

I was experimenting with bottom navigation bar in flutter. Again, very new to flutter. Can I use custom svg icons instead of the icons provided by flutter's material in BottomNavigationBarItem. It would be amazing if you could help me out with a code snippet.

I have this type of navbar I am working on right now.enter image description here. I have these custom icons but I don't know how to use them.

Upvotes: 3

Views: 9077

Answers (2)

Suri
Suri

Reputation: 713

Use a package to use svg, instruction is here: https://pub.dev/packages/flutter_svg#-installing-tab-

then include the svg file to your bottom navigation bar item in the icon parameter

bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(
            icon: SvgPicture.asset(/*path to the svg file*/),
            title: Text("Browse")
          ),
]

Upvotes: 10

chunhunghan
chunhunghan

Reputation: 54377

You can copy paste run full code below
You can use package https://pub.dev/packages/flutter_svg
and wrap with Container to provide width and height
sample svg file https://github.com/dnfield/flutter_svg/blob/master/example/assets/wikimedia/Ghostscript_Tiger.svg

code snippet

BottomNavigationBarItem(
        icon: Container(
          width: 30,
          height: 30,
          child: SvgPicture.asset(
            "assets/wikimedia/Ghostscript_Tiger.svg",
          ),
        ),
        title: Text('Home'),
      )

working demo

enter image description here

full code

import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';

void main() => runApp(MyApp());

/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key}) : super(key: key);

  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _selectedIndex = 0;
  static const TextStyle optionStyle =
      TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  static const List<Widget> _widgetOptions = <Widget>[
    Text(
      'Index 0: Home',
      style: optionStyle,
    ),
    Text(
      'Index 1: Business',
      style: optionStyle,
    ),
    Text(
      'Index 2: School',
      style: optionStyle,
    ),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('BottomNavigationBar Sample'),
      ),
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Container(
              width: 30,
              height: 30,
              child: SvgPicture.asset(
                "assets/wikimedia/Ghostscript_Tiger.svg",
              ),
            ),
            title: Text('Home'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.business),
            title: Text('Business'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.school),
            title: Text('School'),
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.amber[800],
        onTap: _onItemTapped,
      ),
    );
  }
}
          

Upvotes: 1

Related Questions