我正在用底部的导航栏进行试验。再次,扑扑非常新。我可以使用自定义svg图标代替BottomNavigationBarItem中flutter的材料提供的图标吗?如果您可以通过代码片段帮助我,那就太好了。
我现在正在使用这种类型的导航栏。enter image description here。我有这些自定义图标,但我不知道如何使用它们。
答案 0 :(得分:4)
使用软件包使用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")
),
]
答案 1 :(得分:1)
您可以在下面复制粘贴运行完整代码
您可以使用软件包https://pub.dev/packages/flutter_svg
并用Container
包装以提供width
和height
样本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,
),
);
}
}