flutter - 自定义 Flutter 图标位置

标签 flutter dart icons

我正在尝试添加自定义图标(Font Awesome Light)。我已经尝试过提供它的软件包,但仅限于免费图标。

我遵循了本教程:https://medium.com/flutterpub/how-to-use-custom-icons-in-flutter-834a079d977

图标在这里,但不在我的 BottomNavigationBar 的中心,如下图所示。

它与默认图标一起使用,我现在不知道该尝试什么。如果有任何帮助或想法,我将不胜感激。

主.dart

@override
Widget build(BuildContext context) {
return Scaffold(
  body: Center(
    child: _widgetOptions.elementAt(_selectedIndex),
  ),
  bottomNavigationBar: BottomNavigationBar(
  items: const <BottomNavigationBarItem>[
    ...
    BottomNavigationBarItem(
      icon: Icon(WuliIcons.dumbbell),
      title: Text('School'),
      backgroundColor: Colors.red
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.school),
      title: Text('School'),
      backgroundColor: Colors.red
    ),
  ],
  currentIndex: _selectedIndex,
  onTap: _onItemTapped,
),
);
}

我在哪里定义我的图标类:

import 'package:flutter/widgets.dart';

class WuliIcons {
  WuliIcons._();

  static const _kFontFam = 'FontAwesomeLight';

  static const IconData dumbbell = const IconData(0xf44b, fontFamily: _kFontFam);
}

pubspec.yml

flutter:
  fonts:
    - family:  FontAwesomeLight
      fonts:
       - asset: fonts/fa-light-300.ttf

最佳答案

在没有导航栏的情况下进行了大量研究后,自定义图标似乎比他告诉他的 parent 的尺寸更大(正如您在图片上看到的,它只是一个容器中的图标)。

enter image description here

然后我在导航栏中尝试了一些奇怪的东西,制作了一个宽度较大的容器,它起作用了......

BottomNavigationBarItem(
  icon: Container(width: 1000, child: Icon(FontAwesomeLight.utensils_alt)),
  title: Text('Food'),
  backgroundColor: Colors.purple
),

关于flutter - 自定义 Flutter 图标位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56755163/

相关文章:

android - D8 : Cannot fit requested classes in a single dex file

flutter - 选择单个 CheckboxListTile 项目会选择多个项目?

android - 从操作栏 xamarin 中删除图标

css - 管理 LTE 图标不能离线工作?

C#:使用包含多个图像的 .ico 文件

ios - 尝试将Flutter应用程序上传到ItunesConnect时出现UIWebView错误

flutter - 修复了 Flutter Dart 上 DataTable 的列和行标题

按下后退按钮时 Flutter 应用程序崩溃

regex - 如何在Flutter中根据指定的关键字拆分字符串?

dart - 父级分离回调中的children.clear期间的附加回调