Flutter 底部导航栏

Flutter bottom nav bar

我正在试验 flutter 中的底部导航栏。同样,对于 flutter 来说非常新。我可以在 BottomNavigationBarItem 中使用自定义 svg 图标而不是 flutter material 提供的图标吗?如果您能帮我提供一个代码片段,那就太棒了。

我现在正在开发这种类型的导航栏。enter image description here。我有这些自定义图标,但我不知道如何使用它们。

使用包来使用svg,说明在这里:https://pub.dev/packages/flutter_svg#-installing-tab-

然后将 svg 文件包含到图标参数中的底部导航栏项目

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

您可以复制粘贴 运行 下面的完整代码
您可以使用包 https://pub.dev/packages/flutter_svg
并用 Container 包裹以提供 widthheight
示例 svg 文件 https://github.com/dnfield/flutter_svg/blob/master/example/assets/wikimedia/Ghostscript_Tiger.svg

代码片段

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

工作演示

完整代码

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,
      ),
    );
  }
}